Hi @pandamicro, thank you for your response. Iāve tried with emit but it only works if the event target is the same as the emiter. I do have this node system:
Canvas->[A,B,C,D,E,ā¦]
A,B,C,D, etc⦠are sister nodes with the same script. Each one of the has this:
this.node.on('done',this._callback);
and when somebody touch the node (is a sprite) fires the event:
this.node.emit('done');
So, if node A fires the event (this.node.emit('done');
) only node A gets the notification. And I want all the sisters node got them, thats why I tried with dispatchEvent.
Edit: Found a solution
I have built this system to test it:
Container node (CN) is an empty node that acts as a container. It has the script registerEvents.js
:
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function () {
},
subscribeEvent: function(event,_callback,self)
{
this.node.on(event,_callback,self);
cc.log("event registered");
},
sendEvent: function(event)
{
this.node.emit(event);
//this.node.dispatchEvent(new cc.Event.EventCustom('done', true));
}
});
Trigger is the smallest sprite. Acts as event trigger when touched. It has the script sendEvent.js
:
cc.Class({
extends: cc.Component,
properties: {
},
onLoad: function () {
this.node.on(cc.Node.EventType.TOUCH_START, this.Touched, this);
},
Touched: function () {
cc.log("Fire event done");
//this.node.dispatchEvent(new cc.Event.EventCustom('done', true));
var sender = this.node.parent.getComponent('registerEvents');
sender.sendEvent('done');
},
});
And finally, I do have 3 sister nodes that have the same script: observer.js
cc.Class({
extends: cc.Component,
properties: {
},
// use this for initialization
onLoad: function () {
//this.node.on('done',this._callback);
var reg = this.node.parent.getComponent('registerEvents');
reg.subscribeEvent('done',this._callback,this)
},
_callback: function()
{
cc.log("event received at Observer point.");
}
});
So, the A,B,C nodes register their listeners at CN node. And trigger script, calls a function at CN node that fires the event (emit). So all 3 events are listening at the same node that emits the event. The trick is to use a single script to register and fire the events, no matter where the real emitter and the observers are.
The emitter can be one of the A-B-C nodes too. This does the trick for me.
@pandamicro is there a better way to do it?