Hello World

After creating the project by ProjectCreator.py, you will Rapid2D’s Hello World code.

-- 1.
local MainScene = class("MainScene", rd.scene)

-- 2.
function MainScene:ctor()
    -- call super.ctor first
    -- 3.
    MainScene.super.ctor(self)

	-- 4.
    rd.label.TTF.new("Hello World!", 40, "Arial", {0, 0, 93, 255}, rd.label.CENTER)
    -- 5.
	:addTo(self)

    -- create success
    -- 6.
    return self
end

-- 7.
return MainScene
  1. Declare a new scene class, named “MainScene”.
  2. ctor() is the constructor for the MainScene, the initialization code of scene should placed here. More information refer to Lua Object-Oriented Programming
  3. In order to recursively call the constructor function, You should manual call super’s ctor() here. In most cases, it must be the first line of code of ctor().
  4. Text node used TTF provided by Rapid2D. The parameter is a bit complex, refer to Label chapter for more information.
  5. Add text node to scene.
  6. New a object would be failed if you return nil here, normally return self for successful.
  7. As MainScene was declare a local variable, you must return class name here to make require() get the class.