Cocos2dx 2.0.4-Lua add custom class

Source: Internet
Author: User

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.