
2021-05-06
发布日期:2021-05-10 01:23:33
浏览次数:25
分类:精选文章
本文共 1947 字,大约阅读时间需要 6 分钟。
To call C++ functions from JavaScript or TypeScript in CocosCreator, follow these organized steps:
Set Up CocosCreator:
- Ensure you have CocosCreator version 2.4.5 installed. This version supports the necessary features for JS bindings.
Generate Bindings:
- Open the Cocos project in Android Studio.
- Navigate to the project's app module and add a new C++ module using the NDK.
- Place your C++ implementation in the src/cpp directory.
Modify the C++ Files:
- Update the cpp folder to include your C++ functions and override methods like
onKeyDown
for keystats. - Compile the C++ code in Android Studio to ensure it's ready for integration.
Generate Bindings via genbindings.py:
- Access the Cocos directory in the CocosCreator project, specifically in cocos/scripting/js-bindings/auto.
- Run
genbindings.py
via the command line to generate the bindings. - Verify success by checking if new files appear in the bindings directory with the current date.
Include Bindings in Android Project:
- Copy the generated .cpp files to jni/CppBindings.cpp in your project's jni directory.
- In Android.mk, include this file and compile the C++ module into a static library.
Use Bindings in JavaScript:
- Reference your C++ functions in your JavaScript files using the create method.
- Example: Use
cc\ModuleLoader.get("ModuleName").get FUNC_NAME
and call it with parameters if needed.
Test and Debug:
- Run your Android app and test if the C++ functions respond to JavaScript calls.
- Check console logs for any binding-related errors, ensuring you've correctly imported and compiled the C++ module.
Advanced Integration:
- For more complex scenarios, consider using native interfaces or object wrappers if needed.
- Consult the official CocosCreator documentation for best practices and further guidance.
By following these steps, you can successfully integrate and call C++ functions within your CocosCreator project, enabling seamless communication between JavaScript and C++ components.
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月24日 01时23分29秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Wordpress主题Git后台清净模式设置
2019-03-07
JQuery获取元素的方法总结
2019-03-07
张一鸣:创业7年,我经历的5件事
2019-03-07
SQL基础语法
2019-03-07
SQL 已死,但 SQL 将永存
2019-03-07
码农也能有春天:一个人独立运营网站12年,赚到了5亿多美元!
2019-03-07
Python3 日期和时间
2019-03-07
JavaScript实现表格排序
2019-03-07
vue散碎知识点学习
2019-03-07
Flask连接Mysql安装及设置操作
2019-03-07
周报二
2019-03-07
周报十一
2019-03-07
发送短信(js小案例)
2019-03-07
自定义vue模板(name默认是文件名)
2019-03-07
导出 下载excel 方案(点击下载,下载一个excel模板)
2019-03-07
git拉取远程指定分支代码
2019-03-07
C语言--C语言总结大纲
2019-03-07
轻松理解前后端分离(通俗易懂)
2019-03-07
蚂蚁金服(Java研发岗),26岁小伙斩获三面,收获Offer定级P6
2019-03-07
JavaFX官方文档
2019-03-07