【问题标题】:Cocos2d-x JS 3.7.1 touch event MAC issueCocos2d-x JS 3.7.1 触摸事件MAC问题
【发布时间】:2015-08-20 15:39:46
【问题描述】:

我们现在正在处理我们的第一个 cocos2d-x JS 项目,在运行 MAC 项目时遇到了麻烦。对于 WEB 它工作正常。

我们创建了以下事件监听器:

var MouseFetcher;
MouseFetcher = cc.EventListener.create({
    event: cc.EventListener.MOUSE,
    TP: cc.Point,
    initial:function(){
        this.TP = new cc.p(cc.winSize.width/2, cc.winSize.height/2);
    },
    onMouseDown: function (event) {
        this.TP = event.getLocation();
        return true;
    },
    onMouseMove: function (event) {

    },
    onMouseUp:function(event){
        this.TP = event.getLocation();
        var i;
        for(i = 0; i < clickables.length; i++){
            if(clickables[i].containsPoint(this.TP)){
                clickables[i].touchEvent();
            }
        }
    }
});

并且有代码部分在 eventManager 中使用:

this.touchListener = MouseFetcher;
this.touchListener.initial();
cc.eventManager.addListener(this.touchListener, this);

当我们运行应用程序时,出现以下错误:

frameworks/cocos2d-x/cocos/scripting/js-bindings/auto/jsb_cocos2dx_auto.cpp: Line: 18190, Function: js_cocos2dx_EventDispatcher_addEventListenerWithSceneGraphPriority
Invalid Native Object
JS: /script/jsb_cocos2d.js:1643:Error: Invalid Native Object

有人可以帮助解决这个问题吗,我们在 MAC/IOS 版本上做错了什么?在这种情况下应该如何初始化 eventListener 并使用它。

【问题讨论】:

    标签: javascript cocos2d-iphone cocos2d-x cocos2d-js cocos2d-html5


    【解决方案1】:

    “Invalid Native Object”错误意味着您使用的 CCNode 已被释放(可能从未添加到场景图中且未调用 retain(),或者已从父级中删除)。

    在网页版中,节点会自动保留/释放,即使场景图中没有添加节点,您仍然可以使用它;但是,在原生版本中,您必须确保节点在操作之前仍然有效。

    在您的情况下,您应该检查以下代码中的“this”节点是否有效:

    this.touchListener = MouseFetcher;
    this.touchListener.initial();
    cc.eventManager.addListener(this.touchListener, this); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多