Problem with Custom Event in 1.4

There is now a major problem with custom event in Cocos Creator 1.4

They now return an empty listener ID so the system can’t find them and send the trigger:

New function:

var __getListenerID = function (event) {
var eventType = cc.Event, type = event.type;
if (type === eventType.ACCELERATION)
return cc._EventListenerAcceleration.LISTENER_ID;
if (type === eventType.KEYBOARD)
return cc._EventListenerKeyboard.LISTENER_ID;
if (type.startsWith(eventType.MOUSE))
return cc._EventListenerMouse.LISTENER_ID;
if (type === eventType.FOCUS)
return cc._EventListenerFocus.LISTENER_ID;
if (type.startsWith(eventType.TOUCH)){
// Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
// return UNKNOWN instead.
cc.logID(2000);
}
return “”;
};

Old function:

var __getListenerID = function (event) {
var eventType = cc.Event, type = event.getType();
if (type === eventType.ACCELERATION)
return cc._EventListenerAcceleration.LISTENER_ID;
if (type === eventType.KEYBOARD)
return cc._EventListenerKeyboard.LISTENER_ID;
if (type.startsWith(eventType.MOUSE))
return cc._EventListenerMouse.LISTENER_ID;
if (type === eventType.FOCUS)
return cc._EventListenerFocus.LISTENER_ID;
if (type.startsWith(eventType.TOUCH)){
// Touch listener is very special, it contains two kinds of listeners, EventListenerTouchOneByOne and EventListenerTouchAllAtOnce.
// return UNKNOWN instead.
cc.log(cc._LogInfos._getListenerID);
}
return type;
};

The return is now changed to “”.

This is a major road block to us upgrading to Cocos Creator because our game use a lot of custom event, and none of them can trigger anymore.

Is it an oversight or wanted behavior ?

Actually when we design creator, we decide to make our event system a distributed system, which is the contrary of cc.eventManager. Event Manager is a centralized event distribution system, it manages all listeners for all type of events, but in creator, we added EventTarget type which is a host of events, it can distribute events to listeners which is attached on itself.

In Creator, all nodes are EventTarget, you can also create your own EventTarget like this:

var customEventDispatcher = new EventTarget();

// Register an event
customEventDispatcher.on('say-hello', function (event) {
    console.log(event.detail.msg);
});
// Dispatch event with an extra data object which will be stored in event.detail
customEventDispatcher.emit('say-hello', {
    msg: 'Hello, this is Cocos Creator',
});

More information can be found in our documentation:

http://cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/events/

In creator, cc.eventManager’s role is a delegator for all system events, like touch, mouse or keyboard, but it’s not owner of event listeners, user should not directly interact with it. Touch & Mouse events is dispatched by cc.Node which owns them (while hit test succeed), keyboard and other system events is dispatched by cc.systemEvent.

As for custom events, from the above explanation, I think it’s clear that you should use the target (cc.Node) which owns the events or a specific custom event target to dispatch them.

Ok big thanks, we used Cocos Studio previously, so maybe this is bad behaviour from old habit.

Anyway we can’t update to 1.4 for now until the Spine color bug is on a release.

We will change these when we update Creator in a futur time.