From 40c88638c298754a3fe88714f65136cf30fc9065 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 14 Mar 2016 15:18:29 +0800 Subject: [PATCH] fixed stop animation --- cocos2d/animation/animation-animator.js | 2 +- cocos2d/animation/animation-state.js | 6 ++++++ cocos2d/animation/animators.js | 17 +++++++++++------ cocos2d/animation/types.js | 9 --------- test/qunit/unit/test-animation.js | 11 +++++++++++ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/cocos2d/animation/animation-animator.js b/cocos2d/animation/animation-animator.js index fc9b54f043..319db3b11e 100644 --- a/cocos2d/animation/animation-animator.js +++ b/cocos2d/animation/animation-animator.js @@ -80,7 +80,7 @@ p.setStateTime = function (state, time) { p.onStop = function () { var anims = this.playingAnims; - for (var i = 0, l = anims.length; i < l; i++) { + for (var i = anims.length - 1; i >= 0; i--) { anims[i].stop(); } diff --git a/cocos2d/animation/animation-state.js b/cocos2d/animation/animation-state.js index 51292b1a4f..53d5658e2e 100644 --- a/cocos2d/animation/animation-state.js +++ b/cocos2d/animation/animation-state.js @@ -65,6 +65,12 @@ state.onPlay = function () { this.setTime(0); }; +state.onStop = function () { + if (this.animator) { + this.animator.removeAnimation(this); + } +}; + state.setTime = function (time) { this.time = time || 0; diff --git a/cocos2d/animation/animators.js b/cocos2d/animation/animators.js index 3adfa603dd..0a93732ac8 100644 --- a/cocos2d/animation/animators.js +++ b/cocos2d/animation/animators.js @@ -21,26 +21,32 @@ var animProto = Animator.prototype; animProto.update = function (dt) { this._updating = true; - var anims = this.playingAnims; var i, l; + var anims = this.playingAnims; + var stoppedCount = 0; + for (i = 0, l = anims.length; i < l; i++) { var anim = anims[i]; if (anim._isPlaying && !anim._isPaused) { anim.update(dt); + + if (!anim._isPlaying) { + stoppedCount ++; + } } } this._updating = false; + if (anims.length === 0 || stoppedCount >= anims.length ) { + this.stop(); + } + var removeList = this._removeList; for (i = 0, l = removeList.length; i < l; i++) { this.removeAnimation( removeList[i] ); } removeList.length = 0; - - if (anims.length === 0) { - this.stop(); - } }; animProto.onPlay = function () { @@ -48,7 +54,6 @@ animProto.onPlay = function () { }; animProto.onStop = function () { - this.playingAnims.length = 0; cc.director.getAnimationManager().removeAnimator(this); }; diff --git a/cocos2d/animation/types.js b/cocos2d/animation/types.js index 5deaacccd3..a1a12b47de 100644 --- a/cocos2d/animation/types.js +++ b/cocos2d/animation/types.js @@ -347,15 +347,6 @@ JS.mixin(AnimationNode.prototype, { } return info.stopped; - }, - - //onPlay: function () { - //}, - - onStop: function () { - if (this.animator) { - this.animator.removeAnimation(this); - } } }); diff --git a/test/qunit/unit/test-animation.js b/test/qunit/unit/test-animation.js index 1dd4f260e5..847ac1b234 100644 --- a/test/qunit/unit/test-animation.js +++ b/test/qunit/unit/test-animation.js @@ -1125,5 +1125,16 @@ test('stop Animation', function () { strictEqual(animationManager.animators.length, 0, 'playing animators should be 0'); strictEqual(animation._animator.playingAnims.length, 0, 'playing anims should be 0'); + + animation.play('test'); + animationManager.update(0.5); + + strictEqual(animationManager.animators.length, 1, 'playing animators should be 1'); + strictEqual(animation._animator.playingAnims.length, 1, 'playing anims should be 1'); + + animation.stop(); + + strictEqual(animationManager.animators.length, 0, 'playing animators should be 0'); + strictEqual(animation._animator.playingAnims.length, 0, 'playing anims should be 0'); });