Event callbacks not invoking after registration

Hi,

I am trying to use Cocos Creator’s Event system to send a message between 2 game objects, but perhaps because I am not understanding Events, I cannot get a callback to be invoked when I emit a Event ‘message’. In my game, I have a script that attached to a node (SystemView.js) which procedurally generates things on screen. I also have a mini map that tracks on-screen objects via a secondary camera that renders different shapes representing said on-screen objects (Minimap.js). I would like the node of Minimap.js to be an observer to the node of SystemView.js, so that when new objects are instatiated on screen, the minimap will receive the message to create new shapes for that object. However, I cannot get the two to talk to each other.

What I have done so far is create a function in my game controller script (Game.js) as follows:

emitEvent(eventType) { this.node.emit(eventType); }

Meanwhile, SystemView.js has a reference to the game controller component and calls the emitEvent function with the argument ‘make-minimap-arrow’. The mini map component also has a reference to the game controller and registers itself as a listener with:

this.gamecomponent.on('make-minimap-arrow', this.onMakeArrow, this)

which is supposed to invoke the callback function onMakeArrow, correct? Why isn’t the call back being fired? Am I missing the point of Events? I read the docs on the bubbling system and perhaps that has something to do with it? I ask because the MiniMap node and GameController node are siblings in the hierarchy.

If someone wants to help me out, I’d be grateful! Thanks. Fuller code is below.

// Game.js
cc.Class({
    // Properties, etc...
    onload() {
        // stuff
    },
    emitEvents(eventType) {
        this.node.emit(eventType);
    }
});

// SystemView.js
cc.Class({
    // Properties and stuff

    onLoad() {
        this.gameComponent = cc.Canvas.instance.node.getComponent("Game")
    }

    generateGameObjects() {
        // creating the game objects, then call emit.
        this.gameComponent.emitEvent('create-minimap-arrrow');
    }
});

// Minimap.js
cc.Class({
    // Everything that is supposed to go here, including getting the gameComponent

    onCreateIcon() {
        console.log("I'm being invoked!"); // PS: It's not being invoked.
    }

    onLoad() {
        // other stuff
        this.gameComponent.node.on('create-minimap-arrow', this.onCreateIcon, this);
    }
});

I didn’t see obvious issue in your code, you are listening the node which should trigger the exact same event. Normally when gameComponent.emitEvent called, the onCreateIcon should be invoked.

Please debug with Chrome devtools, and setup break points in SystemView.generateGameObjects and Minimap.onLoad

Make sure both this.gameComponent exists at the moment, also observe whether there are error report in the console.