在cocos2d-x中绑定和调用c++自定义的类

这篇博客介绍了如何在cocos2d-x 2.2.2版本中,通过lua调用C++自定义的精灵类。首先,创建cocos2dx-lua项目并编译运行。接着,定义并实现C++的精灵类,然后使用tolua++将该类编译成LuaCoco2d.cpp,以便lua调用。最后,在lua脚本中调用自定义类的方法,成功运行验证了绑定和调用的正确性。

以lua调用我们用c++自定义的一个精灵为例。(测试版本为2.2.2)


1. 创建cocos2dx-lua项目。

找到cocos2dx的根目录,进入到tools->project-creator中,创建一个批处理脚本,如run_lua.bat,内容如下

@echo off
:begin
set/p a1=请输入项目名称并按回车:
rem echo 你输入的项目名称是:%a1%,如果确认请按y
set/p a2=你输入的项目名称是:%a1%,如果确认请按y并回车:

if %a2% NEQ y goto begin
python create_project.py -project %a1% -package com.cocos2dx.%a1% -language lua
pause

执行该脚本,输入项目名,如test_lua,创建完成后,进入cocos2dx的根目录中的projects文件夹,如果看到有一个已你的项目名的文件夹则说明创建成功。


2. 编译test_lua项目,让项目跑起来。

本机是在win7环境下进行的,所以在项目的文件夹中,进入到proj.win32文件夹,打开test_lua.sln解决方案。build一下test_lua。如果没有报错则运行该项目。就可以看到测试用例的画面了,如图


看到以上画面则表明创建成功。


3. 用c++自定义精灵类。

头文件

#ifndef __TEST_SPRITE_H__
#define __TEST_SPRITE_H__
#include "cocos2d.h"
USING_NS_CC;
class TestSprite : public CCSprite
{
public:
   CREATE_FUNC(TestSprite);
   bool init();
   //自定义方法,供lua调用
   void setPosCenter();
   static CCLabelTTF* createLabel(const char* c);
protected:
	unsigned int testInt;
};

#endif 

cpp文件

#include "TestSprite.h"
USING_NS_CC;

bool TestSprite::init() {
	testInt = 0;
	bool result = false;
	if(CCSprite::init()) {
		this->initWithFile("HelloWorld.png");
		result = true;
	}
	return result;
}
void TestSprite::setPosCenter() {
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	this->setPosition(ccp(size.width / 2, size.height / 2));
}
CCLabelTTF* TestSprite::createLabel(const char* c) {
	CCLabelTTF* label = CCLabelTTF::create(c, "Arial", 16);  
	return label;
}


4. 用tolua++编译到LuaCoco2d.cpp,供lua调用

在cocos2dx引擎根目录\tools\tolua++中,创建一个pkg文件,如TestSprite.pkg.

    1) enum keeps the same
    2) remove CC_DLL for the class defines, pay attention to multi inherites
    3) remove inline keyword for declaration and implementation
    4) remove public protect and private
    5) remove the decalration of class member variable
    6) keep static keyword
    7) remove memeber functions that declared as private or protected 


pkg文件

class TestSprite : public CCSprite
{
   static TestSprite* create();
   bool init();
   void setPosCenter();
   static CCLabelTTF* createLabel(const char* c);
};


解压tolua++.rar到当前目录,执行build.bat文件,切换到cocos2dx引擎根目录\scripting\lua\cocos2dx_support,在LuaCocos2d.h中加入自定义类的头文件

#ifndef __LUACOCOS2D_H_
#define __LUACOCOS2D_H_

#ifdef __cplusplus

extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif

//自定义类的头文件
#include "TestSprite.h"

TOLUA_API int tolua_Cocos2d_open(lua_State* tolua_S);

#endif // __LUACOCOS2D_H_

注:要把自定义类放在当前的目录中


5. 在lua中调用自定义类的方法

切换到cocos2dx引擎根目录\\projects\你的lua项目\Resources中,打开hello.lua,定义一个新的层

    -- create my define TestSprite
    local function createTestSprte()
        local testLayer = CCLayer:create()
        local size = CCDirector:sharedDirector():getVisibleSize()
        
        local testLabel = TestSprite:createLabel("MyTestSprite")
        testLabel:setPosition(ccp(200, 200))
        testLayer:addChild(testLabel, 1)

        
        local testSprite = TestSprite:create()
        testSprite:setPosCenter()--调用自定义方法
        testLayer:addChild(testSprite)
        
        return testLayer
    end
加载次层

 local sceneGame = CCScene:create()
    --sceneGame:addChild(createLayerFarm())
    sceneGame:addChild(createTestSprte())
    --sceneGame:addChild(createLayerMenu())
    CCDirector:sharedDirector():runWithScene(sceneGame)

6. 编译,运行项目



OK,看到上图后说明调用成功




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值