第9月第16天 tolua++ cocos2dx cocos2d-lua

1.

http://www.jianshu.com/p/1cdfc60da04f

2.lua c++

Lua访问C++类

现在,我们在Lua里面操作这个Student类。注意,我们绑定的每一个函数都需要一个student对象作为参数,这样使用有一点不太方便。

local s = cc.create()
cc.setName(s,"zilongshanren")
print(cc.getName(s))
cc.setAge(s,20)
print(cc.getAge(s))
cc.print(s)

最后,输出的结果为:

zilongshanren
20
My name is: zilongshanren, and my age is 20

提供Lua面向对象操作API

现在我们已经可以在Lua里面创建C++类的对象了,但是,我们最好是希望可以用Lua里面的面向对象的方式来访问。

local s = cc.create()
s:setName("zilongshanren")
s:setAge(20)
s:print()

而我们知道s:setName(xx)就等价于s.setName(s,xx),此时我们只需要给s提供一个metatable,并且给这个metatable设置一个key为”__index”,value等于它本身的metatable。最后,只需要把之前Student类的一些方法添加到这个metatable里面就可以了。

//入参 lua_to...(L,1) lua_to...(L,2)

int l_setName(lua_State* L)

{

    Student **s = (Student**)lua_touserdata(L,1);//luaL_checkudata(L,1,"cc.Student");

    luaL_argcheck(L, s != NULL, 1, "invalid user data");

    

    luaL_checktype(L, 2, LUA_TSTRING);//luaL_checktype(L, -1, LUA_TSTRING);

    

    std::string name = lua_tostring(L, 2);//lua_tostring(L, -1);

    (*s)->setName(name);

    return 0;

}

Student **s =  (Student**)lua_newuserdata(L, sizeof(Student*));  // lua will manage Student** pointer
    *s = new Student;
    luaL_getmetatable(L, "cc.Student");
    lua_setmetatable(L, -2);

http://www.tuicool.com/articles/YzUrQb2

https://github.com/groov0v/LuaCBind

3.cocos2d-lua

https://github.com/zcgg/douniu

https://github.com/mr-bogey/popstar

https://www.github.com/xiuzhifu/loogrpg/

https://github.com/search?p=3&q=-quick+%24QUICK_COCOS2DX_ROOT&type=Code&utf8=%E2%9C%93

https://github.com/rainswan/Brave

 https://github.com/calorie/game01

export QUICK_COCOS2DX_ROOT=/Users/temp/Downloads/quick-cocos2d-x

 

原文地址:https://www.cnblogs.com/javastart/p/7027128.html