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
- Declare a new scene class, named “MainScene”.
- ctor() is the constructor for the MainScene, the initialization code of scene should placed here.
More information refer to Lua Object-Oriented Programming
- 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().
- Text node used TTF provided by Rapid2D. The parameter is a bit complex, refer to Label chapter for more information.
- Add text node to scene.
- New a object would be failed if you return nil here, normally return self for successful.
- As MainScene was declare a local variable, you must return class name here to make require() get the class.