吐槽: creator 的事件分发 跟 cocos2d-x 的又不一样, 特别是那个dispatchEvent. - -|||
爬贴找到楼主的方案。看了下demo, 感觉有点复杂。
最后, 我是这样实现的:
// helper.js
var _node = null;
var helper = {
setHelperNode: (node) => {
_node = node;
},
emit: function(sender, eventName, data) {
_node.emit(eventName, data);
},
on: function(receiver, eventName, callback) {
_node.on(eventName, callback, receiver);
},
off: (receiver, eventName) => {
_node.off(eventName, element.callback, receiver);
}
};
module.exports = helper;
// end of helper
test:
// A.js
var helper = require('helper');
onLoad(): function() {
// ...
var self = this;
helper.on(self.node, 'TestEvent', function(event) {
cc.log('get :', event.detail.msg);
});
// ...
}
// B.js
var helper = require('helper');
onLoad(): function() {
// ...
var self = this;
setTimeout(function(){
helper.emit(self.node, 'TestEvent', { msg: '11111111111111' });
}, 2000);
// ...
}