cocos2d-js callFunc传参

前端之家收集整理的这篇文章主要介绍了cocos2d-js callFunc传参前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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(@H_403_148@"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(@H_403_148@"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(@H_403_148@"i ==>" + data[0]);
    console.log(@H_403_148@"sprite tag ==>" + data[1].getTag());
};

用法说明:

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

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

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

猜你在找的Cocos2d-x相关文章