Use cocos2dx-lua development, inevitably define their own class, but how to use custom class?
First, let's take a look at how lua calls c ++:
Lua script code-> use the coocs2dx intermediate parsing Layer Code-> convert it and call the front-end code of cocos2dx c ++
The code at the coocs2dx intermediate parsing layer is in the libs/lua/cocos2dx_support/luaco cos2d. cpp file. For more information, see this file.
That is to say, you have defined a class, and lua can call your own defined class. Your custom class must be stated in the parsing file luaco cos2d. cpp.
After reading the luaco cos2d. cpp file, some people may be dizzy and do not know how to declare their defined classes in luaco cos2d. cpp. However, don't worry. cocos2dx already provides the tolua ++ tool to automatically compile and generate a new luaco cos2d. cpp file.
Start to enter the subject.
1. Create a Demo project for the coocs2dx-lua and define a class in the class.
SNSprite. h
[Cpp]
<SPAN style = "FONT-SIZE: 14px"> //
// SNSprite. h
// LuaDemo
//
// Created by LiuYanghui on 13-4-8.
//
//
# Ifndef _ LuaDemo _ SNSprite __
# Define _ LuaDemo _ SNSprite __
# Include "cocos2d. h"
USING_NS_CC;
Class SNSprite: public CCSprite {
Public:
Static SNSprite * create (const char * name );
Private:
Void initData ();
};
# Endif/* defined (_ LuaDemo _ SNSprite __)*/
</SPAN>
//
// SNSprite. h
// LuaDemo
//
// Created by LiuYanghui on 13-4-8.
//
//
# Ifndef _ LuaDemo _ SNSprite __
# Define _ LuaDemo _ SNSprite __
# Include "cocos2d. h"
USING_NS_CC;
Class SNSprite: public CCSprite {
Public:
Static SNSprite * create (const char * name );
Private:
Void initData ();
};
# Endif/* defined (_ LuaDemo _ SNSprite __)*/
SNSprite. cpp
[Cpp]
<SPAN style = "FONT-SIZE: 14px"> //
// SNSprite. cpp
// LuaDemo
//
// Created by LiuYanghui on 13-4-8.
//
//
# Include "SNSprite. h"
SNSprite * SNSprite: create (const char * name ){
SNSprite * sprite = new SNSprite ();
If (sprite & sprite-> initWithFile (name )){
Sprite-> initData ();
Sprite-> autorelease ();
Return sprite;
}
CC_SAFE_DELETE (sprite );
Return NULL;
}
Void SNSprite: initData (){
CCActionInterval * action = CCSequence: createWithTwoActions (CCMoveTo: create (1.0, ccp (300,300 )),
CCMoveTo: create (1.0, ccp (100,100 ))
);
This-> runAction (CCRepeatForever: create (action ));
} </SPAN>
//
// SNSprite. cpp
// LuaDemo
//
// Created by LiuYanghui on 13-4-8.
//
//
# Include "SNSprite. h"
SNSprite * SNSprite: create (const char * name ){
SNSprite * sprite = new SNSprite ();
If (sprite & sprite-> initWithFile (name )){
Sprite-> initData ();
Sprite-> autorelease ();
Return sprite;
}
CC_SAFE_DELETE (sprite );
Return NULL;
}
Void SNSprite: initData (){
CCActionInterval * action = CCSequence: createWithTwoActions (CCMoveTo: create (1.0, ccp (300,300 )),
CCMoveTo: create (1.0, ccp (100,100 ))
);
This-> runAction (CCRepeatForever: create (action ));
}
The above is a simple function for defining a class.
2. Use tolua ++ to add the custom class to the luaco cos2d. cpp file.
The tolua ++ tool is in the/tools/tolua ++/directory.
1. Compile the pkg File
First, we can see that there are a lot of pkg files in it. Therefore, we should first write the pkg file for the custom class. The file writing rules are in README. I will briefly write the rules:
Writing. pkg files
1) enum keeps the same // the enumerated type remains unchanged
2) remove CC_DLL for the class defines, pay attention to multi inherites // Delete the class definition of cc_dll and use multiple inheritance instead.
3) remove inline keyword for declaration and implementation // Delete inline keyword declarations and Implementations
4) remove public protect and private // delete an access restriction
5) remove the decalration of class member variable // delete a member variable
6) keep static keyword // retain static keywords
7) remove memeber functions that declared as private or protected // non-public functions are deleted.
The above are the rules for compiling pkg files.
Write the pkg file of the custom file below. Text only, suffix changed to pkg
SNSprite. pkg
[Cpp]
<SPAN style = "FONT-SIZE: 14px"> class SNSprite: public CCSprite {
Static SNSprite * create (const char * name );
}; </SPAN>
Class SNSprite: public CCSprite {
Static SNSprite * create (const char * name );
};
Pay attention to comparing the previous header file declaration to learn more about the pkg file writing rules. Put the compiled pkg file in tolua ++ and put it together with all pkg files.
2. register the added pkg file in the Cocos2d. pkg file.
Open the Cocos2d. pkg file in a text editor and add our registration statement to the last line:
$ Pfile "SNSprite. pkg"
3. Configure build. sh to compile the script
In the toluaw.gov.mac.zip folder, unzip tolua ++ for mac.
Open the build. sh file and modify it to the following:
[Delphi]
<SPAN style = "FONT-SIZE: 14px"> #! /Bin/bash
#
# Invoked build. xml, overriding the lolua ++ property
SCRIPT_DIR = $ (cd "$ (dirname" $ {BASH_SOURCE [0]} ")" & pwd)
TOLUA =/APP/cocos2d-2.1rc0-x-2.1.2/tools/tolua ++
If [-z "$ {TOLUA}"]; then
TOLUA = 'which tolua ++ 100'
Fi
If [-z "$ {TOLUA}"]; then
Echo "Unable to find tolua ++ (or tolua ++ 5.1) in your PATH ."
Exit 1
Fi
Cd $ {SCRIPT_DIR}
$ {TOLUA}-L basic. lua-o/Users/liuyanghui/Desktop/LuaDemo/libs/lua/cocos2dx_support/luaccos2d. cpp Cocos2d. pkg
</SPAN>
#! /Bin/bash
#
# Invoked build. xml, overriding the lolua ++ property
SCRIPT_DIR = $ (cd "$ (dirname" $ {BASH_SOURCE [0]} ")" & pwd)
TOLUA =/APP/cocos2d-2.1rc0-x-2.1.2/tools/tolua ++
If [-z "$ {TOLUA}"]; then
TOLUA = 'which tolua ++ 100'
Fi
If [-z "$ {TOLUA}"]; then
Echo "Unable to find tolua ++ (or tolua ++ 5.1) in your PATH ."
Exit 1
Fi
Cd $ {SCRIPT_DIR}
$ {TOLUA}-L basic. lua-o/Users/liuyanghui/Desktop/LuaDemo/libs/lua/cocos2dx_support/luaccos2d. cpp Cocos2d. pkg
Note the following two changes:
A, TOLUA =/APP/cocos2d-2.1rc0-x-2.1.2/tools/tolua ++ this is your mac version of tolua ++ address, please change to your own.
B. $ {TOLUA}-L basic. lua-o/Users/liuyanghui/Desktop/LuaDemo/libs/lua/cocos2dx_support/luaccos2d. cpp Cocos2d. pkg
/Users/liuyanghui/Desktop/LuaDemo/libs/lua/cocos2dx_support/luaco cos2d after parameter-o. cpp is to generate a new luaco cos2d. modify the cpp storage path to your own.
4. Execute the compilation script to generate the luaco cos2d. cpp file.
Terminal cd to build. sh directory. Run:
./Build. sh
After the execution, a new luaco cos2d. cpp file is generated in the output directory you have defined. The classes we have defined are also added.
3. Call a custom class in lua
In the Demo project of the new cocos2dx-lua just now, add the function that calls the custom class in the hello. lua file:
[Plain]
<SPAN style = "FONT-SIZE: 14px"> local function createSunnyLayer ()
Local layerSunny = CCLayer: create ()
Local labTips = CCLabelTTF: create ("this icon is the custom class used", "Arial", 18)
LabTips: setPosition (ccp (240,280 ))
LayerSunny: addChild (labTips)
Local sp = SNSprite: create ("Icon.png ")
Sp: setPosition (CPP (100,100 ))
LayerSunny: addChild (sp)
Return layerSunny
End
-- Play background music, preload effec t </SPAN>
Local function createSunnyLayer ()
Local layerSunny = CCLayer: create ()
Local labTips = CCLabelTTF: create ("this icon is the custom class used", "Arial", 18)
LabTips: setPosition (ccp (240,280 ))
LayerSunny: addChild (labTips)
Local sp = SNSprite: create ("Icon.png ")
Sp: setPosition (CPP (100,100 ))
LayerSunny: addChild (sp)
Return layerSunny
End
-- Play background music, preload effec t
Add to scene:
[Plain]
<SPAN style = "FONT-SIZE: 14px"> -- run
Local sceneGame = CCScene: create () -- create scenario
-- SceneGame: addChild (createLayerFarm () -- adds the farm layer to the scenario
-- SceneGame: addChild (createLayerMenu () -- adds the menu interface layer to the scenario
SceneGame: addChild (createSunnyLayer ())
CCDirector: sharedDirector (): runWithScene (sceneGame)
</SPAN>
-- Run
Local sceneGame = CCScene: create () -- create scenario
-- SceneGame: addChild (createLayerFarm () -- adds the farm layer to the scenario
-- SceneGame: addChild (createLayerMenu () -- adds the menu interface layer to the scenario
SceneGame: addChild (createSunnyLayer ())
CCDirector: sharedDirector (): runWithScene (sceneGame)