From 725617ca0684211a999c26704bc1eea7b3f67294 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Tue, 15 Mar 2016 17:50:09 +0800 Subject: [PATCH] add playAdditive --- cocos2d/core/components/CCAnimation.js | 25 +++++++++- test/qunit/unit/test-animation.js | 65 ++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/components/CCAnimation.js b/cocos2d/core/components/CCAnimation.js index 82c3b60956..fe47e40078 100644 --- a/cocos2d/core/components/CCAnimation.js +++ b/cocos2d/core/components/CCAnimation.js @@ -174,13 +174,36 @@ var Animation = cc.Class({ }, /** - * Plays an animation. + * Plays an animation and stop other animations. * @method play * @param {String} [name] - The name of animation to play. If no name is supplied then the default animation will be played. * @param {Number} [startTime] - play an animation from startTime * @return {AnimationState} - The AnimationState of playing animation. In cases where the animation can't be played (ie, there is no default animation or no animation with the specified name), the function will return null. */ play: function (name, startTime) { + var state = this.playAdditive(name, startTime); + var playingStates = this._animator.playingAnims; + + for (var i = playingStates.length; i >= 0; i--) { + if (playingStates[i] === state) { + continue; + } + + this._animator.stopState(playingStates[i]); + } + + return state; + }, + + /** + * Plays an additive animation, it will not stop other animations. + * If there are other animations playing, then will play several animations at the same time. + * @method playAdditive + * @param {String} [name] - The name of animation to play. If no name is supplied then the default animation will be played. + * @param {Number} [startTime] - play an animation from startTime + * @return {AnimationState} - The AnimationState of playing animation. In cases where the animation can't be played (ie, there is no default animation or no animation with the specified name), the function will return null. + */ + playAdditive: function (name, startTime) { this._init(); var state = this.getAnimationState(name || this._defaultClip.name); if (state) { diff --git a/test/qunit/unit/test-animation.js b/test/qunit/unit/test-animation.js index 847ac1b234..f6ff512092 100644 --- a/test/qunit/unit/test-animation.js +++ b/test/qunit/unit/test-animation.js @@ -1138,3 +1138,68 @@ test('stop Animation', function () { strictEqual(animation._animator.playingAnims.length, 0, 'playing anims should be 0'); }); +test('play Animation', function () { + var entity = new cc.Node(); + var animation = entity.addComponent(cc.Animation); + + var clip = new cc.AnimationClip(); + clip._name = 'move'; + clip._duration = 1; + clip.curveData = { + props: { + x: [ + {frame: 0, value: 0}, + {frame: 1, value: 100} + ] + } + }; + animation.addClip(clip); + + clip = new cc.AnimationClip(); + clip._name = 'rotate'; + clip._duration = 1; + clip.curveData = { + props: { + rotation: [ + {frame: 0, value: 0}, + {frame: 1, value: 90} + ] + } + }; + animation.addClip(clip); + + clip = new cc.AnimationClip(); + clip._name = 'scale'; + clip._duration = 1; + clip.curveData = { + props: { + scaleX: [ + {frame: 0, value: 1}, + {frame: 1, value: 2} + ] + } + }; + animation.addClip(clip); + + var moveState = animation.getAnimationState('move'); + var rotateState = animation.getAnimationState('rotate'); + + strictEqual(moveState.isPlaying, false, 'move animation state should not be playing'); + strictEqual(rotateState.isPlaying, false, 'rotate animation state should not be playing'); + + animation.play('move'); + strictEqual(moveState.isPlaying, true, 'move animation state should be playing'); + strictEqual(rotateState.isPlaying, false, 'rotate animation state should not be playing'); + + animation.play('rotate'); + strictEqual(moveState.isPlaying, false, 'move animation state should not be playing'); + strictEqual(rotateState.isPlaying, true, 'rotate animation state should be playing'); + + animation.playAdditive('move'); + strictEqual(moveState.isPlaying, true, 'move animation state should be playing'); + strictEqual(rotateState.isPlaying, true, 'rotate animation state should be playing'); + + animation.play('scale'); + strictEqual(moveState.isPlaying, false, 'move animation state should not be playing'); + strictEqual(rotateState.isPlaying, false, 'rotate animation state should be playing'); +});