Pass events between nodes

Pass events between nodes
0

#1

in the canvas I have a script for the control of the swipe in the scene, I would like to pass an event to the player (son node of the canvas) depending on the swipe (it is already implemented) I just need the player’s script to detect those changes to handle the Actions.

I have tried with the event delivery but they are not working in the opposite way (from node a to c)

A
    if(swipeX > 0){
                    /* left swipe */ 
                    console.log('left');
                    this.node.dispatchEvent( new cc.Event.EventCustom('left', true) );

B
this.node.on('left', function (event) {
           console.log(event);
           
           this.movementLeft();
        event.stopPropagation();
      });

#2

I would like to know too @Big_Bear can help?


#3

This is the right use,you can find it in API document.

this.node.emit('left', {...});

https://docs.cocos.com/creator/manual/en/scripting/events.html#launch-event


#4

I had already tried it, but that solution does not work for me … I have seen the example project but it does not do it that way, could have some complete example of what I am failing? because the event delivery works perfectly for me but it is not the case that I needed (from A => B not around).

B

this.node.on('say-hello', function (event) {
      console.log(event.detail.msg);
    });

A

this.node.emit('say-hello', {
      msg: 'Hello, this is Cocos Creator',
    });

#5

In your example you should not use event.detail.msg but event.msg . Actually I would call it data instead of event, because with emit function you send only data not the Event type event.