- Creator 版本: 2.4.2
看了下cc.Repeat源码,update函数里最后一行看不明白。
locInnerAction.update((dt * locTimes) % 1.0);//这行看不明白。我怎么感觉应该这样写locInnerAction.update(dt % (1.0/locTimes))
//假如我的一个动作是这样的。重复两次locTimes=2,
//我的理解是,总时间this._duration=4,dt是这个动作总的进度,this._innerAction是个
//cc.Sequence,假如现在已经进入到了第二次循环并且过了一帧后dt=0.6,此时
//cc.Repeat总进度是0.6(重复cc.Sequence动作2次,分界线是1/locTimes=0.5),
//那么当前动作locInnerAction.update((0.6*2)%1= 0.2)
//(按我的理解是locInnerAction.update(0.6%(1/2)= 0.1),当前动作的进度应该是0.1)
//感觉智商不够用
cc.tween(this.node).repeat(2,cc.tween().to(1,{position:cc.v2(10,10)}).to(1,{position:cc.v2(-10,-10)})).start()
//下面是cc.Repeat源码
cc.Repeat = cc.Class({
name: 'cc.Repeat',
extends: cc.ActionInterval,
ctor: function (action, times) {
this._times = 0;
this._total = 0;
this._nextDt = 0;
this._actionInstant = false;
this._innerAction = null;
times !== undefined && this.initWithAction(action, times);
},
/*
* @param {FiniteTimeAction} action
* @param {Number} times
* @return {Boolean}
*/
initWithAction:function (action, times) {
var duration = action._duration * times;
if (this.initWithDuration(duration)) {
this._times = times;
this._innerAction = action;
if (action instanceof cc.ActionInstant){
this._actionInstant = true;
this._times -= 1;
}
this._total = 0;
return true;
}
return false;
},
clone:function () {
var action = new cc.Repeat();
this._cloneDecoration(action);
action.initWithAction(this._innerAction.clone(), this._times);
return action;
},
startWithTarget:function (target) {
this._total = 0;
this._nextDt = this._innerAction._duration / this._duration;
cc.ActionInterval.prototype.startWithTarget.call(this, target);
this._innerAction.startWithTarget(target);
},
stop:function () {
this._innerAction.stop();
cc.Action.prototype.stop.call(this);
},
update:function (dt) {
dt = this._computeEaseTime(dt);
var locInnerAction = this._innerAction;
var locDuration = this._duration;
var locTimes = this._times;
var locNextDt = this._nextDt;
if (dt >= locNextDt) {
while (dt > locNextDt && this._total < locTimes) {
locInnerAction.update(1);
this._total++;
locInnerAction.stop();
locInnerAction.startWithTarget(this.target);
locNextDt += locInnerAction._duration / locDuration;
this._nextDt = locNextDt > 1 ? 1 : locNextDt;
}
// fix for issue #1288, incorrect end value of repeat
if (dt >= 1.0 && this._total < locTimes) {
// fix for cocos-creator/fireball/issues/4310
locInnerAction.update(1);
this._total++;
}
// don't set a instant action back or update it, it has no use because it has no duration
if (!this._actionInstant) {
if (this._total === locTimes) {
locInnerAction.stop();
} else {
// issue #390 prevent jerk, use right update
locInnerAction.update(dt - (locNextDt - locInnerAction._duration / locDuration));
}
}
} else {
//这行看不懂//////////////////////////////////////////////
locInnerAction.update((dt * locTimes) % 1.0);
}
},
isDone:function () {
return this._total === this._times;
},
reverse:function () {
var action = new cc.Repeat(this._innerAction.reverse(), this._times);
this._cloneDecoration(action);
this._reverseEaseList(action);
return action;
},
/*
* Set inner Action.
* @param {FiniteTimeAction} action
*/
setInnerAction:function (action) {
if (this._innerAction !== action) {
this._innerAction = action;
}
},
/*
* Get inner Action.
* @return {FiniteTimeAction}
*/
getInnerAction:function () {
return this._innerAction;
}
});