有AB两个不同的scene,现在需要AB之间信息相互连通。
我在使用自定义事件的时候发现这样的问题:程序不关闭,自定义事件会不断响应。
就像这样:
demo中每跳转一次场景,自定义事件响应次数就会+1;
这个A绑定的脚本:
GlobalMsg.GLOBAL_EVENTMGR.on(“scene1”,function(){
console.log(“scene1”);
},cc.director);
GlobalMsg.GLOBAL_EVENTMGR.off(“scene1”,function(){
console.log(“scene1”+“is close”);
},cc.director);
this.scheduleOnce(function(){
GlobalMsg.sendMsg(“scene2”);
cc.director.loadScene(“GlobalMsg_2”);
},3);
这是B绑定的脚本:
GlobalMsg.GLOBAL_EVENTMGR.on(“scene2”,function(){
console.log(“scene2”);
},cc.director);
GlobalMsg.GLOBAL_EVENTMGR.off(“scene2”,function(){
console.log(“scene2”+“is close”);
},cc.director);
this.scheduleOnce(function(){
GlobalMsg.sendMsg(“scene1”);
cc.director.loadScene(“GlobalMsg_1”);
},3);
在测试的时候,无论那个off放到哪结果都是一样的,次数会增加。
还请大佬赐教
自己解决了,还是off不对,删除了off的回调,将off放到on里面就好了,但是为什么off有回调就会出现调用次数不断增加,且off的回调也没有调用呢?
还有一个,使用once也可以之前没有注意到。。。