Mainly from himi street http://www.himigame.com/lua-2/985.html, made some modifications.
Environment: vs2010 + cocos2d-x 2.0.4 win7
A class hsprite inherited from ccsprite:
Hsprite. h
#ifndef HSprite_h#define HSprite_h#include "cocos2d.h"USING_NS_CC;class HSprite : public CCSprite{public: static HSprite* createHSprite(const char* name); void hspriteInit();};#endif
Hsprite. cpp
#include "HSprite.h"HSprite* HSprite::createHSprite(const char* name){ HSprite* sp = new HSprite(); if(sp && sp->initWithFile(name)){ sp->hspriteInit(); sp->autorelease(); return sp; } CC_SAFE_DELETE(sp); return NULL;}void HSprite::hspriteInit(){ CCMessageBox("create HSprite success", "ss_Lua");}
Add
#include "HSprite.h"
In luaco cos2d. cpp
static void tolua_reg_types (lua_State* tolua_S){ ...... tolua_usertype(tolua_S,"HSprite");}
Do not add at the beginning of tolua_cocos2d_open, preferably at the end
TOLUA_API int tolua_Cocos2d_open (lua_State* tolua_S){ ...... tolua_cclass(tolua_S, "HSprite", "HSprite", "CCSprite", NULL); tolua_beginmodule(tolua_S,"HSprite"); tolua_function(tolua_S,"createHSprite",tolua_ss_HSprite_createHSrpite00); tolua_endmodule(tolua_S);}
Then define it before tolua_cocos2d_open
/* method: create of class CCSprite */#ifndef TOLUA_DISABLE_tolua_ss_HSprite_createHSrpite00static int tolua_ss_HSprite_createHSrpite00(lua_State* tolua_S){ tolua_Error tolua_err; if ( !tolua_isusertable(tolua_S,1,"HSprite",0,&tolua_err) || !tolua_isstring(tolua_S,2,0,&tolua_err) || !tolua_isnoobj(tolua_S,3,&tolua_err) ) goto tolua_lerror; else { const char* pszfilename = ((const char*) tolua_tostring(tolua_S,2,0)); { HSprite* tolua_ret = (HSprite*) HSprite::create(pszfilename); 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,"HSprite"); } } return 1;tolua_lerror: return 0;}#endif //#ifndef TOLUA_DISABLE
For more information, see himi...