Different from official descriptions, many APIs of js binding are different from those of ch5. If there is a difference, we need to look at the source code.
Mainly the following files
Cocos2d_specifics.cpp cocos2d_specifics.hpp ScriptingCore. cpp ScriptingCore. h
Now let's talk about the theme.
In ch5, the set touch proxy is different from the jsb api. Here, the target proxy is used as an example.
How to set the ch5 version
Cc. Director. getInstance (). getTouchDispatcher (). addTargetedDelegate (node, priority, true); so there is no problem and there is nothing to say.
Jsb version setting method
If the above sentence is put in the jsb version, an error is immediately reported, telling you that this is not a function. So we are angry with the source code.
Search for addTargetedDelegate and find it in cocos2d_specifics.cpp.
void JSTouchDelegate::registerTargettedDelegate(int priority, bool swallowsTouches) { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, priority, swallowsTouches); } void JSTouchDelegate::registerTargettedDelegate(int priority, bool swallowsTouches) { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, priority, swallowsTouches); }
Then, let's see where to call it.
JSBool js_cocos2dx_JSTouchDelegate_registerTargettedDelegate(JSContextcx, uint32_t argc, jsval vp) { if (argc >= 1) { jsval argv = JS_ARGV(cx, vp); JSObject jsobj = NULL; JSTouchDelegate *touch = new JSTouchDelegate(); touch->autorelease(); touch->registerTargettedDelegate((argc >= 1 ? JSVAL_TO_INT(argv[0]) : 0), (argc >= 2 ? JSVAL_TO_BOOLEAN(argv[1]) : true)); jsobj = (argc == 3 ? JSVAL_TO_OBJECT(argv[2]) : JSVAL_TO_OBJECT(JSVAL_VOID)); touch->setJSObject(jsobj); JSTouchDelegate::setDelegateForJSObject(jsobj, touch); return JS_TRUE; } JS_ReportError(cx, “wrong number of arguments: %d, was expecting >=1”, argc); return JS_FALSE; } JSBool js_cocos2dx_JSTouchDelegate_registerTargettedDelegate(JSContextcx, uint32_t argc, jsval vp) { if (argc >= 1) { jsval argv = JS_ARGV(cx, vp); JSObject jsobj = NULL; JSTouchDelegate *touch = new JSTouchDelegate(); touch->autorelease(); touch->registerTargettedDelegate((argc >= 1 ? JSVAL_TO_INT(argv[0]) : 0), (argc >= 2 ? JSVAL_TO_BOOLEAN(argv[1]) : true)); jsobj = (argc == 3 ? JSVAL_TO_OBJECT(argv[2]) : JSVAL_TO_OBJECT(JSVAL_VOID)); touch->setJSObject(jsobj); JSTouchDelegate::setDelegateForJSObject(jsobj, touch); return JS_TRUE; } JS_ReportError(cx, “wrong number of arguments: %d, was expecting >=1”, argc); return JS_FALSE; }
Next, let's see where js_cocos2dx_JSTouchDelegate_registerTargettedDelegate is registered.
Yes, we found
JS_DefineFunction (cx, ns, "registerTargettedDelegate", role, 1, JSPROP_READONLY | JSPROP_PERMANENT); therefore, we need to use cc. registerTargettedDelegate. For the parameters, refer to the preceding js_cocos2dx_JSTouchDelegate_registerTargettedDelegate method.
The final conclusion is that we should use
cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(node, priority, true); cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(node, priority, true);
Integrated version
To make ch5 and jsb equally effective, we need to judge the current platform in the code.
var addTargetedDelegate = function (node, priority){ if ("opengl" in sys.capabilities && "browser" != sys.platform){ cc.registerTargettedDelegate(priority, true, node); } else { cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(node, priority, true); } }; var addTargetedDelegate = function (node, priority){ if ("opengl" in sys.capabilities && "browser" != sys.platform){ cc.registerTargettedDelegate(priority, true, node); } else { cc.Director.getInstance().getTouchDispatcher().addTargetedDelegate(node, priority, true); }};
Now, this article is complete. The standard proxy and logout proxy are similar solutions. You can check them yourself.