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

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

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);
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值