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("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, 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 i = 0;
        var callFunc = cc.callFunc(this.printLog, this, [i , sprite]);
        var sequence = cc.sequence(spawn, delayTime, 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 //参数,任意格式
);


你可能感兴趣的:(Cocos2d-x3.x,Cocos2d-JS)