cocos2d-x与lua用法整理
Cocos2d-x 2.20以上版本没有了创建模板,创建的方式改用了Python创建,方法如下:
python create_project.py -project HelloWorld -package com.Panda.Game -language cpp
若要创建lua工程,结尾的cpp改成lua
然后创建出来的sln工程文件打开来,直接运行会发现已经是个成品了,有画面有音效有动画= =
vcWxvqOsQXBwRGVsZWdhdGW1xGFwcGxpY2F0aW9uRGlkRmluaXNoTGF1bmNoaW5nuq/K/c/C0tG+rdC0wcu9xbG+tffTw7XEtPrC68HLoaPP69fUvLq2r8rWtcS/ydLUtNNDQ0x1YUVuZ2luZSogcEVuZ2luZSA9IENDTHVhRW5naW5lOjpkZWZhdWx0RW5naW5lKCk7v6rKvMm+tfSjrLKi1MvQ0NfUvLq1xHNjZW5lo7o8L3A+CjxwPnBEaXJlY3Rvci0+cnVuV2l0aFNjZW5lKEhlbGxvTHVhOjpzY2VuZSgpKTs8YnI+CjwvcD4KPHA+PGJyPgo8L3A+CjxwPsi7uvPS1M/CxNrI3dX7wO3X1MS+zbe1xLKpv82jqGh0dHA6Ly9ibG9nLmNzZG4ubmV0L211c2ljdnMvYXJ0aWNsZS9kZXRhaWxzLzg0NDA3MDejqbrNyum8rqOoobZjb2NvczJkLXjTzs+3v6q3otauwsOht6OpoaO40NC71/fV36OhPC9wPgo8cD7W99KqysfA7b3iQyYjNDM7JiM0Mzu6zWx1Yda7xNzNqLn9ttHVu82o0MWjrMDtveJsdWG20dW7tcTSu9CpveLO9rn9s8yhozwvcD4KPHA+SGVsbG9MdWEubHVhvcWxvsjnz8KjujwvcD4KPHA+bXlOYW1lID0g"PandaWu"
helloTable = {name = "Panda", IQ = 129}
function helloAdd(num1, num2, num3)
return (num1 + num2 + num3)
end
初始化,打开lua文件,设置堆栈指针:
lua_State* pL = lua_open();
luaopen_base(pL);
luaopen_math(pL);
luaopen_string(pL);
int err = luaL_dofile(pL, "HelloLua.lua");
CCLOG("open: %d", err);
lua_settop(pL, 0);
取myName变量:
lua_getglobal(pL, "myName");
int isstr = lua_isstring(pL, 1);
CCLOG("isstr = %d", isstr);
if (isstr != 0)
{
const char* str = lua_tostring(pL, 1);
CCLOG("get string %s", str);
}
取table中name和IQ:
lua_getglobal(pL, "helloTable");
lua_pushstring(pL, "name");
lua_gettable(pL, -2);
const char* name = lua_tostring(pL, -1);
CCLOG("%s", name);
lua_getglobal(pL, "helloTable");//这句还得写
lua_pushstring(pL, "IQ");//还是pushstring
lua_gettable(pL, -2);
int IQ = lua_tonumber(pL, -1);
CCLOG("%d", IQ);
调用lua中的helloAdd:
lua_getglobal(pL, "helloAdd");
lua_pushnumber(pL, 10);
lua_pushnumber(pL, 5);
lua_pushnumber(pL, 3);
lua_call(pL, 3, 1);//参数数量和返回值数量
int result = lua_tonumber(pL, -1);
CCLOG("result = %d", result);
最后:lua_close(pL);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>