How to set the touch proxy in cocos2d-x jsb/html5

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.