方法一:(子节点向父节点,制作事件传递)
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);