Cocos Creator API

1.2.0

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

Animation

Module: cc

The animation component is used to play back animations.

Animation provide several events to register:

  • play : Emit when egine playing animation
  • stop : Emit when stop playing animation
  • pause : Emit when pause animation
  • resume : Emit when resume animation
  • lastframe : If animation repeat coutn is larger than 1, emit when animation play to the last frame
  • finished : Emit when finish playing animation

Properties

defaultClip AnimationClip

Animation will play the default clip when start game.

currentClip AnimationClip

Current played clip.

_clips AnimationClip[] private

All the clips used in this animation.

playOnLoad Boolean

Whether the animation should auto play the default clip when start game.

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

Methods

getClips ( ) AnimationClip[]

Get all the clips used in this animation.

returns:

play
(
  • [name ]
  • [startTime ]
)
AnimationState

Plays an animation and stop other animations.

name type description
name optional String

The name of animation to play. If no name is supplied then the default animation will be played.

startTime optional Number

play an animation from startTime

returns:

type: 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.

examples:

var animCtrl = this.node.getComponent(cc.Animation);
animCtrl.play("linear");

playAdditive
(
  • [name ]
  • [startTime ]
)
AnimationState

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.

name type description
name optional String

The name of animation to play. If no name is supplied then the default animation will be played.

startTime optional Number

play an animation from startTime

returns:

type: 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.

examples:

// linear_1 and linear_2 at the same time playing.
var animCtrl = this.node.getComponent(cc.Animation);
animCtrl.playAdditive("linear_1");
animCtrl.playAdditive("linear_2");

stop
(
  • [name ]
)

Stops an animation named name. If no name is supplied then stops all playing animations that were started with this Animation.
Stopping an animation also Rewinds it to the Start.

name type description
name optional String

The animation to stop, if not supplied then stops all playing animations.

pause
(
  • [name ]
)

Pauses an animation named name. If no name is supplied then pauses all playing animations that were started with this Animation.

name type description
name optional String

The animation to pauses, if not supplied then pauses all playing animations.

resume
(
  • [name ]
)

Resumes an animation named name. If no name is supplied then resumes all paused animations that were started with this Animation.

name type description
name optional String

The animation to resumes, if not supplied then resumes all paused animations.

setCurrentTime
(
  • [time ]
  • [name ]
)

Make an animation named name go to the specified time. If no name is supplied then make all animations go to the specified time.

name type description
time optional Number

The time to go to

name optional String

Specified animation name, if not supplied then make all animations go to the time.

getAnimationState
(
  • name
)
AnimationState

Returns the animation state named name. If no animation with the specified name, the function will return null.

name type description
name String

returns:

addClip
(
  • clip
  • [newName ]
)
AnimationState

Adds a clip to the animation with name newName. If a clip with that name already exists it will be replaced with the new clip.

name type description
clip AnimationClip

the clip to add

newName optional String

returns:

type: AnimationState

The AnimationState which gives full control over the animation clip.

removeClip
(
  • clip
  • force
)

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

name type description
clip AnimationClip
force Boolean

If force is true, then will always remove the clip and any animation states based on it.

sample ( )

Samples animations at the current state.
This is useful when you explicitly want to set up some animation state, and sample it once.

on
(
  • type
  • callback
  • target
  • useCapture
)

Register animation event callback. The event argumetns 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.

name type description
type String

A string representing the event type to listen for.

callback Function

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 Event

    event

    • type String

      The name of the event (case-sensitive), e.g. "click", "fire", or "submit"

    • bubbles Boolean

      A boolean indicating whether the event bubbles up through the tree or not

target Object

The target to invoke the callback, can be null

useCapture Boolean

When set to true, the capture argument prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false, callback will NOT be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked when event's eventPhase attribute value is AT_TARGET.

examples:

onPlay: function (event) {
    var state = event.detail;    // state instanceof cc.AnimationState
    var type = event.type;       // type === 'play';
}

// register event to all animation
animation.on('', 'play',      this.onPlay,        this);

off
(
  • type
  • callback
  • target
  • useCapture
)

Unregister animation event callback.

name type description
type String

A string representing the event type being removed.

callback Function

The callback to remove.

target Object

The target to invoke the callback, if it's not given, only callback without target will be removed

useCapture Boolean

Specifies whether the callback being removed was registered as a capturing callback or not. If not specified, useCapture defaults to false. If a callback was registered twice, one with capture and one without, each must be removed separately. Removal of a capturing callback does not affect a non-capturing version of the same listener, and vice versa.

examples:

// unregister event to all animation
animation.off('', 'play',      this.onPlay,        this);

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index