From 6bc0ff1caece86fbae5350029cd8d04ceb2f6309 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 29 Aug 2017 17:58:07 +0800 Subject: [PATCH 1/2] should ignore animation playOnLoad if already play an animation --- cocos2d/core/components/CCAnimation.js | 3 ++- test/qunit/unit-es5/test-animation.js | 32 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCAnimation.js b/cocos2d/core/components/CCAnimation.js index e70f645625..e9a2b5ffc2 100644 --- a/cocos2d/core/components/CCAnimation.js +++ b/cocos2d/core/components/CCAnimation.js @@ -169,7 +169,8 @@ var Animation = cc.Class({ }, start: function () { - if (!CC_EDITOR && this.playOnLoad && this._defaultClip) { + var isPlaying = this._animator && this._animator.isPlaying; + if (!CC_EDITOR && !isPlaying && this.playOnLoad && this._defaultClip) { var state = this.getAnimationState(this._defaultClip.name); this._animator.playState(state); } diff --git a/test/qunit/unit-es5/test-animation.js b/test/qunit/unit-es5/test-animation.js index 4891a817bb..1ff31fbccd 100644 --- a/test/qunit/unit-es5/test-animation.js +++ b/test/qunit/unit-es5/test-animation.js @@ -1553,3 +1553,35 @@ test('animation pause/resume should remove animation-actor from animation manage strictEqual(manager._animators.array.length, 0, 'should remove animation from animation manager'); }); + + +test('animation play on load', function () { + var entity = new cc.Node(); + + var animation = entity.addComponent(cc.Animation); + animation.playOnLoad = true; + + var clip1 = new cc.AnimationClip(); + clip1._name = 'clip1'; + clip1._duration = 1; + + var clip2 = new cc.AnimationClip(); + clip2._name = 'clip2'; + clip2._duration = 1; + + + animation.addClip(clip1); + animation.addClip(clip2); + + animation._defaultClip = clip1; + + animation.play('clip2'); + + entity.parent = cc.director.getScene(); + animation.start(); + + strictEqual(animation.getAnimationState('clip1').isPlaying, false, 'default clip should not be played if there is playing animation'); + strictEqual(animation.getAnimationState('clip2').isPlaying, true, 'should play the specified animation'); + + entity.parent = null; +}); From a6b42859fc98f1e0e9a043cb8163c09f814eb90d Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 29 Aug 2017 18:08:07 +0800 Subject: [PATCH 2/2] refine code --- cocos2d/core/components/CCAnimation.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cocos2d/core/components/CCAnimation.js b/cocos2d/core/components/CCAnimation.js index e9a2b5ffc2..ff1855e8d0 100644 --- a/cocos2d/core/components/CCAnimation.js +++ b/cocos2d/core/components/CCAnimation.js @@ -169,10 +169,12 @@ var Animation = cc.Class({ }, start: function () { - var isPlaying = this._animator && this._animator.isPlaying; - if (!CC_EDITOR && !isPlaying && this.playOnLoad && this._defaultClip) { - var state = this.getAnimationState(this._defaultClip.name); - this._animator.playState(state); + if (!CC_EDITOR && this.playOnLoad && this._defaultClip) { + var isPlaying = this._animator && this._animator.isPlaying; + if (!isPlaying) { + var state = this.getAnimationState(this._defaultClip.name); + this._animator.playState(state); + } } },