方法一:(子节点向父节点,制作事件传递)
A:
onLoad: function () {
var self = this;
self.node.on(cc.Node.EventType.TOUCH_START, function (event) {
cc.log("mouse_up " + self.index +">"+self.isS);
if (self.isS == 0) {
self.playClick();
var event = new cc.Event.EventCustom('selectRight', true);
event.setUserData(self.index);
self.node.dispatchEvent(event);
}
}, self);
},
B,
self.node.on('selectRight', function (event) {
self.pr.barSprite.getComponent(cc.Animation).play('barShowAnimate');
self.pr.progress -= 0.04;
event.stopPropagation();
});
方法二:(把要实现的方法作为参数在子节点‘初始化’的时候传递)
A:
cc.Class({
extends: cc.Component,
properties: {
callFunction:cc.Function,
passSelf:cc.Class
},
// use this for initialization
onLoad: function () {
var self = this;
self.node.on(cc.Node.EventType.TOUCH_START, function (event) {
self.callFunction(self.index,self.currentindex,self.passSelf);
});
},
init:function (i,ci,callFunction,passSelf) {
self.callFunction = callFunction;
self.passSelf = passSelf
},
});
B:
smallNode.getComponent('smallItem').init(i,array[i],self.callFunction,self);
callFunction : function(index,cindex,passSelf ){..实现方法..},
方法三:二的优化
A:
cc.Class({
extends: cc.Component,
properties: {
passSelf:cc.Class
},
// use this for initialization
onLoad: function () {
var self = this;
self.node.on(cc.Node.EventType.TOUCH_START, function (event) {
self.callFunction(self.index,self.currentindex);
});
},
init:function (i,ci,passSelf) {
this.passSelf = passSelf
},
callFunction : function(index,cindex){
var self = this.passSelf;
..实现方法..
},
});
B:
smallNode.getComponent('smallItem').init(i,array[i],self);
本文介绍了游戏开发中三种实现子节点向父节点传递事件的方法,包括事件传递、参数传递及优化后的参数传递方式。这些方法有助于更好地管理和组织游戏场景中的交互逻辑。
1222

被折叠的 条评论
为什么被折叠?



