cocos2dx新创建项目androID部分却少build_native.py 文件需要用cocos 命令行进行编译:
基础配置部分: (for mac)
/cocos2d-x-3.x/tools/cocos2d-console/bin下 (ccoos2dx 3.12)
1. chmod u+x cocos.py
2. ./cocos.py new HelloWorldDemo -p com.coco2dx.org -l cpp -d ~/Desktop (创建项目)
3. 先要cd进入proj.androID目录 cocos compile -p androID (deBUG 编译)
4. 先要cd进入proj.androID目录 cocos compile -p androID -m release (release 编译)
5. 下载macport 软件下载:http://www.ccvita.com/434.html(找到对应的 *** 作系统版本)
官网(https://www.macports.org/install.PHP)
- 环境变量设置:
修改.bash_profile 文件 添加:
export PATH=/opt/local/bin:$PATH
export PATH=/opt/local/sbin:$PATH
source .bash_profile (修改环境变量)
- *** 作指南:
sudo port -v selfupdate (mac ports更新)
port search name (搜索软件)
sudo port install name (安装软件)
sudo port uninstall name (卸载软件)
port outdated (查看软件版本)
sudo port upgrade outdated (升级软件)
- mac port 安卓:
sudo port -v selfupdate
sudo port install python27 py27-yaml py27-cheetah
6. 下来Jsbinding一次你就知道有多坑,Js版本必须要把原工程的tools拷贝过来(这里可以直接使用cocos2dx引擎下的tools文件夹即可),否则是不可以 pythongenbindings.py 无法使用的,但是lua比较正常;
7. 进入当前生成项目下的tools 中的 toJs文件加,复制一下cocos2dx_experimental_webVIEw.ini进行修改 *** 作,重命名TestJsB.ini;
- 修改项:
[TestJsB]
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = TestJsB
# 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 = my
macro_judgement = #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS) && !defined(CC_TARGET_OS_TVOS)
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/%(clang_version)s/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/../runtime-src/Classes/TestJsB.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 = TestJsB
# 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.
skip =
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 =
# 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
8. 添加c++ 原文件与头文件:
- 文件名称:
TestJsB.h
TestJsB.cpp
- 代码:
//TestJsB.h
// TestJsB.hpp
// TestCocosJs
//
// Created by kaitiren on 16/8/4.
//
//
#ifndef TestJsB_hpp
#define TestJsB_hpp
#include <stdio.h>
#include "cocos2d.h"
using namespace cocos2d;
class TestJsB : public cocos2d::Layer
{
public:
TestJsB();
~TestJsB();
voID testprint(voID);
voID testString(const char* _str);
voID testInt(int value);
voID testLong(long value);
voID testBool(bool isBoo);
CREATE_FUNC(TestJsB);
};
#endif /* TestJsB_h */
///TestJsB.cpp
//
// TestJsB.cpp
// TestCocosJs
//
// Created by kaitiren on 16/8/4.
//
#include "TestJsB.h"
TestJsB:: TestJsB()
{
printf("\n TestJsB:: TestJsB() \n");
}
TestJsB:: ~TestJsB()
{
}
voID TestJsB:: testprint(voID)
{
cclOG("TestJsB:: testprint(voID)");
printf("\n TestJsB:: testprint(voID) \n");
}
voID TestJsB:: testString(const char* _str)
{
printf("\n TestJsB:: testString(_str): %s \n",_str);
}
voID TestJsB:: testInt(int value)
{
printf("\n TestJsB:: testInt: %d \n ",value);
}
voID TestJsB:: testLong(long value)
{
printf("\n TestJsB:: testLong: %ld \n ",value);
}
voID TestJsB:: testBool(bool isBoo)
{
printf("\n TestJsB:: testBool: %d \n",isBoo);
}
/// cpp
9. 编译pythongenbindings.py 文件,查看错误部分,并且修改,完成后进行添加到xcode工程(注意: 在cocos2d_Js_bindings.xcodeproj项目下的auto 文件加进行添加,这里有一个坑是必须要添加到ios项目内,否则识别不到);
坑: 如果cpp文件头文件搜索不到自行添加一下即可;
注意导入头文件部分:#include"../../../../../runtime-src/Classes/TestJsB.h"
修改genbindings.py文件:
cmd_args = {
#'cocos2dx.ini': ('cocos2d-x','Jsb_cocos2dx_auto'),
#'cocos2dx_audioengine.ini': ('cocos2dx_audioengine','Jsb_cocos2dx_audioengine_auto'),
#'cocos2dx_extension.ini': ('cocos2dx_extension','Jsb_cocos2dx_extension_auto'),
#'cocos2dx_builder.ini': ('cocos2dx_builder','Jsb_cocos2dx_builder_auto'),
#'cocos2dx_ui.ini': ('cocos2dx_ui','Jsb_cocos2dx_ui_auto'),
#'cocos2dx_studio.ini': ('cocos2dx_studio','Jsb_cocos2dx_studio_auto'),
#'cocos2dx_spine.ini': ('cocos2dx_spine','Jsb_cocos2dx_spine_auto'),
#'cocos2dx_3d.ini': ('cocos2dx_3d','Jsb_cocos2dx_3d_auto'),
#'cocos2dx_3d_ext.ini': ('cocos2dx_3d_extension','Jsb_cocos2dx_3d_extension_auto'),
#'cocos2dx_experimental_webVIEw.ini': ('cocos2dx_experimental_webVIEw','Jsb_cocos2dx_experimental_webVIEw_auto'),
#'cocos2dx_experimental_vIDeo.ini': ('cocos2dx_experimental_vIDeo','Jsb_cocos2dx_experimental_vIDeo_auto'),
#'cocos2dx_physics3d.ini': ('cocos2dx_physics3d','Jsb_cocos2dx_physics3d_auto'),
'cocos2dx_navmesh.ini': ('cocos2dx_navmesh','Jsb_cocos2dx_navmesh_auto'),
#################################################
'TestJsB.ini': ('TestJsB','Jsb_testJsb_auto'),
'JsBindingIOSWXManager.ini': ('JsBindingIOSWXManager','Jsbinding_ioswxmanager_auto'),#添加部分
}
10. 进入当前项目的Classes中的AppDelegate.cpp:
- 修改代码:
添加:"scripting/Js-bindings/auto/Jsb_testJsb_auto.hpp"
注册代码: sc->addRegisterCallback(register_all_TestJsB); 这里可以从Jsb_testJsb_auto.hpp 中查找;
完成后进行xcode build *** 作;
11. 进行Js代码编辑与测试: (进入 app.Js HelloWorldScene)
- 添加测试代码
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
///
console.log("aaaaaaaassssssss");
///
var testJsB = new my.TestJsB();
testJsB.testprint();
testJsB.testString("pppppppppppppppppppppppp");
testJsB.testInt(123);
testJsB.testLong(999999999999);
testJsB.testBool(true);
}
});
控制台输出如果正常Js 调用 c++ 部分结束;
12. 解决c++ 调用 Js 部分: (进入Jsb_testJsb_auto.cpp文件夹)
c++ 模块:
bool Js_TestJsB_TestJsB_testString(jscontext *cx,uint32_t argc,Jsval *vp)
{
Js::CallArgs args = Js::CallArgsFromVp(argc,vp);
bool ok = true;
Js::Rootedobject obj(cx,args.thisv().toObjectOrNull());
Js_proxy_t *proxy = Jsb_get_Js_proxy(obj);
TestJsB* cobj = (TestJsB *)(proxy ? proxy->ptr : NulL);
JsB_PRECONDITION2( cobj,cx,false,"Js_TestJsB_TestJsB_testString : InvalID Native Object");
if (argc == 1) {
const char* arg0 = nullptr;
std::string arg0_tmp; ok &= Jsval_to_std_string(cx,args.get(0),&arg0_tmp); arg0 = arg0_tmp.c_str();
JsB_PRECONDITION2(ok,"Js_TestJsB_TestJsB_testString : Error processing arguments");
cobj->testString(arg0);
args.rval().setUndefined();
/// c++ 调用 Js 部分
jscontext* jc = Scriptingcore::getInstance()->getGlobalContext();
// Jsval ret;
Scriptingcore::getInstance()->evalString( "cpp_callback(5,100)" ); // Js 全局方法
/// c++ 调用 Js 部分
/// Scriptingcore::getInstance()->executeFunctionWithOwner(<#Jsval owner#>,<#const char *name#>,<#uint32_t argc#>,<#Jsval *vp#>) // 此方法可调用全局文件
return true;
}
Js_ReportError(cx,"Js_TestJsB_TestJsB_testString : wrong number of arguments: %d,was expecting %d",argc,1);
return false;
}
Js 模块:
function cpp_callback(a,b) { cc.log("cpp return two integer: " + a + " " b "ssssssssssssssssssssssssssssssssss");}
模拟器与移动端运行正常打印;
13. 区分native与web:
if(cc.sys.isNative){ "platform is native !!!"); var testJsB = new my.TestJsB(); testJsB.testprint(); testString("pppppppppppppppppppppppp"); testInt(123); testLong(999999999999); testBool(true);}else{ "sys.platform is browser !!!");}
Js binding 部分完结;
14. androID版本编译部分;
编译androID到当前项目下的proj.androID中
控制台执行:
cocos compile -p androID
如果有Jsb的部分要修改androID.mk两处:
- 第一处是在当前项目下runtime-src中的proj.androID,导入classes文件夹下的cpp文件到androID.mk中;
- 第二处是在/cocos2d-x/cocos/scripting/Js-bindings/proj.androID/AndroID.mk进行binding.cpp文件添加;
完成后接续ndk编译尝试;
15. 疑难错误解答:
error: Js: assets/src/app.Js:36:ReferenceError: my is not defined
查看AppDelegate.cpp是否没有注册要Jsb的cpp函数;
如: sc->addRegisterCallback(register_all_JsBindingIOSWXManager);
error: cpp调用Js方法传参问题: (这里可以放到任何一个cpp内:)
#include"scripting/Js-bindings/manual/cocos2d_specifics.hpp"
"JsAPI.h"
"JsfrIEndAPI.h"
boolTSScriptJs::RunFunction(std::stringfunname,std::stringarg) { std::stringrStr=funname+"(\""+arg+"\");"; // 注意这里的括号部分 Jsvalv1; JsBoolr=Scriptingcore::getInstance()->evalString(rStr.c_str(),&v1); returnr;
}
例子: c++:
JsC 进行cocos Js代码加密:
ter: cocos Jscompile -s /Users/fox/documents/Js -d /Users/fox/documents/Jsc
-s:Js文件路径
-d:Jsc输出路径
拷贝项目内的Js原文件夹:
Js文件路径:
/Users/xxx/xxx/ClIEntC2DJs/src
/Users/xxx/xxx/ClIEntC2DJs/frameworks/cocos2d-x/cocos/scripting/Js-bindings/script
执行代码:
cocos Jscompile -s /Users/fox/documents/Js -d /Users/fox/documents/Jsc
完成后替换项目中的两个文件夹: ...cocos2d/Jsbinding/script 不要替换Jsbinding中的auto文件夹,...逻辑部分src 但是不要替换main;
完成后运行测试;
总结以上是内存溢出为你收集整理的cocos2d-js javascriptbinding实现全部内容,希望文章能够帮你解决cocos2d-js javascriptbinding实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)