Thursday, March 6, 2025
spot_img

Cannot take away occasion listening – JavaScript


Hello! I’m at present engaged on a easy venture, and as a part of it, I wanted to register participant enter.
So I added occasion listener, so every time participant presses inside father or mother node, different stuff occurs.

onLoad() {
    this.node.father or mother.on(cc.Node.EventType.TOUCH_START, operate (occasion) {
        this.Pressed = true;
        console.log("Pressed");
    }, this);        
    this.node.father or mother.on(cc.Node.EventType.TOUCH_END, operate (occasion) {
        this.Pressed = false;
        console.log("Lifted");
    }, this);
}

After which means dowm the code I attempted eradicating these, but it surely doesn’t appear to work

...
 this.node.father or mother.off(cc.Node.EventType.TOUCH_START, operate (occasion) {
     this.Pressed = true;
     console.log("Pressed");
 }, this);
 this.node.father or mother.off(cc.Node.EventType.TOUCH_END, operate (occasion) {
     this.Pressed = false;
     console.log("Lifted");
 }, this);
...

Code actually does attain these instructions, as different strategies there appear to work, however nor .off(), nor even .destroy() known as on this.node don’t take away the listeners.
Actually need assistance with that as there must be many objects with that script and I actually don’t wish to pile up occasion listeners.
Additionally, in all probability necessary, I exploit cocos 2.4.10 + TS and don’t intend to change model of this venture

You’re defining separate features to register and unregister the occasions, you should use the identical operate, do this

onLoad() {
    this.node.father or mother.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);        
    this.node.father or mother.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
}

onTouchStart(occasion) {
     this.Pressed = true;
     console.log("Pressed");
}

onTouchEnd(occasion) {
    this.Pressed = false;
    console.log("Lifted");
}

to take away

 this.node.father or mother.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
 this.node.father or mother.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);

OR
to take away all listeners of this kind

 this.node.father or mother.off(cc.Node.EventType.TOUCH_START);
 this.node.father or mother.off(cc.Node.EventType.TOUCH_END);

Thanks! Someway I didn’t suppose cocos would deal with them as totally different features, as they do identical factor with the identical syntax, however I suppose it solely watches operate’s identify/sort/and so forth.
Anyhow, thanks once more!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles