Animator
Component that controls animation playback.
| Type | Name | Interface Description |
|---|---|---|
| Variables | animations: Animation[] | • Function: Gets the list of all animation resources associated with this animator. The returned array contains all animation resources that have been added to this animator. Returns The array of animation resources. |
| Variables | playback: Animation | null | • Function: Gets the animation resource currently being played. Returns The animation resource currently being played, or null if no animation is currently playing. |
| Functions | constructor() | |
| Functions | getAnimation(name: string): Animation | • Function: Gets the animation resource with the specified name. The name must match an animation resource name associated with this animator. Parameters • Returns The animation resource with the specified name. |
| Functions | getEmitter(animationName: string): IEventEmitter | undefined | • Function: Gets the event emitter for the specified animation. The |
| Functions | isPlaying(animationName: string): boolean | • Function: Returns whether the specified animation is currently playing. The Returns True if the specified animation is playing, otherwise false. |
| Functions | pauseAll(): void | • Function: pause all animations playing in this animator |
| Functions | play(animationName: string, wrapMode: number | AnimationWrapMode, speed: number, fadeInTime?: number): void | • Function: Deprecated. Play animation exclusively in the default layer; use Parameters • • • • |
| Functions | resumeAll(): void | • Function: resume all animations playing in this animator |
| Functions | stopAll(): void | • Function: stop all animations playing in this animator |
Examples
animations: Animation[]
const anims = animator.animations;
console.log(anims.length);
playback: Animation | null
const current = animator.playback;
if (current) {
console.log(current.frameCount);
}
constructor()
let obj = new APJS.Animator();
getAnimation(name: string): Animation
const anim = animator.getAnimation('walk');
if (anim) {
animator.playback = anim;
}
isPlaying(animationName: string): boolean
const isPlaying = animator.isPlaying('animationName-walk');
pauseAll(): void
animator.pauseAll();
play(animationName: string, wrapMode: number | AnimationWrapMode, speed: number, fadeInTime?: number): void
animator.play('walk', AnimationWrapMode.Repeat, 1.0, 0.3);
resumeAll(): void
animator.resumeAll();
stopAll(): void
animator.stopAll();
Use Case
Control Animator with named animation clips: play idle/walk/run in Repeat mode, play wave/dance Once with auto-return to idle after duration.
@component()
export class CharacterAnimController extends APJS.BasicScriptComponent {
@serializeProperty
private animatorObject: APJS.SceneObject;
private animator: APJS.Animator;
private animations: APJS.Animation[] = [];
private idleReturnTimer: number = 0;
onStart(): void {
this.animator = this.animatorObject.getComponent("Animator") as APJS.Animator;
if (this.animator) {
this.animations = this.animator.animations;
}
}
private playAnimation(name: string, once: boolean = false): void {
if (!this.animator || this.animations.length === 0) return;
const anim = this.animations.find(a => a.name.toLowerCase().includes(name));
if (!anim) return;
// Skip if already playing this animation
if (this.animator.playback && this.animator.playback.name === anim.name) return;
if (once) {
this.animator.play(anim.name, APJS.AnimationWrapMode.Once, 1);
this.idleReturnTimer = anim.duration;
} else {
this.animator.play(anim.name, APJS.AnimationWrapMode.Repeat, 1);
}
}
onUpdate(deltaTime: number): void {
if (this.idleReturnTimer > 0) {
this.idleReturnTimer -= deltaTime;
if (this.idleReturnTimer <= 0) {
this.idleReturnTimer = 0;
this.playAnimation("idle");
}
}
}
onDestroy(): void {}
}