zoukankan      html  css  js  c++  java
  • lua使用cocos2dx中的函数

    在lua中调用cocos2d-x函数 需在lua中注册声明 才可在项目中调用Lua文件避免出现nil

    下面是我的实例说明,我要加的是CCControlSlider,其实很简单,首先我们找到Luacocos2d.cpp,

    仔细看看,里面已经有很多类都已经注册声明,我们何不就仿照他们已有的,一一添加注册声明。

    在Luacocos2d.cpp中的 tolua_reg_types函数中加入:

    tolua_usertype(tolua_S,"CCControlSlider");

    在Luacocos2d.cpp中的 tolua_Cocos2d_open函数中打开我们的CCControlSlider并声明CCControlSlider类中我们要用到的函数create,

    要加CCControlSlider类其他的函数也是此法,我这里只举其中一个create函数:

      tolua_cclass(tolua_S,"CCControlSlider","CCControlSlider","",NULL);
      tolua_beginmodule(tolua_S,"CCControlSlider");
       tolua_function(tolua_S,"create",tolua_Cocos2d_CCControlSlider_create00);
      tolua_endmodule(tolua_S);


    好了,create函数的声明就写好了,记得下面要添加 “create” 对应的tolua_Cocos2d_CCControlSlider_create00函数实现,就在Luacocos2d.cpp这个类中添加咯:

    /* method: create of class  CCControlSlider */
    #ifndef TOLUA_DISABLE_tolua_Cocos2d_CCControlSlider_create00
    static int tolua_Cocos2d_CCControlSlider_create00(lua_State* tolua_S)
    {
    #ifndef TOLUA_RELEASE
     tolua_Error tolua_err;
     if (
         !tolua_isusertable(tolua_S,1,"CCControlSlider",0,&tolua_err) ||
         !tolua_isstring(tolua_S,2,0,&tolua_err) ||
         !tolua_isstring(tolua_S,3,0,&tolua_err) ||
         !tolua_isstring(tolua_S,4,0,&tolua_err) ||
         !tolua_isnoobj(tolua_S,5,&tolua_err)
     )
      goto tolua_lerror;
     else
    #endif
     {
      const char* normalSlider = ((const char*)  tolua_tostring(tolua_S,2,0));
      const char* selectedSlider = ((const char*)  tolua_tostring(tolua_S,3,0));
      const char* disabledSlider = ((const char*)  tolua_tostring(tolua_S,4,0));
      {
       CCControlSlider* tolua_ret = (CCControlSlider*)  CCControlSlider::create(normalSlider,selectedSlider,disabledSlider);
        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,"CCControlSlider");
      }
     }
     return 1;
    #ifndef TOLUA_RELEASE
     tolua_lerror:
     tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err);
     return 0;
    #endif
    }
    #endif //#ifndef TOLUA_DISABLE

    这样就O啦,我们可以在Lua中用到cocos2d-x下的CCControlSlider类中的create,只是create哦,如果你还想用到CCControlSlider类中更多的函数,就按上面的去做就OK了。

  • 相关阅读:
    聊天工具分享bug
    Git命令查看代码提交次数
    短链接生成实例
    .Net MVC用户注册验证码
    js写验证码
    笔记
    jq获取数组中的某个字段拆分成字符串。
    IIS部署后中文Cookie乱码
    C#反射(Reflection)与特性(Attribute)实例
    jmm
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3047846.html
Copyright © 2011-2022 走看看