Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions cocos2d/core/components/CCAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ let EventType = cc.Enum({
/**
* !#en Emit when pause animation
* !#zh 暂停播放时触发
* @property {String} PAUSE
* @property {String} PAUSE
* @static
*/
PAUSE: 'pause',
Expand Down Expand Up @@ -89,7 +89,7 @@ let EventType = cc.Enum({

/**
* !#en The animation component is used to play back animations.
*
*
* Animation provide several events to register:
* - play : Emit when begin playing animation
* - stop : Emit when stop playing animation
Expand All @@ -99,15 +99,15 @@ let EventType = cc.Enum({
* - finished : Emit when finish playing animation
*
* !#zh Animation 组件用于播放动画。
*
*
* Animation 提供了一系列可注册的事件:
* - play : 开始播放时
* - stop : 停止播放时
* - pause : 暂停播放时
* - resume : 恢复播放时
* - lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
* - finished : 动画播放完成时
*
*
* @class Animation
* @extends Component
* @uses EventTarget
Expand Down Expand Up @@ -391,7 +391,7 @@ let Animation = cc.Class({
return;
}
if (name) {
let state = this._nameToState[name];
let state = this.getAnimationState(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处不应该这么做.有点浪费性能. 建议如下:

    resume: function (name) {
        if (!this._didInit) {
            return;
        }
        if (name) {
            let state = this._nameToState[name];
            if (state) {
                if (!state.curveLoaded) {
                    this._animator._reloadClip(state);
                }
                this._animator.resumeState(state);
            }
        }
        else {
            this.enabled = true;
        }
    },

因为执行到 let state = ... 这一行时, 必然是已经 init过了 (否则第一行就return了)
那么何必 再执行一次 getAnimationState 里的init 呢?

if (state) {
this._animator.resumeState(state);
}
Expand All @@ -411,7 +411,7 @@ let Animation = cc.Class({
setCurrentTime: function (time, name) {
this._init();
if (name) {
let state = this._nameToState[name];
let state = this.getAnimationState(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处不应该这么做.有点浪费性能. 建议如下:

    setCurrentTime: function (time, name) {
        this._init();
        if (name) {
            let state = this._nameToState[name];
            if (state) {
                if (!state.curveLoaded) {
                    this._animator._reloadClip(state);
                }
                this._animator.setStateTime(state, time);
            }
        }
        else {
            this._animator.setStateTime(time);
        }
    },

因为 getAnimationState 里也会执行 _init() . cc @jare @knoxHuang @holycanvas

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getAnimationState 里的 _init 也会判断去 _didInit

image

if (state) {
this._animator.setStateTime(state, time);
}
Expand Down Expand Up @@ -492,7 +492,7 @@ let Animation = cc.Class({
},

/**
* !#en
* !#en
* Remove clip from the animation list. This will remove the clip and any animation states based on it.
* If there are animation states depand on the clip are playing or clip is defaultClip, it will not delete the clip.
* But if force is true, then will always remove the clip and any animation states based on it. If clip is defaultClip, defaultClip will be reset to null
Expand Down Expand Up @@ -524,7 +524,7 @@ let Animation = cc.Class({
else {
if (!CC_TEST) cc.warnID(3902);
return;
}
}
}

if (state && state.isPlaying) {
Expand All @@ -540,7 +540,7 @@ let Animation = cc.Class({
});

if (state) {
delete this._nameToState[state.name];
delete this._nameToState[state.name];
}
},

Expand All @@ -556,7 +556,7 @@ let Animation = cc.Class({
this._init();

if (name) {
let state = this._nameToState[name];
let state = this.getAnimationState(name);
if (state) {
state.sample();
}
Expand All @@ -568,7 +568,7 @@ let Animation = cc.Class({


/**
* !#en
* !#en
* Register animation event callback.
* The event arguments will provide the AnimationState which emit the event.
* When play an animation, will auto register the event callback to the AnimationState, and unregister the event callback from the AnimationState when animation stopped.
Expand All @@ -580,7 +580,7 @@ let Animation = cc.Class({
* @param {String} type - A string representing the event type to listen for.
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param {cc.AnimationState} state
* @param {cc.AnimationState} state
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @param {Boolean} [useCapture=false] - When set to true, the capture argument prevents callback
* from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE.
Expand All @@ -596,15 +596,15 @@ let Animation = cc.Class({
* onPlay: function (type, state) {
* // callback
* }
*
*
* // register event to all animation
* animation.on('play', this.onPlay, this);
*/
on: function (type, callback, target, useCapture) {
this._init();

let ret = this._EventTargetOn(type, callback, target, useCapture);

if (type === 'lastframe') {
let states = this._nameToState;
for (let name in states) {
Expand Down Expand Up @@ -665,7 +665,7 @@ let Animation = cc.Class({

_createStates: function() {
this._nameToState = js.createMap(true);

// create animation states
let state = null;
let defaultClipState = false;
Expand Down