微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2d-x 之quick-ini

最近要学习在quick上用lua编写游戏了,在学习之前,很想知道lua与c++之间的关系,于是找到了tolua++,tolua++是一种第三方的软件包,可以为Lua提供面向对象的特性,这样我们就可以使用Lua来编写使用C++语言库的脚本文件。在cocos中,bindings-generator脚本代替了toLua++。更快的将C++类注册进Lua环境。 
 

首先在quick 3.5下新建一个项目,cd进入工具-bin目录下,输入:cocos new NB -l lua -d ./Projects。这样就新建了项目NB。进入:D:\NB\frameworks\cocos2d-x\tools\tolua 目录你会看见很多ini文件,首先打开README.mdown。这里面有环境的配置,为了方便以后的记忆,我还是写出来了。我是在win7下建的环境。

第一步:确定电脑中有`android-ndk-r9b`.

第二步:Download python2.7.3 (32bit) from (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi).

第三步:Add the installed path of python (e.g. C:\Python27) to windows environment variable named 'PATH'.

第四步:Download pyyaml from http://pyyaml.org/download/pyyaml/PyYAML-3.10.win32-py2.7.exe and install it.(这个很重要,不然会执行python时报错,什么yaml的错)

第五步:Download pyCheetah from https://raw.github.com/dumganhar/my_old_cocos2d-x_backup/download/downloads/Cheetah.zip,unzip it to "C:\Python27\Lib\site-packages"

第六步:Set environment variables (`NDK_ROOT`)

第七步:run "genbindings.py".

环境配好后,在D:\NB\frameworks\cocos2d-x\tools\tolua 目录下,看见genbindings.py,可以在里面改,也可以重新复制一份出来改。我是复制一份出来改的。

1、先说ini,任意打开一份ini,

[cocos2dx_spine]  (ini文件名)
<span style="color: rgb(85,85,85); font-family: 'Microsoft Yahei',sans-serif; font-size: 15px; line-height: 26px; text-indent: 30px;">#prefix会被添加生成函数.你也可以选择不添加这个到你的模板</span>
prefix = cocos2dx_spine 

# create a target namespace (in javascript,this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace (命名空间)
target_namespace = sp

android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_ 

clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include 
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__

cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android

cocos_flags = -DANDROID

cxxgenerator_headers = 

# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s 

# what headers to parse (类所在路劲)
headers = %(cocosdir)s/cocos/editor-support/spine/spine-cocos2dx.h

# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression,it will be enclosed in "^$",like this: "^Menu*$".
classes = SkeletonRenderer SkeletonAnimation (需要注册的类,有多个需要用空格空开)

# what should we skip? in the format ClassName::[function function]
# ClassName is a regular expression,but will be used like this: "^ClassName$" functions are also
# regular expressions,they will not be surrounded by "^$". If you want to skip a whole class,just
# add a single "*" as functions. See bellow for several examples. A special class name is "*",which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.
(在lua中不能访问的函数名,没有,=后面就不写)
skip = SkeletonRenderer::[findBone findSlot getAttachment setAttachment update draw createWithData],*::[update draw drawSkeleton],SkeletonAnimation::[setAnimationStateData createWithData addAnimation getCurrent setAnimation onAnimationStateEvent onTrackEntryEvent getState createWithFile]
(下面全空的认OK)
rename_functions = 

rename_classes = 

# for all class names,should we remove something when registering in the target VM?
remove_prefix = 

# classes for which there will be no "parent" lookup
classes_have_no_parents = 

# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip =

# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes = Skeleton SkeletonAnimation

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no

2、在D:\NB\frameworks\runtime-src\Classes目录下编写自己的类,也可以在其他目录下。

(这个是引用:http://segmentfault.com/a/1190000000631630)

MyClass.h

#pragma once
#include "cocos2d.h"

using namespace cocos2d;

class MyClass : public Ref
{
public:
  MyClass()   {};
  ~MyClass()  {};
  bool init() { return true; };
  CREATE_FUNC(MyClass);

  int foo(int i);
};
MyClass.cpp

#include "MyClass.h"

int MyClass::foo(int i)
{
  return i + 100;
}

3、MyClass.ini

[MyClass] (改)
prefix = MyClass (改)
target_namespace = my (改)
android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_ 
clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include 
clang_flags = -nostdinc -x c++ -std=c++11 -U __SSE__
cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/cocos/editor-support -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID
cxxgenerator_headers = 
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s 
headers = %(cocosdir)s/../runtime-src/Classes/MyClass.h (改,这是自己类路劲,可以自己改)
classes = MyClass (改)
skip =  (改)
rename_functions = 
rename_classes =
remove_prefix = 
classes_have_no_parents = 
base_classes_to_skip = Ref ProcessBase
abstract_classes = 
script_control_cpp = no
4、复制一份genbindings.py为genbindings_MyClass。类容如下:

output_dir = '%s/cocos/scripting/lua-bindings/auto' % project_root (这个路径是生成lua_MyClass_auto 的路径,可以自己改
cmd_args = { 'MyClass.ini' : ('MyClass','lua_MyClass_auto')} 只生成MyClass的注册类。

5、在D:\NB\frameworks\cocos2d-x\tools\tolua目录下,执行python genbindings_myclass.py。没问题的话,你会在output_dir的路径下看见

lua_MyClass_auto.cpp 和 lua_MyClass_auto.hpp 两个文件

在D:\NB\frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto\api目录下看见MyClass.lua
6、在D:\NB\frameworks\runtime-src\Classes\AppDelegate.cpp中加入:#include "lua_MyClass_auto.hpp"

LuaEngine* engine = LuaEngine::getInstance();后面加入:register_all_myclass(engine->getLuaStack()->getLuaState());

7、编译运行,这样,MyClass这个类就被导出到lua了

8、在main.lua中尝试:

local test = my.MyClass:create()
print("lua bind: " .. test:foo(99))

(仅个人理解,如有不对,还请指正)

参考链接:http://www.cocoachina.com/bbs/read.PHP?tid-196416.html

http://segmentfault.com/a/1190000000631630

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


    本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991.  软件准备 下载地址:http://cn.cocos2d-x.org/download 2.  简介2.1         引用C
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download  v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发引擎,易学易用,支持多种智能移动平台。官网地址:http://cocos2d-x.org/当前版本:2.0    有很多的学习资料,在这里我只做为自己的笔记记录下来,错误之处还请指出。在VisualStudio2008平台的编译:1.下载当前稳
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
 下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net前言上次用象棋演示了cocos2dx的基本用法,但是对cocos2dx并没有作深入的讨论,这次以超级马里奥的源代码为线索,我们一起来学习超级马里奥的实
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建