[Nagging]
When using Lua, The cocos2d-x provides us withClass (classname, super)This function.
It allows us to easily define a class, or inherit a class from the cocos2d-x.
PS: Class () is the cocos2d-x for our encapsulated function, itself Lua does not have this function.
[Class]
Class FunctionsIs defined in "cocos2d-x-3.2/Cocos/scripting/LUA-bindings/script/extern. Lua.
-- Create an class. Function Class (classname, super) LocalSupertype =Type(Super) LocalCLS
IfSupertype ~ = "Function"AndSupertype ~ = "Table"Then Supertype =Nil Super =Nil End
IfSupertype = "function"Or(SuperAndSuper. _ ctype = 1)Then -- Inherited from native C ++ object CLS = {}
IfSupertype = "table"Then -- Copy fields from super ForK, VIn Pairs(Super)DoCLS [k] = VEnd Cls. _ create = super. _ create Cls. Super = super Else Cls. _ create = super End
Cls.Ctor=Function()End Cls. _ cname = classname Cls. _ ctype = 1
FunctionCls.New (...) LocalInstance = Cls. _ create (...) -- Copy fields from class to native object ForK, VIn pairs(CLS)DoInstance [k] = VEnd Instance. Class = CLS Instance:Ctor (...) ReturnInstance End
Else -- Inherited from Lua object IfSuperThen CLS =Clone (Super) Cls. Super = super Else CLS = {ctor =Function()End} End
Cls. _ cname = classname Cls. _ ctype = 2-- Lua Cls. _ Index = CLS
FunctionCls.New (...) LocalInstance =Setretriable({}, CLs) Instance. Class = CLS Instance:Ctor (...) ReturnInstance End End
ReturnCLS End |
In Lua, the concept of a class is a table. The above function code mainly copies the table of the parent class super, and then defines two functions.Cls. New (...)AndCLS: ctor (...). It doesn't matter if we don't know the above Code, as long as we use it.
0. Usage
>Define a class:CLS =Class ("Class Name", "parent class ")
> Create an instance: OBJ = Cls. New ()
>Initialization function: CLS: ctor () is automatically called when an instance is created.
As long as you master these three steps, for our cocos2d-x development is enough!
1. Create a base class
First, createHeroClass, and then override the Hero class initialization function.Hero: ctor ().
Create another hero class instance objectHero. New ()Will automatically call the initialization function hero: ctor ().
Test output.
Note: The self in ctor () is the instance object, which is equivalent to this in the C ++ class.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4C/DB/wKiom1RGn37wHdw6AAFXC7srOWg614.jpg "Title =" 1.png" alt = "wkiom1rgn37whdw6aafxc7srowg614.jpg"/>
2. inherit the parent class
In cocos2d-x development, it is often necessary to inherit the class in the engine, such as layer, Sprite.
Here is an example. Modify the above Hero class and inherit fromSprite.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4C/DD/wKioL1RGoOWAXNLMAAIfl3ox2uQ600.jpg "Title =" 2.png" alt = "wkiol1rgoowaxnlmaaifl3ox2uq600.jpg"/>
3. For another example, inherit the layer class
I usually get usedCtor ()Only member variables of some classes are declared.
Then define a function.Create (), SetNew ()Encapsulate and then call it in create ()Init ()Function to initialize the instance object.
The advantage is that I use the same method in C ++.
3.1 define a class inherited from Layer
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4C/DD/wKioL1RGpdHSkzvoAAS4LwqyVUM952.jpg "Title =" initpintu.png "alt =" wkiol1rgpdhskzvoaas4lwqyvum952.jpg "/>
3.2 usage
> Create a scene scenario that contains the gamelayer layer: gamelayer: createscene ()
> Create a gamelayer layer -----------:Gamelayer: Create ()
> The Custom initialization function Init () is also called when gamelayer is created ()
> The member variables of the class are all in the ctor () .
3.3 download code
Http://down.51cto.com/data/1886438
This article is from the "summer wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1566513
Cc3.2 + Lua (3) -- Lua class