0

我们现在正在处理我们的第一个 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 并使用它。

4

1 回答 1

0

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

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

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

this.touchListener = MouseFetcher;
this.touchListener.initial();
cc.eventManager.addListener(this.touchListener, this); 
于 2015-10-26T10:38:27.727 回答