Cocos2d-x LUA Binding implements access to LUA's custom object in C ++, cocos2d-xlua
LUA Binding is simpler than JSBinding. Whether it is automatically bound by a script or manually writing the Binding code, LUA can easily access C ++ classes and objects. If you want to access the custom classes and objects in LUA in C ++, You need to modify the C ++ code.
Application scenarios:
1. Assume that there is a class MyLayer in LUA that inherits CCLayer and adds the four attributes a, B, c, and d.
2. Create a MyLayer Instance Object in LUA, add it to the current Scene, and then use Scene. when the getChildByTag method obtains the MyLayer instance object, it will find that the custom attributes a, B, c, and d have all disappeared because the toluafix_pushusertype_ccobject (tolua_S, nID, pLuaID, (void *) tolua_ret, "CCNode"); A CCNode is returned, and the following is luaco cos2d. code in cpp
static int tolua_Cocos2d_CCNode_getChildByTag00(lua_State* tolua_S){#ifndef TOLUA_RELEASE tolua_Error tolua_err; if ( !tolua_isusertype(tolua_S,1,"CCNode",0,&tolua_err) || !tolua_isnumber(tolua_S,2,0,&tolua_err) || !tolua_isnoobj(tolua_S,3,&tolua_err) ) goto tolua_lerror; else#endif { CCNode* self = (CCNode*) tolua_tousertype(tolua_S,1,0); int tag = ((int) tolua_tonumber(tolua_S,2,0));#ifndef TOLUA_RELEASE if (!self) tolua_error(tolua_S,"invalid 'self' in function 'getChildByTag'", NULL);#endif { CCNode* tolua_ret = (CCNode*) self->getChildByTag(tag); int nID = (tolua_ret) ? (int)tolua_ret->m_uID : -1; int* pLuaID = (tolua_ret) ? &tolua_ret->m_nLuaID : NULL; toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCNode"); } } return 1;#ifndef TOLUA_RELEASE tolua_lerror: tolua_error(tolua_S,"#ferror in function 'getChildByTag'.",&tolua_err); return 0;#endif}
3. The solution is very simple,
A) First, call tolua_usertype (tolua_S, "MyLayer"); and register the custom class. The code can be added to any place where the pointer lua_State * can be called, but it must be called only once during initialization.
B) write the following code in luaco cos2d. cpp and put it at your own position:
// Extended getChildByTag, which can return lua objects, by lizc/* method: getChildByTag of class CCNode */# ifndef struct int tolua_Cocos2d_CCNode_getChildByTag01 (lua_State * tolua_S) {# ifndef TOLUA_RELEASE tolua_Error tolua_err; if (! Tolua_isusertype (tolua_S, 1, "CCNode", 0, & tolua_err) |! Tolua_isnumber (tolua_S, 2, 0, & tolua_err) | (! Tolua_isvaluenil (tolua_S, 3, & tolua_err )&&! Tolua_isstring (tolua_S, 3,0, & tolua_err) |! Tolua_isnoobj (tolua_S, 4, & tolua_err) goto tolua_lerror; else # endif {CCNode * self = (CCNode *) tolua_tousertype (tolua_S, 1, 0); int tag = (int) tolua_tonumber (tolua_S, 2, 0); const char * userTypeName = (const char *) tolua_tostring (tolua_S, 3, 0); # ifndef TOLUA_RELEASE if (! Self) tolua_error (tolua_S, "invalid 'self 'in function 'getchildbytag'", NULL); # endif {CCNode * tolua_ret = (CCNode *) self-> getChildByTag (tag ); int nID = (tolua_ret )? (Int) tolua_ret-> m_uID:-1; int * pLuaID = (tolua_ret )? & Tolua_ret-> m_nLuaID: NULL; identifier (tolua_S, nID, pLuaID, (void *) tolua_ret, userTypeName) ;}} return 1; # ifndef identifier: return identifier (tolua_S ); # endif} # endif // # ifndef TOLUA_DISABLE
C), and then add
Tolua_function (tolua_S, "getChildByTag", tolua_Cocos2d_CCNode_getChildByTag01); To tolua_function (tolua_S, "getChildByTag", tags); below for convenient management.
4. Usage
-- MyLayer must inherit from a class in C ++. Here it inherits CCLayerlocal myLayer = MyLayer: create () myLayer. a = 1myLayer. B = 2 local scene = CCScene: create () scene: addChild (myLayer, 0, 0) myLayer = scene: getChildByTag (0, "MyLayer") myLayer = tolua. cast (myLayer, "MyLayer") cclog ("myLayer. a ".. myLayer. a) cclog ("myLayer. B ".. myLayer. b)
Cocos2d-x + lua class inheritance
AddChild receives two parameters. Try writing this. layer: addChild (cardListLayer)
How to combine cocos2d-x with Lua to use specific points
The cocos2d-x comes with the lua engine. Look at this. Although it is English, it is easy to understand.
Cocos2d-x NEW Lua Engine README
Main features
Support autorelease CCObject object.
Call Lua function from C ++ (local, global, closure), avoid memory leaks.
Add CCNode: setPosition (x, y), CCNode: getPosition () huge performance boost.
Remove needless class and functions from tolua ++. pkg files, improved performance.
CCMenuItem events handler.
CCNode onEnter/onExit events handler.
CCLayer touch & multi-touches events handler.
Check the two connections.
Www.cocos2d-x.org/..uments
Scripting and Translating between Programming ages