Cocos2d-x JS 3.7.1触摸事件MAC问题

Cocos2d-x JS 3.7.1 touch event MAC issue

本文关键字:事件 MAC 问题 触摸 JS Cocos2d-x      更新时间:2023-09-26

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

我们已经创建了以下eventListener:

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应该如何初始化和使用。

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

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

在您的例子中,您应该检查"this"节点是否在以下代码中有效:

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