cocos2dx中使用tolua++使lua调用c++函数

news/2024/7/5 11:38:29

一直想学学cocos2dx中如何使用tolua++工具使得lua脚本调用C++函数,今天就来搞一下,顺便记录下来:

首先,我们打开cocos2dx-2.2.4中projects下的test的VS工程,可以看到这个例子里面已经有一个HelloWorld的类,我们就用它来说明一下。

然后,我们照着HelloWorld类的定义来写pkg文件:

//MyClass.pkg

class HelloWorld : public cocos2d::CCLayer

{

      virtual bool init();

      static cocos2d::CCScene* scene();

      void menuCloseCallback(CCObject* pSender);

      static HelloWorld* create();

};

 

我们打开README文件可以看看书写pkg文件的语法:

1. Generating the lua<-->C bindings with tolua++

    Build scripts for windows (build.bat) and unix (build.sh) are provided

    to generate the relevant files after modifying the .pkg files.  These

    scripts basically run the following command:

        tolua++.exe -L basic.lua -o LuaCocos2d.cpp Cocos2d.pkg

    This will generate the bindings file and patch it with come cocos2dx

    specific modifications.

    On POSIX systems you can also just run "make" to build the bindings

    if/when you change .pkg files.

2. Writing .pkg files

    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

有一点还需要注意的是static cocos2d::CCScene* scene(); 这句话最好写成static CCScene* scene();不然的话会出现下面这种错误:

error in function 'runWithScene'.

argument #2 is 'cocos2d::CCScene'; 'CCScene' expected.

原因我也不太清楚,按照错误提示替换掉就行了。

 

接着,我们把写好的pkg文件放在tolua++目录下,在cocos2d.pkg文件末尾添加$pfile "MyClass.pkg",点击运行build.bat,这个文件主要是将cocos2d.pkg内包含的pkg文件整合生成LuaCocos2d.cpp,看下内部语法就知道了:

tolua++ -L basic.lua -o "../../scripting/lua/cocos2dx_support/LuaCocos2d.cpp" Cocos2d.pkg

然后,我们在LuaCocos2d.h中引入我们的HelloWorld的头文件,在将lualib工程添加到该工程来,在该工程添加lua头文件的引用,在链接其中添加lua51.lib和lualib.lib。

 

接下来我们添加一个lua脚本HelloWorld.lua,内容如下:

print("begin ...... ")

local director = CCDirector:sharedDirector()

local scene = HelloWorld:scene()

director:runWithScene(scene)

print("end ........ ")

 

在AppDelegate.cpp引入CCLuaEngine.h头文件,代码稍作如下修改:

//CCScene *pScene = HelloWorld::scene();

//pDirector->runWithScene(pScene);

CCScriptEngineProtocol* pEngine = CCLuaEngine::defaultEngine();

CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);

string fullpath = CCFileUtils::sharedFileUtils()->fullPathForFilename("HelloWorld.lua");

CCLuaEngine::defaultEngine()->executeScriptFile(fullpath.c_str());

 

最后运行看看:

转载于:https://www.cnblogs.com/yu-chao/p/4458179.html


http://www.niftyadmin.cn/n/666906.html

相关文章

MySQL 5.7的新特性(新功能)

与 MySQL 5.6 相比&#xff0c;MySQL 5.7 具有以下几个方面的新功能。 1) 随机 root 密码 MySQL 5.7 数据库初始化完成后&#xff0c;会自动生成一个 rootlocalhost 用户&#xff0c;root 用户的密码不为空&#xff0c;而是随机产生一个密码。 2) 自定义 test 数据库 MySQL …

MAC 10.12系统环境下无法打开360加固助手的解决

&#xff08;1&#xff09;系统环境&#xff1a; MAC 10.12.1 &#xff08;2&#xff09;问题详情 从360官方下载了加固助手之后&#xff0c;双击解压文件&#xff0c;直接双击 图标 &#xff0c;程序并没有运行起来&#xff0c;没有弹出登录界面 , 只是图标闪动了一下 然后&am…

大数据实时处理:百分点实时计算架构和算法

http://www.open-open.com/lib/view/open1393682536912.html转载于:https://www.cnblogs.com/diyunpeng/p/5199869.html

Error:Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry

AS报错处理 早上起来报这个错误,顿时间懵逼,我又没改文件怎么报这个错误,后来找到了解决办法 Gradle MyApp refresh failed.Error:Cannot locate factory for objects of type DefaultGradleConnector, as ConnectorServiceRegistry has been closed.123 解决方法 点击清理后!立…

jquery遍历数组与筛选数组的方法

grepgrep()方法用于数组元素过滤筛选 grep(array,callback,invert)array&#xff1a;待过滤数组;callback&#xff1a; 处理数组中的每个元素&#xff0c;并过滤元素&#xff0c;该函数中包含两个参数&#xff0c;第一个是当前数组元素的值&#xff0c;一个是当前数组元素的下标…

Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图

Android图表库MPAndroidChart(十)——散点图的孪生兄弟气泡图 起泡图和散点图如出一辙&#xff0c;但是个人认为要比散点图好看一点&#xff0c;我们来看下实际的演示效果 这个和散点图的实现很相似&#xff0c;我们一起来看下 一.基本实现 先看下我的xml <com.github.mikep…

MySQL客户端和服务器端工具集

MySQL 一般是安装在服务器上的&#xff0c;我们在客户端可以进行连接&#xff0c;然后可以进行一些增删改查操作。下面我们分服务器端和客户端来讲解一下 MySQL 的实用工具集。 MySQL 服务器端实用工具 1) mysqld SQL 后台程序&#xff08;即 MySQL 服务器进程&#xff09;。…

MySQL下载步骤详解(带安装教程)

用户可以根据自身的操作系统类型&#xff0c;从 MySQL 官方下载页面免费下载相应的服务器安装包。本书以 MySQL 5.7.20 为例介绍其在 Windows 10 操作系统下的安装和配置过程。 用户下载 Windows 图形化安装包的步骤如下。 步骤 1)&#xff1a;打开 MySQL 官方网站&#xff08…