加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-js callFunc传参

发布时间:2020-12-14 17:11:26 所属栏目:百科 来源:网络整理
导读:1.传递一个参数: pg. TestScene . prototype . init = function () { if (cc.Scene. prototype . init . call ( this )) { var sprite = cc.Sprite. create (res. Bet_HighLighted_png ) ; sprite. setPosition (cc. p (cc. winSize . width / 2 , cc. winS

1.传递一个参数:

pg.TestScene.prototype.init = function () {
    if (cc.Scene.prototype.init.call(this)) {

        var sprite = cc.Sprite.create(res.Bet_HighLighted_png);
        sprite.setPosition(cc.p(cc.winSize.width/2,cc.winSize.height/2));
        this.addChild(sprite,1,100);

        var moveToSeat = cc.moveTo(0.5,cc.p(200,200));
        var rotateBy = cc.rotateBy(0.5,180);
        var spawn = cc.spawn(moveToSeat,rotateBy);
        var delayTime = cc.delayTime(2);

        // 写法一
        var callFunc = cc.callFunc(function () {
            console.log("1.sprite tag ==>" + sprite.getTag());
        },this);
        // 写法二
        // var callFunc = cc.callFunc(this.printLog,this,sprite);
        var sequence = cc.sequence(spawn,delayTime,callFunc);
        sprite.runAction(sequence);
        return true;
    }
    return false;
};

pg.TestScene.prototype.printLog = function (sprite) {
    console.log("2.sprite tag ==>" + sprite.getTag());
};

2.传递两个参数:

pg.TestScene.prototype.init = function () {
    if (cc.Scene.prototype.init.call(this)) {
       
       var sprite = cc.Sprite.create(res.Bet_HighLighted_png);
        sprite.setPosition(cc.p(cc.winSize.width / 2,cc.winSize.height / 2));
        this.addChild(sprite,rotateBy);
        var delayTime = cc.delayTime(2);
        
        var i = 0;
        var callFunc = cc.callFunc(this.printLog,this,[i ,sprite]);
        var sequence = cc.sequence(spawn,callFunc);
        sprite.runAction(sequence);
        return true;
    }
    return false;
};

pg.TestScene.prototype.printLog = function (target,data) {
    console.log("i ==>" + data[0]);
    console.log("sprite tag ==>" + data[1].getTag());
};

用法说明:

cc.callFunc(function(target,data) {
        //target  默认是调用这个action的本体Node,Sprite等等
        //data 传递的参数,多个参数可以用[data1,data2] 的Array方式组合

    },    this,//通常绑定最外围的HelloWorldLayer 之类的, 可以方便调用, 比如为了removeChild

    data //参数,任意格式
);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读