我们有一个C++类,TestLayer.h
#ifndef __TEST_LAYER_H__
#define __TEST_LAYER_H__
#include "cocos2d.h"
namespace cocos2d{
class TestLayer : public Layer
{
public:
static TestLayer* create();
bool init();
void myPrint(const std::string &str);
TestLayer(void);
~TestLayer(void);
};
}
int register_TestLayer(lua_State* tolua_S);
#endif
TestLayer.cpp
#include "TestLayer.h"
using namespace cocos2d;
TestLayer::TestLayer()
{
}
TestLayer::~TestLayer()
{
}
void TestLayer::myPrint(const std::string &str)
{
CCLOG(str.c_str());
}
TestLayer* TestLayer::create()
{
TestLayer *pRet = new TestLayer();
if ( pRet && pRet->init() ) {
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return nullptr;
}
bool TestLayer::init()
{
if ( !Layer::init() ) {
return false;
}
return true;
}
//lua binding
#include "tolua_fix.h"
#include "LuaBasicConversions.h"
int lua_cocos2dx_TestLayer_create(lua_State* tolua_S)
{
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertable(tolua_S,1,"cc.TestLayer",0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(tolua_S) - 1;
if (argc == 0)
{
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_TestLayer_create'", nullptr);
return 0;
}
TestLayer* ret = TestLayer::create();
object_to_luaval<TestLayer>(tolua_S, "cc.Layer",(TestLayer*)ret);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d\n ", "cc.Layer:create",argc, 0);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_TestLayer_create'.",&tolua_err);
#endif
return 0;
}
int lua_cocos2dx_TestLayer_myPrint(lua_State* tolua_S)
{
int argc = 0;
TestLayer* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertype(tolua_S,1,"cc.TestLayer",0,&tolua_err)) goto tolua_lerror;
#endif
cobj = (cocos2d::TestLayer*)tolua_tousertype(tolua_S,1,0);
#if COCOS2D_DEBUG >= 1
if (!cobj)
{
tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_TestLayer_myPrint'", nullptr);
return 0;
}
#endif
argc = lua_gettop(tolua_S)-1;
if (argc == 1)
{
std::string arg0;
ok &= luaval_to_std_string(tolua_S, 2,&arg0, "cc.TestLayer:myPrint");
if(!ok)
{
tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_TestLayer_myPrint'", nullptr);
return 0;
}
cobj->myPrint(arg0);
lua_settop(tolua_S, 1);
return 1;
}
luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.TestLayer:myPrint",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_TestLayer_myPrint'.",&tolua_err);
#endif
return 0;
}
int lua_register_cocos2dx_TestLayer(lua_State* tolua_S)
{
tolua_usertype(tolua_S,"cc.TestLayer");
tolua_cclass(tolua_S,"TestLayer","cc.TestLayer","cc.Layer",nullptr);
tolua_beginmodule(tolua_S,"TestLayer");
tolua_function(tolua_S,"create", lua_cocos2dx_TestLayer_create);
tolua_function(tolua_S,"myPrint",lua_cocos2dx_TestLayer_myPrint);
tolua_endmodule(tolua_S);
std::string typeName = typeid(TestLayer).name();
g_luaType[typeName] = "cc.TestLayer";
g_typeCast["TestLayer"] = "cc.TestLayer";
return 1;
}
int register_TestLayer(lua_State* tolua_S)
{
lua_getglobal(tolua_S, "_G");
if (lua_istable(tolua_S,-1))//stack:...,_G,
{
tolua_open(tolua_S);
tolua_module(tolua_S,"cc",0);
tolua_beginmodule(tolua_S,"cc");
lua_register_cocos2dx_TestLayer(tolua_S);
tolua_endmodule(tolua_S);
}
lua_pop(tolua_S, 1);
return 1;
}
这样就写好了一个c++类及其绑定函数,然后我们在Appdelegate.cpp中加入如下代码
bool AppDelegate::applicationDidFinishLaunching()
{
// set default FPS
Director::getInstance()->setAnimationInterval(1.0 / 60.0f);
// register lua module
auto engine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
lua_State* L = engine->getLuaStack()->getLuaState();
lua_module_register(L);
register_TestLayer(L);
// If you want to use Quick-Cocos2d-X, please uncomment below code
// register_all_quick_manual(L);
LuaStack* stack = engine->getLuaStack();
stack->setXXTEAKeyAndSign("2dxLua", strlen("2dxLua"), "XXTEA", strlen("XXTEA"));
//register custom function
//LuaStack* stack = engine->getLuaStack();
//register_custom_function(stack->getLuaState());
#if (COCOS2D_DEBUG > 0) && (CC_CODE_IDE_DEBUG_SUPPORT > 0)
// NOTE:Please don't remove this call if you want to debug with Cocos Code IDE
RuntimeEngine::getInstance()->start();
cocos2d::log("iShow!");
#else
if (engine->executeScriptFile("src/main.lua"))
{
return false;
}
#endif
return true;
}
OK,大功告成,我们只要在lua文件中做如下调用进行测试:
local MainScene = class("MainScene", cc.load("mvc").ViewBase)
function MainScene:onCreate()
-- add background image
display.newSprite("MainSceneBg.jpg")
:move(display.center)
:addTo(self)
-- add play button
local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
:onClicked(function()
--self:getApp():enterScene("PlayScene")
local layer = cc.TestLayer:create()
layer:myPrint("哈哈")
end)
cc.Menu:create(playButton)
:move(display.cx, display.cy - 200)
:addTo(self)
end
return MainScene
可以看到,lua文件中,成功打印“哈哈”