Quantcast
Channel: Cocos中文社区 - 最新帖子
Viewing all articles
Browse latest Browse all 494966

自己搞了一个简单粗暴的全局事件派送(全局广播) 7月15日更新

$
0
0

吐槽: 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);
// ...
}


Viewing all articles
Browse latest Browse all 494966

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>