-
Creator 版本: 2.4.10
-
目标平台:模拟器
-
重现方式:
let node = new cc.Node();
node.addParent(this.node);
node.on(‘test’,()=>{});
node.removeFromParent(true);
cc.log(node.hasEventListener(‘test’));
控制台打印是true
与官方文档creator.d.ts描述不符!
-
重现概率:100%
你可以在最后调一下emit,应该是不会触发的
-node.on(‘test’,()=>{});
+node.on(‘test’,()=>{console.log(‘on test’)});
node.removeFromParent(true);
+node.emit(‘test’);
如果需要最后输出为false,改用:
node.targetOff(node)

大佬,我测了一下,还是会触发啊

node.targetOff(node);也一样不会移除。
只有写node.targetOff(‘test’)才会移除。
而我想实现的是“移除全部事件”,要怎么解决?
自定义事件需要自己管理,引擎是不会给你做处理的
只是反馈一下这里与文档描述不符
ondestory自己写事件移除
start(){
let node = new Node();
this.node.addChild(node);
node.on('test1', this.logTest1, this);
node.on('test2', this.logTest2, this);
node.on('test3', this.logTest3, this);
node.targetOff(this);
console.log(node.hasEventListener('test1'));
node.emit('test1');
node.emit('test2');
node.emit('test3');
}
logTest1(){
console.log('on test1')
}
logTest2(){
console.log('on test2')
}
logTest3(){
console.log('on test3')
}
抱歉,之前写的确实错了,改成这样试试。
targetOff是移除“target”上面的回调,所以添加的时候应该设置一个非空的target值(例如this)
这个例子应该可以实现你的需求,一行代码node.targetOff(this)就移除了3个事件
1赞
确实,设置target就可以成功移除了,谢谢大佬!
但是node.removeFromParent(true);还是移除不了,那这个参数填true的作用是什么呢?
