此篇基本【COCOS2DX(2.X)_LUA开发之三】在LUA中使用自定义精灵(LUA脚本与自创建类之间的访问)及LUA基础讲解

在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类供Lua脚本调用使用,当时出于Himi对Lua研究不够深入,所以当时使用了笨方法手动添加的方式进行的,那么本篇将介绍利用tolua++快速将我们自定义的c2dx类嵌入,供 lua脚本使用。

首先介绍整个过程:

之前我们的过程: 自定义类->手动到LuaCoco2d.cpp中手动添加binding->lua使用

现在我们的过程是: 自定义类->使用tolua++工具编译到LuaCoco2d.cpp中->lua使用

下面进行详细步骤讲解:

步骤一:首先自定义类(这里Himi自定义类名 “MySprite”)

MySprite.h


  1. //
  2. //  MySprite.h
  3. //  mtet
  4. //
  5. //  Created by Himi on 13-4-7.
  6. //
  7. //
  8. #ifndef __mtet__MySprite__
  9. #define __mtet__MySprite__
  10. #include "cocos2d.h"
  11. using namespace cocos2d;
  12. class MySprite : public CCSprite{
  13. public:
  14. static MySprite* createMS(const char* fileName);
  15. };
  16. #endif /* defined(__mtet__MySprite__) */
 

MySprite.cpp


  1. 2
  2. 3
  3. 4
  4. 5
  5. 6
  6. 7
  7. 8
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. //
  20. //  MySprite.cpp
  21. //  mtet
  22. //
  23. //  Created by Himi on 13-4-7.
  24. //
  25. //
  26. #include "MySprite.h"
  27. MySprite* MySprite::createMS(const char* fileName){
  28. MySprite* sp = new MySprite();
  29. if(sp && sp->initWithFile(fileName)){
  30. sp->setPosition(ccp(100,100));
  31. sp->autorelease();
  32. return sp;
  33. }
  34. CC_SAFE_DELETE(sp);
  35. return NULL;
  36. }
 

步骤二:利用tolua++编译我们创建的pkg,将自定义类嵌入LuaCocos2d.cpp中

首先我们到cocos2dx引擎目录下找到tools下的tolua++文件夹。

然后你看到很多的pkg文件,你可以使用文本打开,就会发现都是Cocos2dx引擎封装的类、函数定义,如下CCSprite.pkg

 

  1. /*
  2. typedef enum {
  3. //! Translate with it's parent
  4. CC_HONOR_PARENT_TRANSFORM_TRANSLATE =  1 << 0,
  5. //! Rotate with it's parent
  6. CC_HONOR_PARENT_TRANSFORM_ROTATE    =  1 << 1,
  7. //! Scale with it's parent
  8. CC_HONOR_PARENT_TRANSFORM_SCALE     =  1 << 2,
  9. //! Skew with it's parent
  10. CC_HONOR_PARENT_TRANSFORM_SKEW      =  1 << 3,
  11. //! All possible transformation enabled. Default value.
  12. CC_HONOR_PARENT_TRANSFORM_ALL       =  CC_HONOR_PARENT_TRANSFORM_TRANSLATE | CC_HONOR_PARENT_TRANSFORM_ROTATE | CC_HONOR_PARENT_TRANSFORM_SCALE | CC_HONOR_PARENT_TRANSFORM_SKEW,
  13. } ccHonorParentTransform;
  14. */
  15. class CCSprite : public CCNode
  16. {
  17. void setDirty(bool bDirty);
  18. bool isDirty(void);
  19. ccV3F_C4B_T2F_Quad getQuad(void);
  20. CCRect getTextureRect(void);
  21. //bool isUsesBatchNode(void);
  22. bool isTextureRectRotated(void);
  23. void setAtlasIndex(unsigned int uAtlasIndex);
  24. unsigned int getAtlasIndex(void);
  25. //void setUsesSpriteBatchNode(bool bUsesSpriteBatchNode);
  26. void setTextureAtlas(CCTextureAtlas *pobTextureAtlas);
  27. CCTextureAtlas* getTextureAtlas(void);
  28. //void setSpriteBatchNode(CCSpriteBatchNode *pobSpriteBatchNode);
  29. //CCSpriteBatchNode* getSpriteBatchNode(void);
  30. //void setHonorParentTransform(ccHonorParentTransform eHonorParentTransform);
  31. //ccHonorParentTransform getHonorParentTransform(void);
  32. void setBlendFunc(ccBlendFunc blendFunc);
  33. ccBlendFunc getBlendFunc(void);
  34. CCPoint getOffsetPosition(void);
  35. void ignoreAnchorPointForPosition(bool newValue);
  36. void setFlipX(bool bFlipX);
  37. void setFlipY(bool bFlipY);
  38. bool isFlipX(void);
  39. bool isFlipY(void);
  40. void removeChild(CCNode* pChild, bool bCleanUp);
  41. void removeAllChildrenWithCleanup(bool bCleanup);
  42. void reorderChild(CCNode* pChild, int zOrder);
  43. void addChild(CCNode* pChild);
  44. void addChild(CCNode* pChild, int zOrder);
  45. void addChild(CCNode* pChild, int zOrder, int tag);
  46. void sortAllChildren();
  47. //void setPosition(CCPoint pos);
  48. void setRotation(float rotation);
  49. void setSkewX(float sx);
  50. void setSkewY(float sy);
  51. void setScale(float fScale);
  52. void setScaleX(float fScaleX);
  53. void setScaleY(float fScaleY);
  54. void setVertexZ(float fVertexZ);
  55. void setAnchorPoint(const CCPoint & anchor);
  56. void setVisible(bool bVisible);
  57. void setOpacity(GLubyte opacity);
  58. GLubyte getOpacity(void);
  59. void setColor(ccColor3B color3);
  60. ccColor3B getColor(void);
  61. void setOpacityModifyRGB(bool bValue);
  62. bool isOpacityModifyRGB(void);
  63. void setTexture(CCTexture2D *texture);
  64. CCTexture2D* getTexture(void);
  65. void updateTransform(void);
  66. //void useSelfRender(void);
  67. void setTextureRect(CCRect rect);
  68. void setTextureRect(CCRect rect, bool rotated, CCSize size);
  69. void setVertexRect(CCRect rect);
  70. //void useBatchNode(CCSpriteBatchNode *batchNode);
  71. void setDisplayFrame(CCSpriteFrame *pNewFrame);
  72. bool isFrameDisplayed(CCSpriteFrame *pFrame);
  73. CCSpriteFrame* displayFrame(void);
  74. void setBatchNode(CCSpriteBatchNode* pBatchNode);
  75. CCSpriteBatchNode* getBatchNode();
  76. void setDisplayFrameWithAnimationName(const char *animationName, int frameIndex);
  77. static CCSprite* createWithTexture(CCTexture2D *pTexture);
  78. static CCSprite* createWithTexture(CCTexture2D *pTexture, CCRect rect);
  79. static CCSprite* createWithSpriteFrame(CCSpriteFrame *pSpriteFrame);
  80. static CCSprite* createWithSpriteFrameName(const char *pszSpriteFrameName);
  81. static CCSprite* create(const char *pszFileName, CCRect rect);
  82. static CCSprite* create(const char *pszFileName);
  83. static CCSprite* create();
  84. };

没错,我们也会按照类似方式进行创建我们自定义类的pkg文件。

我们自定义一个文件(文本、xcode等都可以),后缀 .pkg ,然后将Himi自定义的MySprite类定义到pkg中,如下:

注意:只要自定义类.h中的内容,至于cpp的实现,binding后lua自动调用你类的函数

MySprite.pkg


  1. class MySprite : public CCSprite{
  2. static MySprite* createMS(const char* fileName);
  3. };
 

在pkg中我只是定义了创建函数而已,至于更多的函数就交给大家自定义啦,另外我们注意书写pkg时是需要几条规则的,其实在tolua++这个文件夹中我们也能看到有一个名字叫 README 的文件,打开如下:


  1. 1. Generating the lua<-->C bindings with tolua++
  2. Build scripts for windows (build.bat) and unix (build.sh) are provided
  3. to generate the relevant files after modifying the .pkg files.  These
  4. scripts basically run the following command:
  5. tolua++.exe -L basic.lua -o LuaCocos2d.cpp Cocos2d.pkg
  6. This will generate the bindings file and patch it with come cocos2dx
  7. specific modifications.
  8. On POSIX systems you can also just run "make" to build the bindings
  9. if/when you change .pkg files.
  10. 2. Writing .pkg files
  11. 1) enum keeps the same
  12. 2) remove CC_DLL for the class defines, pay attention to multi inherites
  13. 3) remove inline keyword for declaration and implementation
  14. 4) remove public protect and private
  15. 5) remove the decalration of class member variable
  16. 6) keep static keyword
  17. 7) remove memeber functions that declared as private or protected
 

这个文件声明了书写pkg的规则,不多赘述。

书写好我们的pkg之后,将pkg文件放置此tolua++文件夹下即可,然后配置我们tolua++工具。

继续在tolua++文件夹中解压tolua++.Mac.zip 文件,会得到一个tolua++的工具,如下图:

解压出工具之后,我们还要在tolua++文件夹中,配置tolua++路径,打开“build.sh”文件,如下:

这里 TOLUA 是tolua++工具的位置(路径后面要架上 /tolua++  表示这个工具),最下面配置的是编译后的luaCocos2d.cpp文件导出的位置,Himi这里配置到桌面,配置如下:

最后,我们要将我们定义的pkg文件注册到 tolua++文件夹下的Cocos2d.pkg中,如下:

如上步骤都OK后,我们就可以使用“终端”,先cd到tolua++的文件夹下,然后使用“make”命令执行tolua++工具。

(如果这里终端不能正常执行, 请继续修改tolua++文件夹下的: makefile  ,将其路径配置一下即可。)

终端正常执行后,会在一开始指定的目录生成LuaCocos2d.cpp 文件,且其中已经binding好了自定义类,将生成的LuaCocos2d.cpp替换到你项目的/libs/lua/cocos2dx_support下的LuaCocos2d.cpp 文件。

Himi建议生成的LuaCocos2d.cpp 文件路径直接设置你的项目的/libs/lua/cocos2dx_support下很方便

注意:这时候LuaCoco2d.cpp中虽然已经binding了我们的自定义类,但是没有引用我们的头文件,所以我们还需要在LuaCocos2d.h中倒入我们自定义类.h 。

步骤三:Lua测试我们的自定义类


  1. -- for CCLuaEngine traceback
  2. function __G__TRACKBACK__(msg)
  3. print("----------------------------------------")
  4. print("LUA ERROR: " .. tostring(msg) .. "\n")
  5. print(debug.traceback())
  6. print("----------------------------------------")
  7. end
  8. local function main()
  9. -- avoid memory leak
  10. collectgarbage("setpause", 100)
  11. collectgarbage("setstepmul", 5000)
  12. local cclog = function(...)
  13. print(string.format(...))
  14. end
  15. require "hello2"
  16. cclog("result is " .. myadd(3, 5))
  17. ---------------
  18. -- create farm
  19. local function createLayerFarm()
  20. local layerFarm = CCLayer:create()
  21. local font = CCLabelTTF:create("Himi 使用tolua++ binding自定义类", "Verdana-BoldItalic", 20)
  22. font:setPosition(ccp(220,260))
  23. layerFarm:addChild(font)
  24. local ms  = MySprite:createMS("Icon.png")
  25. layerFarm:addChild(ms)
  26. return layerFarm
  27. end
  28. -- run
  29. local sceneGame = CCScene:create()
  30. sceneGame:addChild(createLayerFarm())
  31. CCDirector:sharedDirector():runWithScene(sceneGame)
  32. end
  33. xpcall(main, __G__TRACKBACK__)
 




本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/1173220,如需转载请自行联系原作者

【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pk创建自定义类的更多相关文章

  1. cocos2d-x lua脚本开发 1

    自从开始关注OpenResty之后,逐渐关注Lua语言,发现这个语言真真是容易让人喜爱的语言.偶然间发现了cocos2d-x,还支持lua,所以果断尝试一下. 这里是在cocos2d-x官方网站下载了 ...

  2. 使用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 // //  ...

  3. 【COCOS2DX-LUA 脚本开发之四】

    使用tolua++编译pkg,从而创建自定义类让Lua脚本使用 本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi ) 转载自[黑米GameDev街区] 原文链接: ...

  4. Cocos2d-x教程(26)-Cocos2d-x + Lua脚本实现大地图缩放功能

    欢迎增加 Cocos2d-x 交流群: 193411763 视频教程地址:http://www.tudou.com/programs/view/qRiOfppMghM/ 转载请注明原文出处:http: ...

  5. Knockout应用开发指南 第五章:创建自定义绑定

    原文:Knockout应用开发指南 第五章:创建自定义绑定 创建自定义绑定 你可以创建自己的自定义绑定 – 没有必要非要使用内嵌的绑定(像click,value等).你可以你封装复杂的逻辑或行为,自定 ...

  6. 第二十三篇:在SOUI中使用LUA脚本开发界面

    像写网页一样做客户端界面可能是很多客户端开发的理想. 做好一个可以实现和用户交互的动态网页应该包含两个部分:使用html做网页的布局,使用脚本如vbscript,javascript做用户交互的逻辑. ...

  7. Cocos2d-x Lua游戏开发Mac环境搭建以及一点点感悟

    接触Cocos2d-x 最近由于公司项目的需要,自己开始接触Cocos,开始做一些简单的轻量级的游戏,以前没有接触过这一块的东西,也是借助这个机会学习一下游戏的开发,由于以前自己接触的全都是iOS和A ...

  8. Redis进阶之使用Lua脚本开发

    1.在Redis中使用Lua 在Redis中执行Lua脚本有两种方法:eval和evalsha. (1)eval eval 脚本内容 key个数 key列表 参数列表 下面例子使用了key列表和参数列 ...

  9. Cocos2d-x lua游戏开发之安装Lua到mac系统

    注意:mac ox .lua version :5.15 下载lua官网的lua, 注意:最好是5.15下面.5.2的lua不支持table的getn()方法,这让我情何以堪.(获取table长度.相 ...

随机推荐

  1. 电商平台--Mysql主从搭建(2)

    Master上授权从库: ```grant replication slave on *.* to slave1@ip identified by 'password';``` 基于数据库hotcop ...

  2. SpringMVC框架详细教程(六)_HelloWorld

    HelloWorld 在src下创建包com.pudding.controller,然后创建一个类HelloWorldController: package com.pudding.controlle ...

  3. Visual C++ 6.0踩坑记录---在Win10下安装Visual C++ 6.0安装成功后点击“打开”按钮闪退问题

    前言: 为了更好的学习C及C++,前段时间下载了Microsoft Visual C++ 6.0(以下简称VC6),原因是VC6具有查看反汇编代码.监视内存.寄存器等功能,并且因为本人正在学习滴水逆向 ...

  4. 怎么高效学习python?其实只需要这个方法,快速掌握不叫事儿

    很多人想学python,并且希望能快速高效的学习python,但一直都没有找到合适的方法,下面谈一下我的方法. 首先,高效入门python 怎么高效学习python?想要高效,就要先搞清楚你这个阶段, ...

  5. WinForm:API

    一.WndProc:  主要用在拦截并处理系统消息和自定义消息 比如:windows程序会产生很多消息,比如你单击鼠标,移动窗口都会产生消息.这个函数就是默认的消息处理函数.你可以重载这个函数来制定自 ...

  6. work of 1/5/2016

    part 组员                今日工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云 UI页面切换,词本显示下滑条     6 继续下滑条等增删补 ...

  7. Connections in Galaxy War ZOJ - 3261 (并查集)

    点权并查集的反向离线操作 题目大意:有n个stars,每一个都一定的“颜值”.然后stars与stars之间可以相连,query c表示再与c相连的stars中,颜值比c高的,stars的标号,如果有 ...

  8. 智能可视化搭建系统 Atom 服务架构演变

    作者:凹凸曼 - Manjiz Atom 是什么?Atom 是集结业内各色资深电商行业设计师,提供一站式专业智能页面和小程序设计服务的平台.经过 2 年紧凑迭代,项目越来越庞大,需求不断变更优化,内部 ...

  9. Java多台中成员访问特点

    多态中的成员访问特点: A:成员变量 编译看左边,运行看左边 B:构造方法 创建子类对象的时候,访问父类的构造方法,对父类的数据进行初始化 C:成员方法 编译看左边,运行看右边.//因为调用对象时,子 ...

  10. php用户量剧增导致cpu100%解决办法

    在php扩展里边开启opcache扩展,此扩展是解析php的缓存机制,每次解析都要消耗cpu,所以有大量的fpm进程去占用cpu,开启此扩展之后cpu就瞬间下来了,只解析第一次,往后的都使用缓存.很好 ...