From 41c563a64732847a3fee11d3b18fd1a19dec86a1 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 28 Aug 2017 11:37:20 +0800 Subject: [PATCH] optimize animation --- cocos2d/animation/animation-animator.js | 97 ++++++--- cocos2d/animation/animation-curves.js | 140 ++++++------- cocos2d/animation/animation-manager.js | 27 ++- cocos2d/animation/animators.js | 249 ------------------------ cocos2d/animation/index.js | 1 - cocos2d/animation/types.js | 162 ++++++++++----- 6 files changed, 257 insertions(+), 419 deletions(-) delete mode 100644 cocos2d/animation/animators.js diff --git a/cocos2d/animation/animation-animator.js b/cocos2d/animation/animation-animator.js index def384f3a7..4ba4094000 100644 --- a/cocos2d/animation/animation-animator.js +++ b/cocos2d/animation/animation-animator.js @@ -1,7 +1,7 @@ var JS = cc.js; -var Animator = require('./animators').Animator; +var Playable = require('./playable'); var DynamicAnimCurve = require('./animation-curves').DynamicAnimCurve; -var SampledAnimCurve = require('./animation-curves').SampledAnimCurve; +var quickFindIndex = require('./animation-curves').quickFindIndex; var sampleMotionPaths = require('./motion-path-helper').sampleMotionPaths; var EventAnimCurve = require('./animation-curves').EventAnimCurve; var EventInfo = require('./animation-curves').EventInfo; @@ -11,10 +11,13 @@ var binarySearch = require('../core/utils/binary-search').binarySearchEpsilon; // The actual animator for Animation Component function AnimationAnimator (target, animation) { - Animator.call(this, target); + Playable.call(this); + this.target = target; this.animation = animation; + + this._anims = new JS.array.MutableForwardIterator([]); } -JS.extend(AnimationAnimator, Animator); +JS.extend(AnimationAnimator, Playable); var p = AnimationAnimator.prototype; p.playState = function (state, startTime) { @@ -37,8 +40,32 @@ p.playState = function (state, startTime) { this.play(); }; +p.stopStatesExcept = function (state) { + var iterator = this._anims; + var array = iterator.array; + for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { + var anim = array[iterator.i]; + if (anim === state) { + continue; + } + + this.stopState(anim); + } +}; + +p.on = function (type, callback, target, useCapture) { + var array = this._anims.array; + for (var i = 0; i < array.length; ++i) { + array[i].on(type, callback, target, useCapture); + } +} + p.addAnimation = function (anim) { - Animator.prototype.addAnimation.call(this, anim); + var index = this._anims.array.indexOf(anim); + if (index === -1) { + this._anims.push(anim); + cc.director.getAnimationManager().addAnimation(anim); + } var listeners = this.animation._listeners; for (var i = 0, l = listeners.length; i < l; i++) { @@ -48,7 +75,14 @@ p.addAnimation = function (anim) { }; p.removeAnimation = function (anim) { - Animator.prototype.removeAnimation.call(this, anim); + var index = this._anims.array.indexOf(anim); + if (index >= 0) { + this._anims.fastRemoveAt(index); + cc.director.getAnimationManager().removeAnimation(anim); + } + else { + cc.errorID(3908); + } anim.animator = null; }; @@ -94,10 +128,9 @@ p.setStateTime = function (state, time) { else { time = state; - var iterator = this._anims; - var array = iterator.array; - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - var anim = array[iterator.i]; + var array = this._anims.array; + for (var i = 0; i < array.length; ++i) { + var anim = array[i]; anim.setTime(time); anim.sample(); } @@ -111,8 +144,6 @@ p.onStop = function () { var anim = array[iterator.i]; anim.stop(); } - - Animator.prototype.onStop.call(this); }; p.onPause = function () { @@ -120,26 +151,24 @@ p.onPause = function () { for (var i = 0; i < array.length; ++i) { var anim = array[i]; anim.pause(); + cc.director.getAnimationManager().removeAnimation(anim); // need to unbind animator to anim, or it maybe cannot be gc. anim.animator = null; } - - Animator.prototype.onPause.call(this); }; p.onResume = function () { var array = this._anims.array; for (var i = 0; i < array.length; ++i) { var anim = array[i]; + cc.director.getAnimationManager().addAnimation(anim); // rebind animator to anim anim.animator = this; anim.resume(); } - - Animator.prototype.onResume.call(this); }; p._reloadClip = function (state) { @@ -201,7 +230,7 @@ function initClipData (root, state) { function checkMotionPath(motionPath) { if (!Array.isArray(motionPath)) return false; - for (var i = 0, l = motionPath.length; i < l; i++) { + for (let i = 0, l = motionPath.length; i < l; i++) { var controls = motionPath[i]; if (!Array.isArray(controls) || controls.length !== 6) return false; @@ -213,12 +242,8 @@ function initClipData (root, state) { function createPropCurve (target, propPath, keyframes) { var isMotionPathProp = (target instanceof cc.Node) && (propPath === 'position'); var motionPaths = []; - var curve; - - if (isMotionPathProp) - curve = new SampledAnimCurve(); - else - curve = new DynamicAnimCurve(); + + var curve = new DynamicAnimCurve(); // 缓存目标对象,所以 Component 必须一开始都创建好并且不能运行时动态替换…… curve.target = target; @@ -244,8 +269,8 @@ function initClipData (root, state) { curve.subProps = splitPropPath(propPath); // for each keyframes - for (var j = 0, l = keyframes.length; j < l; j++) { - var keyframe = keyframes[j]; + for (let i = 0, l = keyframes.length; i < l; i++) { + var keyframe = keyframes[i]; var ratio = keyframe.frame / state.duration; curve.ratios.push(ratio); @@ -253,7 +278,7 @@ function initClipData (root, state) { var motionPath = keyframe.motionPath; if (motionPath && !checkMotionPath(motionPath)) { - cc.errorID(3904, target.name, propPath, j); + cc.errorID(3904, target.name, propPath, i); motionPath = null; } @@ -290,6 +315,24 @@ function initClipData (root, state) { sampleMotionPaths(motionPaths, curve, clip.duration, clip.sample); } + // if every piece of ratios are the same, we can use the quick function to find frame index. + var ratios = curve.ratios; + var currRatioDif, lastRatioDif; + var canOptimize = true; + var EPSILON = 1e-6; + for (let i = 1, l = ratios.length; i < l; i++) { + currRatioDif = ratios[i] - ratios[i-1]; + if (i === 1) { + lastRatioDif = currRatioDif; + } + else if (Math.abs(currRatioDif - lastRatioDif) > Math.EPSILON) { + canOptimize = false; + break; + } + } + + curve._findFrameIndex = canOptimize ? quickFindIndex : binarySearch; + return curve; } @@ -350,7 +393,7 @@ function initClipData (root, state) { if (!CC_EDITOR && events) { var curve; - for (var i = 0, l = events.length; i < l; i++) { + for (let i = 0, l = events.length; i < l; i++) { if (!curve) { curve = new EventAnimCurve(); curve.target = root; diff --git a/cocos2d/animation/animation-curves.js b/cocos2d/animation/animation-curves.js index 895ba2e55e..3d79157492 100644 --- a/cocos2d/animation/animation-curves.js +++ b/cocos2d/animation/animation-curves.js @@ -49,9 +49,35 @@ var AnimCurve = cc.Class({ onTimeChangedManually: function () {} }); +/** + * 当每两帧之前的间隔都一样的时候可以使用此函数快速查找 index + */ +function quickFindIndex (ratios, ratio) { + var length = ratios.length - 1; + + if (length === 0) return 0; + + var start = ratios[0]; + if (ratio < start) return 0; + + var end = ratios[length]; + if (ratio > end) return length; + + ratio = (ratio - start) / (end - start); + + var eachLength = 1 / length; + var index = ratio / eachLength; + var floorIndex = index | 0; + var EPSILON = 1e-6; + + if ((index - floorIndex) < EPSILON) { + return floorIndex; + } + + return ~(floorIndex + 1); +} // -// 区别于 SampledAnimCurve。 // // @class DynamicAnimCurve // @@ -94,35 +120,10 @@ var DynamicAnimCurve = cc.Class({ subProps: null }, - _calcValue: function (frameIndex, ratio) { - var values = this.values; - var fromVal = values[frameIndex - 1]; - var toVal = values[frameIndex]; - - var value; - // lerp - if (typeof fromVal === 'number') { - value = fromVal + (toVal - fromVal) * ratio; - } - else { - if (fromVal && fromVal.lerp) { - value = fromVal.lerp(toVal, ratio); - } - else { - // no linear lerp function, just return last frame - value = fromVal; - } - } - - return value; + _findFrameIndex: function () { + return 0; }, - _applyValue: function (target, prop, value) { - target[prop] = value; - }, - - _findFrameIndex: binarySearch, - sample: function (time, ratio, animationNode) { var values = this.values; var ratios = this.ratios; @@ -146,14 +147,35 @@ var DynamicAnimCurve = cc.Class({ value = values[frameCount - 1]; } else { - var fromRatio = ratios[index - 1]; - var toRatio = ratios[index]; - var type = this.types[index - 1]; - var ratioBetweenFrames = (ratio - fromRatio) / (toRatio - fromRatio); + var fromVal = values[index - 1]; - ratioBetweenFrames = computeRatioByType(ratioBetweenFrames, type); + var isNumber = typeof fromVal === 'number'; + var canLerp = fromVal && fromVal.lerp; - value = this._calcValue(index, ratioBetweenFrames); + if (!isNumber && !canLerp) { + value = fromVal; + } + else { + var fromRatio = ratios[index - 1]; + var toRatio = ratios[index]; + var type = this.types[index - 1]; + var ratioBetweenFrames = (ratio - fromRatio) / (toRatio - fromRatio); + + if (type) { + ratioBetweenFrames = computeRatioByType(ratioBetweenFrames, type); + } + + // calculate value + var toVal = values[index]; + + // lerp + if (isNumber) { + value = fromVal + (toVal - fromVal) * ratioBetweenFrames; + } + else if (canLerp) { + value = fromVal.lerp(toVal, ratioBetweenFrames); + } + } } } else { @@ -179,7 +201,7 @@ var DynamicAnimCurve = cc.Class({ var propName = subProps[subProps.length - 1]; if (subProp) { - this._applyValue(subProp, propName, value); + subProp[propName] = value; } else { return; @@ -189,7 +211,7 @@ var DynamicAnimCurve = cc.Class({ } // apply value - this._applyValue(this.target, this.prop, value); + this.target[this.prop] = value; } }); @@ -200,47 +222,6 @@ DynamicAnimCurve.Bezier = function (controlPoints) { -/** - * SampledAnimCurve, 这里面的数值需要是已经都预先sample好了的, - * 所以 SampledAnimCurve 中查找 frame index 的速度会非常快 - * - * @class SampledAnimCurve - * @private - * - * @extends DynamicAnimCurve - */ -var SampledAnimCurve = cc.Class({ - name: 'cc.SampledAnimCurve', - extends: DynamicAnimCurve, - - _findFrameIndex: function (ratios, ratio) { - var length = ratios.length - 1; - - if (length === 0) return 0; - - var start = ratios[0]; - if (ratio < start) return 0; - - var end = ratios[length]; - if (ratio > end) return length; - - ratio = (ratio - start) / (end - start); - - var eachLength = 1 / length; - var index = ratio / eachLength; - var floorIndex = index | 0; - var EPSILON = 1e-6; - - if ((index - floorIndex) < EPSILON) { - return floorIndex; - } - - return ~(floorIndex + 1); - } -}); - - - /** * Event information, * @class EventInfo @@ -433,15 +414,14 @@ var EventAnimCurve = cc.Class({ if (CC_TEST) { cc._Test.DynamicAnimCurve = DynamicAnimCurve; - cc._Test.SampledAnimCurve = SampledAnimCurve; cc._Test.EventAnimCurve = EventAnimCurve; } module.exports = { AnimCurve: AnimCurve, DynamicAnimCurve: DynamicAnimCurve, - SampledAnimCurve: SampledAnimCurve, EventAnimCurve: EventAnimCurve, EventInfo: EventInfo, - computeRatioByType: computeRatioByType + computeRatioByType: computeRatioByType, + quickFindIndex: quickFindIndex }; diff --git a/cocos2d/animation/animation-manager.js b/cocos2d/animation/animation-manager.js index e6fa61a105..bea06cd0ec 100644 --- a/cocos2d/animation/animation-manager.js +++ b/cocos2d/animation/animation-manager.js @@ -4,7 +4,7 @@ var AnimationManager = cc.Class({ ctor: function () { this.__instanceId = cc.ClassManager.getNewInstanceId(); - this._animators = new JS.array.MutableForwardIterator([]); + this._anims = new JS.array.MutableForwardIterator([]); this._delayEvents = []; }, @@ -12,12 +12,12 @@ var AnimationManager = cc.Class({ // for manager update: function (dt) { - var iterator = this._animators; + var iterator = this._anims; var array = iterator.array; for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - var animator = array[iterator.i]; - if (animator._isPlaying && !animator._isPaused) { - animator.update(dt); + var anim = array[iterator.i]; + if (anim._isPlaying && !anim._isPaused) { + anim.update(dt); } } @@ -31,25 +31,24 @@ var AnimationManager = cc.Class({ destruct: function () {}, - // for animator /** - * @param {Animator} animator + * @param {AnimationNode} anim */ - addAnimator: function (animator) { - var index = this._animators.array.indexOf(animator); + addAnimation: function (anim) { + var index = this._anims.array.indexOf(anim); if (index === -1) { - this._animators.push(animator); + this._anims.push(anim); } }, /** - * @param {Animator} animator + * @param {AnimationNode} anim */ - removeAnimator: function (animator) { - var index = this._animators.array.indexOf(animator); + removeAnimation: function (anim) { + var index = this._anims.array.indexOf(anim); if (index >= 0) { - this._animators.fastRemoveAt(index); + this._anims.fastRemoveAt(index); } else { cc.errorID(3907); diff --git a/cocos2d/animation/animators.js b/cocos2d/animation/animators.js deleted file mode 100644 index ad0af17a0b..0000000000 --- a/cocos2d/animation/animators.js +++ /dev/null @@ -1,249 +0,0 @@ -var JS = cc.js; -var Playable = require('./playable'); -var AnimationNode = require('./types').AnimationNode; -var DynamicAnimCurve = require('./animation-curves').DynamicAnimCurve; - -// The base of animators -function Animator (target) { - this.target = target; - // {AnimationNodeBase} - this._anims = new JS.array.MutableForwardIterator([]); -} - -JS.extend(Animator, Playable); - -var animProto = Animator.prototype; - -// 由 AnimationManager 调用,只有在该 animator 处于播放状态时才会被调用 -animProto.update = function (dt) { - var iterator = this._anims; - var array = iterator.array; - - var stoppedCount = 0; - var originLength = array.length; - - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - var anim = array[iterator.i]; - if (anim._isPlaying && !anim._isPaused) { - anim.update(dt); - - if (!anim._isPlaying) { - stoppedCount ++; - } - } - } - - var allRemoved = array.length === 0; - var allStopped = stoppedCount >= originLength; - if (allRemoved || allStopped) { - this.stop(); - } -}; - -animProto.stopStatesExcept = function (state) { - var iterator = this._anims; - var array = iterator.array; - for (iterator.i = 0; iterator.i < array.length; ++iterator.i) { - var anim = array[iterator.i]; - if (anim === state) { - continue; - } - - this.stopState(anim); - } -}; - -animProto.on = function (type, callback, target, useCapture) { - var array = this._anims.array; - for (var i = 0; i < array.length; ++i) { - array[i].on(type, callback, target, useCapture); - } -} - -animProto.onPlay = function () { - cc.director.getAnimationManager().addAnimator(this); -}; - -animProto.onStop = function () { - if (!this._isPaused) { - cc.director.getAnimationManager().removeAnimator(this); - } -}; - -animProto.onResume = function () { - cc.director.getAnimationManager().addAnimator(this); -}; - -animProto.onPause = function () { - cc.director.getAnimationManager().removeAnimator(this); -}; - -animProto.addAnimation = function (anim) { - var index = this._anims.array.indexOf(anim); - if (index === -1) { - this._anims.push(anim); - } -}; - -animProto.removeAnimation = function (anim) { - var index = this._anims.array.indexOf(anim); - if (index >= 0) { - this._anims.fastRemoveAt(index); - } - else { - cc.errorID(3908); - } -}; - - -// The actual animator for Entity -function EntityAnimator (target) { - Animator.call(this, target); -} -JS.extend(EntityAnimator, Animator); - -var entProto = EntityAnimator.prototype; - -// 通用逻辑 - -function computeNullRatios (keyFrames) { - var lastIndex = 0; - var lastRatio = 0; - - var len = keyFrames.length; - for (var i = 0; i < len; i++) { - var frame = keyFrames[i]; - var ratio = frame.ratio; - if (i === 0 && typeof ratio !== "number") { - // 如果一开始就没有 ratio,则默认从 0 开始 - frame.computedRatio = ratio = 0; - } - else if (i === len - 1 && typeof ratio !== "number") { - // 如果最后没有 ratio,则设置为 1 - frame.computedRatio = ratio = 1; - } - if (typeof ratio === "number") { - if (lastIndex + 1 < i) { - var count = i - lastIndex; - var step = (ratio - lastRatio) / count; - for (var j = lastIndex + 1; j < i; j++) { - lastRatio += step; - keyFrames[j].computedRatio = lastRatio; // 不占用已有变量,这样 keyFrames 才能重用 - } - } - lastIndex = i; - lastRatio = ratio; - } - } -} - -if (CC_TEST) { - cc._Test.computeNullRatios = computeNullRatios; -} - -///** -// * @param {object[]} keyFrames -// * @param {object} [timingInput] - This dictionary is used as a convenience for specifying the timing properties of an Animation in bulk. -// * @return {AnimationNode} -// */ -entProto.animate = function (keyFrames, timingInput) { - if (! keyFrames) { - cc.errorID(3909); - return null; - } - // compute absolute ratio of each keyframe with a null ratio - computeNullRatios(keyFrames); - - var anim = this._doAnimate(keyFrames, timingInput); - - this.play(); - return anim; -}; - -// 具体逻辑 - -function findCurve (curves, target, propName) { - var i = 0, curve; - - for (; i < curves.length; i++) { - curve = curves[i]; - if (curve.target === target && curve.prop === propName) { - return curve; - } - } - - return null; -} - -function createPropCurve (curves, target, propName, value, ratio) { - var curve = findCurve(curves, target, propName); - if (! curve) { - curve = new DynamicAnimCurve(); - curves.push(curve); - // 缓存目标对象,所以 Component 必须一开始都创建好并且不能运行时动态替换…… - curve.target = target; - curve.prop = propName; - } - curve.values.push(value); - curve.ratios.push(ratio); -} - -entProto._doAnimate = function (keyFrames, timingInput) { - var anim = new AnimationNode(this, null, timingInput); - anim.play(); - var curves = anim.curves; - - // create curves - var lastRatio = -1; - for (var i = 0; i < keyFrames.length; i++) { - var frame = keyFrames[i]; - - // get ratio - var ratio = frame.ratio; - if (typeof ratio !== "number") { - ratio = frame.computedRatio; - } - if (ratio < 0) { - cc.errorID(3910); - continue; - } - if (ratio < lastRatio) { - cc.errorID(3911); - continue; - } - lastRatio = ratio; - - // 先遍历每一帧,获得所有曲线 - for (var key in frame) { - - var data = frame[key]; - - if (key === 'props') { - for (var propName in data) { - createPropCurve(curves, this.target, propName, data[propName], ratio); - } - } - else if (key === 'comps') { - for (var compName in data) { - var comp = this.target.getComponent(compName); - var compData = data[compName]; - - for (var propName in compData) { - createPropCurve(curves, comp, propName, compData[propName], ratio); - } - } - } - } - } - this._anims.push(anim); - return anim; -}; - -if (CC_TEST) { - cc._Test.EntityAnimator = EntityAnimator; -} - -module.exports = { - Animator: Animator, - EntityAnimator: EntityAnimator -}; diff --git a/cocos2d/animation/index.js b/cocos2d/animation/index.js index 20c03dace5..d09cdd0244 100644 --- a/cocos2d/animation/index.js +++ b/cocos2d/animation/index.js @@ -4,7 +4,6 @@ require('./types'); require('./motion-path-helper'); require('./animation-curves'); require('./animation-clip'); -require('./animators'); require('./animation-manager'); require('./animation-state'); require('./animation-animator'); diff --git a/cocos2d/animation/types.js b/cocos2d/animation/types.js index 09fe78fbe8..c966bd46f8 100644 --- a/cocos2d/animation/types.js +++ b/cocos2d/animation/types.js @@ -128,6 +128,7 @@ AnimationNodeBase.prototype.update = function (deltaTime) {}; */ function AnimationNode (animator, curves, timingInput) { AnimationNodeBase.call(this); + cc.EventTarget.call(this); this._firstFramePlayed = false; @@ -137,6 +138,8 @@ function AnimationNode (animator, curves, timingInput) { this._wrappedInfo = new WrappedInfo(); this._lastWrappedInfo = null; + this._process = process; + this.animator = animator; /** @@ -257,8 +260,78 @@ JS.extend(AnimationNode, AnimationNodeBase); var proto = AnimationNode.prototype; -proto.update = function (delta) { +function process () { + // sample + var info = this.sample(); + if (this._eventFlagMap['lastframe']) { + var lastInfo; + if (!lastInfo) { + lastInfo = this._lastWrappedInfo = new WrappedInfo(info); + } + + if (this.repeatCount > 1 && + ((info.iterations | 0) > (lastInfo.iterations | 0)) // another iterations + ) { + if ((this.wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) { + if (lastInfo.direction < 0) { + this.emit('lastframe', this); + } + } + else { + if (lastInfo.direction > 0) { + this.emit('lastframe', this); + } + } + } + + lastInfo.set(info); + } + + if (info.stopped) { + this.stop(); + this.emit('finished', this); + } +}; + +function simpleProcess () { + var time = this.time; + var duration = this.duration; + + if (time > duration) { + time = time % duration; + if (time === 0) time = duration; + } + else if (time < 0) { + time = time % duration; + if (time !== 0 ) time += duration; + } + + var ratio = time / duration; + + var curves = this.curves; + for (var i = 0, len = curves.length; i < len; i++) { + var curve = curves[i]; + curve.sample(time, ratio, this); + } + + if (this._eventFlagMap['lastframe']) { + var currentIterations = time > 0 ? (time / duration) : -(time / duration); + + var lastIterations = this._lastIterations; + if (lastIterations === undefined) { + lastIterations = this._lastIterations = currentIterations; + } + + if ((currentIterations | 0) > (lastIterations | 0)) { + this.emit('lastframe', this); + } + + this._lastIterations = currentIterations; + } +} + +proto.update = function (delta) { // calculate delay time if (this._delayTime > 0) { @@ -279,33 +352,7 @@ proto.update = function (delta) { this._firstFramePlayed = true; } - // sample - var info = this.sample(); - - if (!this._lastWrappedInfo) { - this._lastWrappedInfo = new WrappedInfo(info); - } - - var anotherIteration = (info.iterations | 0) > (this._lastWrappedInfo.iterations | 0); - if (this.repeatCount > 1 && anotherIteration) { - if ((this.wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse) { - if (this._lastWrappedInfo.direction < 0) { - this.emit('lastframe', this); - } - } - else { - if (this._lastWrappedInfo.direction > 0) { - this.emit('lastframe', this); - } - } - } - - if (info.stopped) { - this.stop(); - this.emit('finished', this); - } - - this._lastWrappedInfo.set(info); + this._process(); }; proto._needRevers = function (currentIterations) { @@ -331,26 +378,17 @@ proto._needRevers = function (currentIterations) { proto.getWrappedInfo = function (time, info) { info = info || new WrappedInfo(); - + var stopped = false; var duration = this.duration; - var ratio = 0; - var wrapMode = this.wrapMode; + var repeatCount = this.repeatCount; - var currentIterations = Math.abs(time / duration); - if (currentIterations > this.repeatCount) currentIterations = this.repeatCount; + var currentIterations = time > 0 ? (time / duration) : -(time / duration); + if (currentIterations >= repeatCount) { + currentIterations = repeatCount; - var needRevers = false; - if (wrapMode & WrapModeMask.ShouldWrap) { - needRevers = this._needRevers(currentIterations); - } - - var direction = needRevers ? -1 : 1; - if (this.speed < 0) direction *= -1; - - if (currentIterations >= this.repeatCount) { stopped = true; - var tempRatio = this.repeatCount - (this.repeatCount | 0); + var tempRatio = repeatCount - (repeatCount | 0); if (tempRatio === 0) { tempRatio = 1; // 如果播放过,动画不复位 } @@ -366,14 +404,23 @@ proto.getWrappedInfo = function (time, info) { if (time !== 0 ) time += duration; } - // calculate wrapped time - if (wrapMode & WrapModeMask.ShouldWrap) { - if (needRevers) time = duration - time; + var needRevers = false; + var shouldWrap = this._wrapMode & WrapModeMask.ShouldWrap; + if (shouldWrap) { + needRevers = this._needRevers(currentIterations); } - ratio = time / duration; + var direction = needRevers ? -1 : 1; + if (this.speed < 0) { + direction *= -1; + } - info.ratio = ratio; + // calculate wrapped time + if (shouldWrap && needRevers) { + time = duration - time; + } + + info.ratio = time / duration; info.time = time; info.direction = direction; info.stopped = stopped; @@ -428,6 +475,25 @@ JS.getset(proto, 'wrapMode', else { this.repeatCount = 1; } + + } +); + +JS.getset(proto, 'repeatCount', + function () { + return this._repeatCount; + }, + function (value) { + this._repeatCount = value; + + var shouldWrap = this._wrapMode & WrapModeMask.ShouldWrap; + var reverse = (this.wrapMode & WrapModeMask.Reverse) === WrapModeMask.Reverse; + if (value === Infinity && !shouldWrap && !reverse) { + this._process = simpleProcess; + } + else { + this._process = process; + } } );