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

Cocos绑定Lua自定义类——Mac

原文地址:http://www.cocos.com/doc/tutorial/show?id=2496


从论坛和技术支持的反馈来看,很多童鞋在Cocos中进行lua绑定时遇到了不少问题,因为Cocos使用的是预编译库的方式,所以和之前用Cocos2d-x绑定又不太一样。原有的教程也需要做调整,这篇文章目的是在Cocos中创建一个自定义类,并进行lua绑定,然后在lua中调用C++。

关于如何在Cocos2d-x中进行lua绑定自定义类,请看Binding_Custom_Class_To_Lua_Runtime。本文是基于此教程做的调整。

1. 版本说明

环境: Mac OSX

Cocos: v2.1

Cocos2d-x: v3.4Final

Cocos Code IDE: v1.2.0

Cocos Studio: v2.1.2 beta(这里没用到)

2. 在Cocos中进行lua绑定自定义

2.1 创建项目

首先,创建一个Cocos项目,名字为CocosLuaBinding。

2.2 发布项目

创建后认会使用Cocos Studio打开,选择发布为XCode工程。

2.3 添加自定义

在Xcode中,项目的Classes目录下添加Custom类。

CustomClass.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef__CUSTOM__CLASS
#define__CUSTOM__CLASS
#include"cocos2d.h"
namespace cocos2d{
class CustomClass: public cocos2d::Ref
{
:
CustomClass();
~CustomClass();
bool init();
std::stringhelloMsg();
CREATE_FUNC(CustomClass);
};
} //namespacecocos2d
#endif//__CUSTOM__CLASS

CustomClass.cpp

12
#include"CustomClass.h"
USING_NS_CC;
CustomClass::CustomClass(){
}
CustomClass::~CustomClass(){
}
CustomClass::init(){
return true ;
}
std::stringCustomClass::helloMsg(){
return "HellofromCustomClass::sayHello" ;
}

2.4 添加cocos2dx_custom.ini文件

由于使用的是Cocos Framework,所以创建的cocos2dx_custom.ini路径是 /Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/(安装路径)。需要注意的是headers的路径要指向项目/frameworks/runtime-src/Classes/,这里我用了绝对路径,您需要修改为自己的路径。

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[cocos2dx_custom]
#theprefixtobeaddedtothegeneratedfunctions.Youmightormightnotusethisinyourown
#templates
prefix=cocos2dx_custom
#createatargetnamespace(injavascript,thiswouldcreatesomecodeliketheequiv.to`ns=ns||{}`)
#allclasseswillbeembeddedinthatnamespace
target_namespace=
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-xc++-std=c++11
cocos_headers=-I%(cocosdir)s/cocos-I%(cocosdir)s/my-I%(cocosdir)s/cocos/base-I%(cocosdir)s/cocos/platform/android
cocos_flags=-DANDROID
cxxgenerator_headers=
#extraargumentsforclang
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
#whatheaderstoparse
#headers=%(cocosdir)s/cocos/my/CustomClass.h
headers=/Users/Jacky/Workspace/CocosLuaBinding/frameworks/runtime-src/Classes/CustomClass.h
#whatclassestoproducecodefor.Youcanuseregularexpressionshere.Whentestingtheregular
#expression,itwillbeenclosedin"^$",likethis:"^Menu*$".
classes=CustomClass.*
#whatshouldweskip?intheformatClassName::[functionfunction]
#ClassNameisaregularexpression,butwillbeusedlikethis:"^ClassName$"functionsarealso
#regularexpressions,theywillnotbesurroundedby"^$".Ifyouwanttoskipawholeclass,just
#addasingle"*"asfunctions.Seebellowforseveralexamples.Aspecialclassnameis"*",which
#willapplytoallclassnames.Thisisaconveniencewildcardtobeabletoskipsimilarnamed
#functionsfromallclasses.
skip=
rename_functions=
rename_classes=
#forallclassnames,shouldweremovesomethingwhenregisteringinthetargetVM?
remove_prefix=
#classesforwhichtherewillbeno"parent"lookup
classes_have_no_parents=
#baseclasseswhichwillbeskippedwhentheirsub-classesfoundthem.
base_classes_to_skip=
#classesthatcreatenoconstructor
#Setisspecialandwewilluseahand-writtenconstructor
abstract_classes=
#Determiningwhethertousescriptobject(jsobject)tocontrolthelifecycleofnative(cpp)objectortheotherwayaround.Supportedvaluesare'yes'or'no'.
script_control_cpp=no

2.5 修改genbindings.py

genbindings.py脚本是用来运行绑定的脚本。这里的路径是/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/genbindings.py。

找到cmd_args,加入下面这行:

1
'cocos2dx_custom.ini' :( 'cocos2dx_custom' , 'lua_cocos2dx_custom' ),\

output_dir参数指定了绑定文件输出目录,这里可以更改为Classes,也可以不改。这里就不改了。

cmd_args的其他行是引擎的脚本绑定,如果未做修改,可以删除以避免引擎文件的重复绑定,加快绑定速度。这里也不改了。

2.6 运行绑定脚本

打开终端,进入tolua目录,运行刚才修改的genbindings.py脚本。

2
cd/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua
./genbindings.py

这里不讨论绑定失败的情况,大部分是环境变量没配好的原因,在运行之前请先阅读/Applications/Cocos/frameworks/cocos2d-x-3.4/tools/tolua/README.mdown。

2.7 拷贝绑定文件到项目目录

在成功运行完绑定脚本后,绑定文件生成在/Applications/Cocos/frameworks/cocos2d-x-3.4/cocos/scripting/lua-bindings/auto/lua_cocos2dx_custom.cpp(h)

将这俩个文件剪切到Classes目录,并添加到XCode项目中。

到此为止,基本上是和Binding_Custom_Class_To_Lua_Runtime一致的。

3.编译运行

由于使用的是Cocos Framework,所以引擎没有源码了,只有预编译库,这也就是为什么在第二步中我们主要做的调整:把自定义类及其绑定文件放到Classes中。

3.1 注册自定义

打开Classes/lua_module_register.h文件添加文件

include"lua_cocos2dx_custom.hpp"

在static int lua_module_register(lua_State* L)添加注册函数

register_all_cocos2dx_custom(L);

3.2 关闭COCOS_IDE_DEBUG_SUPPORT

打开Classes/ide-support/CodeIDESupport.h文件修改

defineCC_CODE_IDE_DEBUG_SUPPORT0

3.3 在Cocos IDE中构建自定义模拟器

在Cocos Code IDE中打开项目,选中CocosLuaBinding项目,右键,Cocos工具,构建自定义模拟器,选择相应的模拟器,点击生成

因为Cocos Framework认使用自带的模拟器,所以如果修改了C++文件,仍然使用认模拟器,修改的C++文件不会生效。

构建自定义模拟器后,以后每次都会使用自定义的模拟器。

3.4 如何知道目前使用的是自定义模拟器还是认模拟器?

选中CocosLuaBinding项目,右键,调试方式,调试配置。

以Mac平台为例:

自定义模拟器路径为/Users/Jacky/Workspace/CocosLuaBinding/runtime/mac/CocosLuaBinding Mac

认模拟器路径为/Applications/Cocos/cocos-simulator-bin/mac/Simulator.app

4.测试绑定是否成功

在src/app/views/MainScene.lua的function MainScene:onCreate()中添加如下测试代码

3
localcustomClass=CustomClass:create()
localmsg=customClass:helloMsg()
release_print( "customClass'smsgis:" ..msg)--注意已经没有cclog了

5.搞定,收工~


来源网址:http://www.cocoachina.com/bbs/read.php?tid-287623.html

原文地址:https://www.jb51.cc/cocos2dx/341274.html

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

相关推荐