CocosCreator 源码CCActionInterval详解

/** * @module cc */
/** * !#en * <p> An interval action is an action that takes place within a certain period of time. <br/> * It has an start time, and a finish time. The finish time is the parameter<br/> * duration plus the start time.</p> * * <p>These CCActionInterval actions have some interesting properties, like:<br/> * - They can run normally (default)  <br/> * - They can run reversed with the reverse method   <br/> * - They can run with the time altered with the Accelerate, AccelDeccel and Speed actions. </p> * * <p>For example, you can simulate a Ping Pong effect running the action normally and<br/> * then running it again in Reverse mode. </p> * !#zh 时间间隔动作,这种动作在已定时间内完成,继承 FiniteTimeAction。 * @class ActionInterval * @extends FiniteTimeAction * @param {Number} d duration in seconds *//* /// 间隔动作,继承自有限动作(不会无限是执行下去)/// 间隔动作是一个会在确定时间段内执行的动作/// 这种动作有一个开始时间和结束时间,结束时间是开始时间加上持续时间 */cc.ActionInterval = cc.Class({    name: 'cc.ActionInterval',    extends: cc.FiniteTimeAction,
    /* 构造函数 */    ctor:function (d) {        /* 最大值 */        this.MAX_VALUE = 2;        /* 自操作开始运行以来已经过去了多少秒 */        this._elapsed = 0;        /* 是否是第一次tick */        this._firstTick = false;
        this._easeList = null;        this._speed = 1;        this._timesForRepeat = 1;        this._repeatForever = false;        this._repeatMethod = false;//Compatible with repeat class, Discard after can be deleted        this._speedMethod = false;//Compatible with repeat class, Discard after can be deleted        d !== undefined && cc.ActionInterval.prototype.initWithDuration.call(this, d);    },
    /*     * How many seconds had elapsed since the actions started to run.     * @return {Number}     */    // 从这个动作开始到现在已经过去了多场时间,这个时间是按秒计算的    getElapsed:function () {        return this._elapsed;    },
    /*     * Initializes the action.     * @param {Number} d duration in seconds     * @return {Boolean}     */    /* 初始化操作。 */    initWithDuration:function (d) {        this._duration = (d === 0) ? cc.macro.FLT_EPSILON : d;        // prevent division by 0        // This comparison could be in step:, but it might decrease the performance        // by 3% in heavy based action games.        /* // 防止被 0 除        // 这种比较可以是步骤:,但可能会降低性能        // 在重度动作游戏中增加 3%。         */        this._elapsed = 0;        this._firstTick = true;        return true;    },
    isDone:function () {        /* 经历的时间已经超过 init设置的事假 */        return (this._elapsed >= this._duration);    },
    /* 复制当前属性给一个action */    _cloneDecoration: function(action){        action._repeatForever = this._repeatForever;        action._speed = this._speed;        action._timesForRepeat = this._timesForRepeat;        action._easeList = this._easeList;        action._speedMethod = this._speedMethod;        action._repeatMethod = this._repeatMethod;    },/* 把easelist里面的action 进行reverse */    _reverseEaseList: function(action){        if(this._easeList){            action._easeList = [];            for(var i=0; i<this._easeList.length; i++){                action._easeList.push(this._easeList[i].reverse());            }        }    },
    /* 复制当前action */    clone:function () {        var action = new cc.ActionInterval(this._duration);        this._cloneDecoration(action);        return action;    },
    /**     * !#en Implementation of ease motion.     * !#zh 缓动运动。     * @method easing     * @param {Object} easeObj     * @returns {ActionInterval}     * @example     * action.easing(cc.easeIn(3.0));     */    easing: function (easeObj) {        /* 清空_easeList ,把新的参数push进去 */        if (this._easeList)            this._easeList.length = 0;        else            this._easeList = [];        /* 把action push到list */        for (var i = 0; i < arguments.length; i++)            this._easeList.push(arguments[i]);        return this;    },
    /* 计算easetime */    _computeEaseTime: function (dt) {        var locList = this._easeList;        if ((!locList) || (locList.length === 0))            return dt;        for (var i = 0, n = locList.length; i < n; i++)            dt = locList[i].easing(dt);        return dt;    },
    step:function (dt) {        if (this._firstTick) {            this._firstTick = false;            this._elapsed = 0;        } else            this._elapsed += dt;
        //this.update((1 > (this._elapsed / this._duration)) ? this._elapsed / this._duration : 1);        //this.update(Math.max(0, Math.min(1, this._elapsed / Math.max(this._duration, cc.macro.FLT_EPSILON))));        var t = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);        t = (1 > t ? t : 1);        this.update(t > 0 ? t : 0);
        //Compatible with repeat class, Discard after can be deleted (this._repeatMethod)        /* //兼容repeat类, */        if(this._repeatMethod && this._timesForRepeat > 1 && this.isDone()){            if(!this._repeatForever){                this._timesForRepeat--;            }            //var diff = locInnerAction.getElapsed() - locInnerAction._duration;            this.startWithTarget(this.target);            // to prevent jerk. issue #390 ,1247            //this._innerAction.step(0);            //this._innerAction.step(diff);            this.step(this._elapsed - this._duration);
        }    },
    /* 调用startWithTarget */    startWithTarget:function (target) {        cc.Action.prototype.startWithTarget.call(this, target);        this._elapsed = 0;        this._firstTick = true;    },
    /* 无用 */    reverse:function () {        cc.logID(1010);        return null;    },
    /*     * Set amplitude rate.     * @warning It should be overridden in subclass.     * @param {Number} amp     */    /*  * 设置振幅率。     * @它应该在子类中被覆盖。 */    setAmplitudeRate:function (amp) {        // Abstract class needs implementation        cc.logID(1011);    },
    /*     * Get amplitude rate.     * @warning It should be overridden in subclass.     * @return {Number} 0     */    getAmplitudeRate:function () {        // Abstract class needs implementation        cc.logID(1012);        return 0;    },
    /**     * !#en     * Changes the speed of an action, making it take longer (speed>1)     * or less (speed<1) time. <br/>     * Useful to simulate 'slow motion' or 'fast forward' effect.     * !#zh     * 改变一个动作的速度,使它的执行使用更长的时间(speed > 1)<br/>     * 或更少(speed < 1)可以有效得模拟“慢动作”或“快进”的效果。     * @param {Number} speed     * @returns {Action}     */    speed: function(speed){        if(speed <= 0){            cc.logID(1013);            return this;        }
        this._speedMethod = true;//Compatible with repeat class, Discard after can be deleted        this._speed *= speed;        return this;    },
    /**     * Get this action speed.     * @return {Number}     */    /* 获取当前action的速度 */    getSpeed: function(){        return this._speed;    },
    /**     * Set this action speed.     * @param {Number} speed     * @returns {ActionInterval}     */    setSpeed: function(speed){        this._speed = speed;        return this;    },
    /**     * !#en     * Repeats an action a number of times.     * To repeat an action forever use the CCRepeatForever action.     * !#zh 重复动作可以按一定次数重复一个动作,使用 RepeatForever 动作来永远重复一个动作。     * @method repeat     * @param {Number} times     * @returns {ActionInterval}     */    /* 设置重复次数,重复播放一个action */    repeat: function(times){        times = Math.round(times);        if(isNaN(times) || times < 1){            cc.logID(1014);            return this;        }        this._repeatMethod = true;//Compatible with repeat class, Discard after can be deleted        this._timesForRepeat *= times;        return this;    },
    /**     * !#en     * Repeats an action for ever.  <br/>     * To repeat the an action for a limited number of times use the Repeat action. <br/>     * !#zh 永远地重复一个动作,有限次数内重复一个动作请使用 Repeat 动作。     * @method repeatForever     * @returns {ActionInterval}     */    /* 永远地重复一个动作,有限次数内重复一个动作请使用 Repeat 动作。 */    repeatForever: function(){        this._repeatMethod = true;//Compatible with repeat class, Discard after can be deleted        this._timesForRepeat = this.MAX_VALUE;        this._repeatForever = true;        return this;    }});
cc.actionInterval = function (d) {    return new cc.ActionInterval(d);};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值