欢迎关注公众号:“Cocos Creator 源码讲解”,一起学习。
/*
* Repeats an action for ever. <br/>
* To repeat the an action for a limited number of times use the Repeat action. <br/>
* @warning This action can't be Sequenceable because it is not an IntervalAction
* @class RepeatForever
* @extends ActionInterval
* @param {FiniteTimeAction} action
* @example
* var rep = new cc.RepeatForever(cc.sequence(jump2, jump1), 5);
*/
/* /*
* 永远重复一个动作。 <br/>
* 要重复操作有限次数,请使用“重复”操作。 <br/>
* @警告此操作无法进行排序,因为它不是 IntervalAction
* @类重复永远
* @extends ActionInterval
* @param {FiniteTimeAction} 动作
* @例子
* var rep = new cc.RepeatForever(cc.sequence(jump2, jump1), 5);
*/ * /
cc.RepeatForever = cc.Class({
name: 'cc.RepeatForever',
extends: cc.ActionInterval,
/* 构造函数 */
ctor: function (action) {
this._innerAction = null;
action && this.initWithAction(action);
},
/*
* @param {ActionInterval} action
* @return {Boolean}
*/
/* 初始化 init action */
initWithAction: function (action) {
if (!action) {
cc.errorID(1026);
return false;
}
this._innerAction = action;
return true;
},
/* 复制action cc.RepeatForever */
clone: function () {
var action = new cc.RepeatForever();
this._cloneDecoration(action);
action.initWithAction(this._innerAction.clone());
return action;
},
/* cc.RepeatForever 调用 cc.ActionInterval,传入参数 target*/
startWithTarget: function (target) {
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._innerAction.startWithTarget(target);
},
step: function (dt) {
/* 声明局部变量,获取当前action */
var locInnerAction = this._innerAction;
/* 当前action执行step */
locInnerAction.step(dt);
/* action已经执行完毕了 */
if (locInnerAction.isDone()) {
//var diff = locInnerAction.getElapsed() - locInnerAction._duration;
locInnerAction.startWithTarget(this.target);
// to prevent jerk. issue #390 ,1247
//this._innerAction.step(0);
//this._innerAction.step(diff);
locInnerAction.step(locInnerAction.getElapsed() - locInnerAction._duration);
}
},
isDone: function () {
return false;
},
/* 反向一个action */
reverse: function () {
var action = new cc.RepeatForever(this._innerAction.reverse());
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
},
/*
* Set inner action.
* @param {ActionInterval} action
*/
/* 设置当前action */
setInnerAction: function (action) {
if (this._innerAction !== action) {
this._innerAction = action;
}
},
/*
* Get inner action.
* @return {ActionInterval}
*/
/* 获取当前action */
getInnerAction: function () {
return this._innerAction;
}
});
/**
* !#en Create a acton which repeat forever, as it runs forever, it can't be added into cc.sequence and cc.spawn.
* !#zh 永远地重复一个动作,有限次数内重复一个动作请使用 repeat 动作,由于这个动作不会停止,所以不能被添加到 cc.sequence 或 cc.spawn 中。
* @method repeatForever
* @param {FiniteTimeAction} action
* @return {ActionInterval}
* @example
* // example
* var repeat = cc.repeatForever(cc.rotateBy(1.0, 360));
*/
cc.repeatForever = function (action) {
return new cc.RepeatForever(action);
};