diff --git a/cocos2d/animation/animation-animator.js b/cocos2d/animation/animation-animator.js index 234b7a92bd..af4c23d98e 100644 --- a/cocos2d/animation/animation-animator.js +++ b/cocos2d/animation/animation-animator.js @@ -31,7 +31,7 @@ p.playState = function (state, startTime) { state.play(); if (typeof startTime === 'number') { - state.time = startTime; + state.setTime(startTime); } this.play(); diff --git a/cocos2d/animation/animation-curves.js b/cocos2d/animation/animation-curves.js index 5dce0a2796..abc51e9011 100644 --- a/cocos2d/animation/animation-curves.js +++ b/cocos2d/animation/animation-curves.js @@ -297,7 +297,15 @@ var EventAnimCurve = cc.Class({ * @type {Object} * @default null */ - _lastWrappedInfo: null + _lastWrappedInfo: null, + + /** + * Ignore event index + * @property _ignoreIndex + * @type {number} + * @default NaN + */ + _ignoreIndex: NaN }, _wrapIterations: function (iterations) { @@ -318,6 +326,10 @@ var EventAnimCurve = cc.Class({ if (direction < 0) currentIndex += 1; } + if (this._ignoreIndex !== currentIndex) { + this._ignoreIndex = NaN; + } + var lastWrappedInfo = this._lastWrappedInfo; currentWrappedInfo.frameIndex = currentIndex; @@ -377,7 +389,7 @@ var EventAnimCurve = cc.Class({ }, _fireEvent: function (index) { - if (index < 0 || index >= this.events.length) return; + if (index < 0 || index >= this.events.length || this._ignoreIndex === index) return; var eventInfo = this.events[index]; var events = eventInfo.events; @@ -396,8 +408,23 @@ var EventAnimCurve = cc.Class({ } }, - onTimeChangedManually: function () { + onTimeChangedManually: function (time, animationNode) { this._lastWrappedInfo = null; + this._ignoreIndex = NaN; + + var info = animationNode.getWrappedInfo(time); + var direction = info.direction; + var frameIndex = binarySearch(this.ratios, info.ratio); + + // only ignore when time not on a frame index + if (frameIndex < 0) { + frameIndex = ~frameIndex - 1; + + // if direction is inverse, then increase index + if (direction < 0) frameIndex += 1; + + this._ignoreIndex = frameIndex; + } } }); diff --git a/cocos2d/animation/animation-state.js b/cocos2d/animation/animation-state.js index 7e2d90c3c7..5f28adaf9c 100644 --- a/cocos2d/animation/animation-state.js +++ b/cocos2d/animation/animation-state.js @@ -88,8 +88,9 @@ state.onStop = function () { state.setTime = function (time) { this.time = time || 0; + var self = this; this.curves.forEach(function (curve) { - curve.onTimeChangedManually(); + curve.onTimeChangedManually(time, self); }); }; diff --git a/cocos2d/core/components/CCPageView.js b/cocos2d/core/components/CCPageView.js index 2ec2ac0c5b..afa56b5be8 100644 --- a/cocos2d/core/components/CCPageView.js +++ b/cocos2d/core/components/CCPageView.js @@ -111,10 +111,23 @@ var PageView = cc.Class({ default: 0.5, type: cc.Float, slide: true, - range: [0, 1, 0.1], + range: [0, 1, 0.01], tooltip: 'i18n:COMPONENT.pageview.scrollThreshold' }, + /** + * !#en Change the AutoScroll stop epsilon value of PageView, change this value could adjust the PageView's + * event triggerring timing. + * !#zh 设置 PageView 页面自动滚动动画结束的阈值,修改此值可以调整 PageView 事件的发送时机。 + * @property {Number} autoScrollStopEpsilon + */ + autoScrollStopEpsilon: { + default: 0.1, + type: cc.Float, + range: [0, 1, 0.01], + tooltip: 'i18n:COMPONENT.pageview.autoScrollStopEpsilon' + }, + /** * !#en The Page View Indicator * !#zh 页面视图指示器组件 @@ -300,6 +313,11 @@ var PageView = cc.Class({ } }, + //override the method of ScrollView + getAutoScrollStopEpsilon: function () { + return this.autoScrollStopEpsilon; + }, + // 刷新页面视图 _updatePageView: function () { var pageCount = this._pages.length; diff --git a/cocos2d/core/components/CCScrollView.js b/cocos2d/core/components/CCScrollView.js index c3dafe2a4d..5b093e4b99 100644 --- a/cocos2d/core/components/CCScrollView.js +++ b/cocos2d/core/components/CCScrollView.js @@ -1088,6 +1088,10 @@ var ScrollView = cc.Class({ return false; }, + getAutoScrollStopEpsilon: function () { + return EPSILON; + }, + _processAutoScrolling: function(dt) { var isAutoScrollBrake = this._isNecessaryAutoScrollBrake(); @@ -1100,7 +1104,11 @@ var ScrollView = cc.Class({ } var newPosition = cc.pAdd(this._autoScrollStartPosition, cc.pMult(this._autoScrollTargetDelta, percentage)); - var reachedEnd = (percentage === 1); + var reachedEnd = Math.abs(percentage - 1) <= this.getAutoScrollStopEpsilon(); + + if(reachedEnd) { + newPosition = cc.pAdd(this._autoScrollStartPosition, this._autoScrollTargetDelta); + } if (this.elastic) { var brakeOffsetPosition = cc.pSub(newPosition, this._autoScrollBrakingStartPosition); diff --git a/test/qunit/unit/test-animation.js b/test/qunit/unit/test-animation.js index 51f1afc1ca..21477f4de8 100644 --- a/test/qunit/unit/test-animation.js +++ b/test/qunit/unit/test-animation.js @@ -882,6 +882,51 @@ test('EventAnimCurve', function () { ], 'should triggered frame 5 event once if speed is -1'); + state.speed = 1; + state.wrapMode = cc.WrapMode.Normal; + animation.stop('test'); + animation.play('test', 1.1); + calls = []; + manager.update(0); + manager.update(1); + deepEqual(calls, [ + { + func: 'func1', + args: ['Frame 3 Event triggered'] + }, + { + func: 'func1', + args: ['Frame 4 Event triggered'] + }, + { + func: 'func1', + args: ['Frame 5 Event triggered'] + } + ], 'should triggered from frame 3 to frame 5'); + + + state.wrapMode = cc.WrapMode.Reverse; + animation.stop('test'); + animation.play('test', 1.1); + calls = []; + manager.update(0); + manager.update(1); + deepEqual(calls, [ + { + func: 'func2', + args: [1,2] + }, + { + func: 'func3', + args: ['Second event on frame 0.4'] + }, + { + func: 'func1', + args: ['Frame 0 Event triggered'] + } + ], 'should triggered from frame 2 to frame 0'); + + // new clip clip = new cc.AnimationClip(); clip._duration = 2;