Cocos2DX-JS and Swift projects

This article is a follow-up to the previous article on configuring swift-cocos2dx-js. If you are unclear, please check the previous article to teach you how to integrate the cocos2dx-js module in a swift project. The specific code is also here;

Create a scene with CocosCreator

Open CocosCreator and create a new project
New js project

Double-click the scene in the project as shown in the figure, and click the wizard
Ready to add an event

Right click on the script to create a new script, and drag the script to the attribute selector on the right; < br> Create a new script

Double-click to open the script, we are inside Write something, here is the point, knock on the blackboard! ! !

// onLoad () {}, start () {this< /span>.node.on('touchend',()=> {console.log('start btn load, this is also a test!'); var ret = jsb.reflection.callStaticMethod("NativeOcClass"< /span>, "callNativeUIWithTitle:andContent:", "cocos2d-js", "Yes! you call a Native UI from Reflection"); console.log(ret); }) },//Explain the code above /* What we actually use is just one method, callStaticMethod; the parameters of the method: first: an OC class name; second: the OC object The method name; The third one: Just press this to write; The fourth one: The parameter that js needs to pass to oc; */

Is this not intuitive enough, let’s put the project Build it and take a look in the swift project;

Click the CocosCreator menu bar item, build and publish, as shown in the settings:
build release

Find the file as shown in the project directory:
These resources need to be copied

Let’s go to the swift project

Copy to Resources:
Resources

Before opening In the swift project, import the files in Resources into the project:
import into the swift project

Open project.js, you can see the events we wrote before:

testbutton: [function(t, e, o) { "use strict"; cc._RF.push(e, "b6a52AhNWVJ6oIZk3VBVDFd", "testbutton"); cc.Class({ extends: cc.Component, properties: {}, start: function() { this.node.on ("touchend", function() { console.log("js --------> oc"); var t = jsb.reflection.callStaticMethod("NativeOcClass", " callNativeUIWithTitle:andContent:", "cocos2d-js", "Yes! you call a Native UI from Reflection "); console.log(t); });} }); cc._RF.pop(); }, {}]

Next we write a receiving class: < br> NativeOcClass

#import @interface NativeOcClass: NSObject+(NSString *)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content;@end
#import "NativeOcClass.h"#import @implementation NativeOcClass +(NSString *)callNativeUIWithTitle:(NSString *) title andContent:(NSStr ing *)content{ printf("oc file called 
!"); UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@ "OK", nil]; [alertView show]; return @"oc --------> js";}@end

We pop up A native Alert and return the result to js;
See the console print:

Print it out!

Okay, this article is a supplement, do the rest by yourself!!! Actually, the routine of this project is bound by c++ and js, and called lua script The method is exactly the same. For details, please refer to the online example. It is easy to understand if you write a call example separately. This is the end of this article, thank you!!!

Leave a Comment

Your email address will not be published.