一、首先在引擎的跟目录下找到cocos2d-x自带的工具tolua++

二、使用tolua++生成自定义类的声明

打开tool文件夹中的readme文件如下:

  1. 1. Generating the lua<-->C bindings with tolua++
  2. tolua++.exe -tCocos2d -o LuaCocos2d.cpp Cocos2d.pkg

我们为了方便可以制作一个批处理文件、放在tolua.exe同目录下、每次点击就可产生自定义类的声明、如下

  1. -----------------------------------------createCpp.bat---------------------------
  2. @echo on
  3. E:\gkh_netdisk\tools\tolua++\tolua++.exe -o testCtoLua.cpp testCtoLua.pkg
  4. @pause
  5. ---------------------------------------------------------------------------------

然后开始编写testCtoLua.pkg 要遵循以下的规则

  1. 2.Writing .pkg files  编写pkg文件
  2. 1)enum keeps the same 保持枚举类型不变
  3. 2)remove CC_DLL for the class defines, pay attention to multi inherites 删除CC_DLL的类定义、改为多继承
  4. 3)remove inline keyword for declaration and implementation 删掉声明的inline关键词
  5. 4)remove public protect and private 删除访问限定
  6. 5)remove the decalration of class member variable 删除类的成员变量
  7. 6)keep static keyword  保留statiic关键词
  8. 7)remove memeber functions that declared as private or protected 成员函数声明为私人或受保护的都删掉

我这里就不去写了、就直接用现成的CCDirector类做个测试

  1. ----------- CCDirector.pkg-----------
  2. typedef enum {
  3. /// Device oriented vertically, home button on the bottom
  4. kCCDeviceOrientationPortrait = 0, // UIDeviceOrientationPortrait,
  5. /// Device oriented vertically, home button on the top
  6. kCCDeviceOrientationPortraitUpsideDown = 1, // UIDeviceOrientationPortraitUpsideDown,
  7. /// Device oriented horizontally, home button on the right
  8. kCCDeviceOrientationLandscapeLeft = 2, // UIDeviceOrientationLandscapeLeft,
  9. /// Device oriented horizontally, home button on the left
  10. kCCDeviceOrientationLandscapeRight = 3, // UIDeviceOrientationLandscapeRight,
  11. } ccDeviceOrientation;
  12. class CCDirector : public CCObject
  13. {
  14. CCScene* getRunningScene(void);
  15. double getAnimationInterval(void);
  16. bool isDisplayFPS(void);
  17. void setDisplayFPS(bool bDisplayFPS);
  18. bool isRetinaDisplay();
  19. bool isPaused(void);
  20. unsigned int getFrames(void);
  21. CCSize getWinSize(void);
  22. CCSize getWinSizeInPixels(void);
  23. CCSize getDisplaySizeInPixels(void);
  24. CCPoint convertToGL(CCPoint obPoint);
  25. CCPoint convertToUI(CCPoint obPoint);
  26. void runWithScene(CCScene *pScene);
  27. void pushScene(CCScene *pScene);
  28. void popScene(void);
  29. void replaceScene(CCScene *pScene);
  30. CGFloat getContentScaleFactor(void);
  31. int getDeviceOrientation();
  32. static CCDirector* sharedDirector(void);
  33. };

然后将编写好的*.pkg文件复制到tolua++文件目录下、双击前文的批处理文件、不出意外就能生成相对应的Cpp文件了
 
CCDirector.cpp中的关键代码是这些:

  1. tolua_beginmodule(tolua_S,"CCDirector");
  2. tolua_function(tolua_S,"getRunningScene",tolua_CCDirector_CCDirector_getRunningScene00);
  3. tolua_function(tolua_S,"getAnimationInterval",tolua_CCDirector_CCDirector_getAnimationInterval00);
  4. tolua_function(tolua_S,"isDisplayFPS",tolua_CCDirector_CCDirector_isDisplayFPS00);
  5. tolua_function(tolua_S,"setDisplayFPS",tolua_CCDirector_CCDirector_setDisplayFPS00);
  6. tolua_function(tolua_S,"isRetinaDisplay",tolua_CCDirector_CCDirector_isRetinaDisplay00);
  7. tolua_function(tolua_S,"isPaused",tolua_CCDirector_CCDirector_isPaused00);
  8. tolua_function(tolua_S,"getFrames",tolua_CCDirector_CCDirector_getFrames00);
  9. tolua_function(tolua_S,"getWinSize",tolua_CCDirector_CCDirector_getWinSize00);
  10. tolua_function(tolua_S,"getWinSizeInPixels",tolua_CCDirector_CCDirector_getWinSizeInPixels00);
  11. tolua_function(tolua_S,"getDisplaySizeInPixels",tolua_CCDirector_CCDirector_getDisplaySizeInPixels00);
  12. tolua_function(tolua_S,"convertToGL",tolua_CCDirector_CCDirector_convertToGL00);
  13. tolua_function(tolua_S,"convertToUI",tolua_CCDirector_CCDirector_convertToUI00);
  14. tolua_function(tolua_S,"runWithScene",tolua_CCDirector_CCDirector_runWithScene00);
  15. tolua_function(tolua_S,"pushScene",tolua_CCDirector_CCDirector_pushScene00);
  16. tolua_function(tolua_S,"popScene",tolua_CCDirector_CCDirector_popScene00);
  17. tolua_function(tolua_S,"replaceScene",tolua_CCDirector_CCDirector_replaceScene00);
  18. tolua_function(tolua_S,"getContentScaleFactor",tolua_CCDirector_CCDirector_getContentScaleFactor00);
  19. tolua_function(tolua_S,"getDeviceOrientation",tolua_CCDirector_CCDirector_getDeviceOrientation00);
  20. tolua_function(tolua_S,"sharedDirector",tolua_CCDirector_CCDirector_sharedDirector00);

只要把cpp中的对应的代码复制到引擎目录下cocos2d_support文件夹中Luacocos2d.cpp中就搞定了 然后重新编译项目、就可以在lua中使用这个接口了、

关于tolua的使用的更多相关文章

  1. Unity3d:UI面板管理整合进ToLua

    本文基于 https://github.com/chiuan/TTUIFramework https://github.com/jarjin/LuaFramework_UGUI 进行的二次开发,Tha ...

  2. cocos2dx 3.x tolua 分析

    cocos2dx 3.x 版本已经出到3.10了,终于决定要进行引擎版本升级,c++配合lua进行游戏开发,从3.x版本开始cocos使用了新的tolua方式,由于不懂python,折腾tolua搞的 ...

  3. tolua++实现分析

    项目正在使用cocos2dx的lua绑定,绑定的方式是tolua++.对大规模使用lua代码信心不是很足,花了一些时间阅读tolua++的代码,希望对绑定实现的了解,有助于项目对lua代码的把控.从阅 ...

  4. U3D 扩展方法 & Dotween & tolua

    using UnityEngine; using System.Collections; using LuaInterface;//tolua 空间引用 using DG.Tweening;//DOT ...

  5. tolua.cast的实用方法

    local name = (tolua.cast(sender, "ccui.Button")):getTitleText()

  6. cocos2dx 2.x版本:简化提炼tolua++绑定自定义类到lua中使用

    cocos2dx的3.x版本已经提供了更好地绑定方式,网上有很多相关的教程,这里给一个链接:http://www.cocoachina.com/bbs/read.php?tid=196416. 由于目 ...

  7. 学习tolua#·20多个例子

    初始项目搭建 clone官方库 新建unity工程 依次把官方库里的Assets和Unity5.x/Assets拷贝到项目Assets里 打开unity工程, 开始逐个学习例子,例子目录: 1. he ...

  8. 使用tolua++编译pkg,从而创建自定义类让Lua脚本使用

    步骤一:首先自定义类(这里Himi自定义类名 “MySprite”) MySprite.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // //  ...

  9. tolua 有些功能可以用(经过测试)

    tolua 提供几个 C++ 与 Lua 进行数据交换的工具函数. ~~ tolua.type 返回一个 C++ 对象的类型描写叙述字符串. local node = display.newNode( ...

  10. cocos2dx使用tolua关于字符串处理的一个问题

    正在使用cocos2dx的tolua binding在此过程中发现的一个问题.假设一回或输入是std::string当我们不同意包括二进制数据,和std::string我同意,这样一来就导致了不正确的 ...

随机推荐

  1. http服务详解(1)——一次完整的http服务请求处理过程

    前言:要熟练掌握一个服务,首先需要非常了解这个服务的工作过程,这篇就详细解释了http服务的请求处理过程. 一次完整的http请求处理过程 (1)流程图 (2)过程详解 0.DNS域名解析:递归查询. ...

  2. 【网络爬虫入门05】分布式文件存储数据库MongoDB的基本操作与爬虫应用

    [网络爬虫入门05]分布式文件存储数据库MongoDB的基本操作与爬虫应用 广东职业技术学院  欧浩源 1.引言 网络爬虫往往需要将大量的数据存储到数据库中,常用的有MySQL.MongoDB和Red ...

  3. 简述Apache的ab测试主要有那些关键指标

    一.ab的原理 ab是apachebench命令的缩写. ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问.它的测试目标是基于URL的,因此,它既可以用来测试ap ...

  4. 使用脚本监控windows服务的方法

    以下脚本可监控某一个windows服务,发现其停止就立即重启之. @echo off rem 定义循环间隔时间和监测的服务: set secs=60 set srvname="NetWin ...

  5. PhiloGL学习(2)——骚年,让我们荡起双桨

     前言 上一篇文章中简单介绍了PhiloGL框架如何上手.GLSL语言以及简单的绘制一个方块(见PhiloGL学习(1)--场景创建及二维方块加载).本文很简单,我们一起来让这个方块动起来.  一.  ...

  6. IO(Input&Output)流の介绍

    1.导读 对于设备之间的流动(即写入数据或读出数据),Java专门用Java.io包进行操作.这些数据的流动便是我们所说的数据的输入输出流(IO流). 2.数据流的处理:字节流和字符流 数据的基本单位 ...

  7. tp5引入第三方类库

    1.在/public/index.php中添加 define('EXTEND_PATH', '../extend/'); 2./extend/lib 中添加第三方类,类文件的名称和类名一样,命名空间为 ...

  8. LeetCode 476. Number Complement (数的补数)

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  9. 【20171027中】alert(1) to win 第13,14,15,16题

    第13题 题目: function escape(s) { var tag = document.createElement('iframe'); // For this one, you get t ...

  10. lintcode 132 模式

    题目要求 给你一个 n 个整数的序列 a1,a2,...,an,一个 132 模式是对于一个子串 ai,aj,ak,满足 i < j < k 和 ai < ak < aj.设计 ...