Lua does not support classes the way that languages like C++, Java and ActionScript do. But Lua is a multi-paradigm language and have roots from prototype-based languages. In Lua, each object can define its own behaviour through metatables. Therefore, it is possible to emulate OO programming and classes in Lua.
(For the detailed discussion of object oriented programming in Lua, please refer to http://www.lua.org/pil/16.html)
Gideros follows the same paradigm in its API design. Each instance created by Gideros API is a Lua table with a metatable attached.
Instances in Gideros is created through
new function. For example, to create
Bitmap and a
local sprite = Sprite.new() local texture = Texture.new("image.png") local bitmap = Bitmap.new(texture) local timer = Timer.new(1000, 0)
Core.class function is used to create your own classes through inheritance. You
can create your own classes like:
MyClass = Core.class()
or inherit from Gideros API’s own classes (
Sprite, etc.). For example, you can create your EventDispatcher class as:
MyEventDispatcher = Core.class(EventDispatcher)
By using Inheritance, you can design and implement the visual elements of your game separately:
StartButton = Core.class(Sprite) -- create your own start button class Menu = Core.class(Sprite) -- create your own menu class Player = Core.class(Sprite) -- create your own player class function Player:walk() -- walk logic end function Player:jump() -- jump logic end stage:addChild(Player.new()) -- create and add a player instance to the stage
When an instance is created,
init function is called to do the initialization:
Player = Core.class(Sprite) function Player:init() -- do the initialization of Player instance self.health = 100 self.speed = 3 end local player = Player.new() -- after Player instance is created, init function is called
Whether to use inheritance or not is related to your programming taste. It’s possible to implement a whole game without creating custom classes. You can refer to “Jumping Ball” and “Jumping Balls” examples to see the difference between designing your code with classes or not.