cocos2d-js 类之家传递事件的方法

来源:互联网 发布:网络文件传输协议 编辑:程序博客网 时间:2024/04/30 06:34

方法一:(子节点向父节点,制作事件传递)

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);
2 0
原创粉丝点击