• 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 //参数,任意格式
    );
  • 相关阅读:
    【python】正则表达式
    Java 接口、抽象类
    设计模式之抽象工厂方法模式
    设计模式之工厂方法模式
    设计模式之单例模式
    pulltorefresh学习
    ProgressDialog使用
    android:descendantFocusability用法简析
    数据保存之File
    runOnUiThread学习
  • 原文地址:https://www.cnblogs.com/Siegel/p/6197370.html
Copyright © 2020-2023  润新知