Cocos Creator API

1.2.0

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

Animation

模块: cc

Animation 组件用于播放动画。你能指定动画剪辑到动画组件并从脚本控制播放。

Animation 提供了一系列可注册的事件:

  • play : 开始播放时
  • stop : 停止播放时
  • pause : 暂停播放时
  • resume : 恢复播放时
  • lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
  • finished : 动画播放完成时

属性(properties)

defaultClip AnimationClip

在勾选自动播放或调用 play() 时默认播放的动画剪辑。

currentClip AnimationClip

当前播放的动画剪辑。

_clips AnimationClip[] private

通过脚本可以访问并播放的 AnimationClip 列表。

playOnLoad Boolean

是否在运行游戏后自动播放默认动画剪辑。

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

方法

getClips ( ) AnimationClip[]

获取动画组件上的所有动画剪辑。

返回:

类型: AnimationClip[]

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

播放当前或者指定的动画,并且停止当前正在播放动画。

名称 类型 描述
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

返回:

类型: 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.

示例:

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

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

播放当前或者指定的动画(将不会停止当前播放的动画)。

名称 类型 描述
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

返回:

类型: 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.

示例:

// 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 ]
)

停止当前或者指定的动画。如果没有指定名字,则停止所有动画。

名称 类型 描述
name optional String

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

pause
(
  • [name ]
)

暂停当前或者指定的动画。如果没有指定名字,则暂停当前正在播放的动画。

名称 类型 描述
name optional String

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

resume
(
  • [name ]
)

重新播放指定的动画,如果没有指定名字,则重新播放当前正在播放的动画。

名称 类型 描述
name optional String

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

setCurrentTime
(
  • [time ]
  • [name ]
)

设置指定动画的播放时间。如果没有指定名字,则设置所有动画的播放时间。

名称 类型 描述
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

获取当前或者指定的动画状态,如果未找到指定动画剪辑则返回 null。

名称 类型 描述
name String

返回:

类型: AnimationState

addClip
(
  • clip
  • [newName ]
)
AnimationState

添加动画剪辑,并且可以重新设置该动画剪辑的名称。

名称 类型 描述
clip AnimationClip

the clip to add

newName optional String

返回:

类型: AnimationState

The AnimationState which gives full control over the animation clip.

removeClip
(
  • clip
  • force
)

从动画列表中移除指定的动画剪辑,
如果依赖于 clip 的 AnimationState 正在播放或者 clip 是 defaultClip 的话,默认是不会删除 clip 的。 但是如果 force 参数为 true,则会强制停止该动画,然后移除该动画剪辑和相关的动画。这时候如果 clip 是 defaultClip,defaultClip 将会被重置为 null。

名称 类型 描述
clip AnimationClip
force Boolean

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

sample ( )

对当前动画进行采样。你可以手动将动画设置到某一个状态,然后采样一次。

on
(
  • type
  • callback
  • target
  • useCapture
)

注册动画事件回调。 回调的事件里将会附上发送事件的 AnimationState。 当播放一个动画时,会自动将事件注册到对应的 AnimationState 上,停止播放时会将事件从这个 AnimationState 上取消注册。

名称 类型 描述
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).

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.

示例:

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
)

取消注册动画事件回调。

名称 类型 描述
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.

示例:

// 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