In GameCrom, animations are handled through the
AnimationController component.
With the new system, scripts can switch animations using the global functions
PlayAnimation() and SetAnimation(),
without directly accessing the component.
doc_animation_control.html// ======================================================
// 🕹️ Animation control (new system)
// ======================================================
let anim;
function Start(obj) {
anim = getComponent(obj, "AnimationController");
if (anim) PlayAnimation(obj, "Idle");
}
function Update(obj, dt) {
// Move right
if (IsAnyPressed("ArrowRight", "d", "gamepad_right")) {
if (anim) PlayAnimation(obj, "Walk");
obj.flipX = 0;
}
// Move left
else if (IsAnyPressed("ArrowLeft", "a", "gamepad_left")) {
if (anim) PlayAnimation(obj, "Walk");
obj.flipX = 1;
}
// No movement
else {
if (anim) PlayAnimation(obj, "Idle");
}
// Jump example
if (IsPressed("Space") && obj.onGround) {
if (anim) PlayAnimation(obj, "Jump");
}
}
PlayAnimation(obj, name) – Immediately switches to the specified animation.SetAnimation(obj, name) – Changes the animation only if it is different from the current one.getComponent(obj, "AnimationController") – Returns the animation component of the object (if any).This example shows how to switch between animations based on character state. Ideal for platformers or side-scrolling action games.
// ======================================================
// 🧍 Animation state system (Idle, Walk, Jump, Fall)
// ======================================================
let anim;
function Start(obj) {
anim = getComponent(obj, "AnimationController");
if (anim) PlayAnimation(obj, "Idle");
}
function Update(obj, dt) {
if (!anim) return;
if (!obj.onGround) {
if (obj.vy < 0) PlayAnimation(obj, "Jump"); // Going up
else if (obj.vy > 0) PlayAnimation(obj, "Fall"); // Falling
}
else {
if (Math.abs(obj.vx) > 0.1) PlayAnimation(obj, "Walk");
else PlayAnimation(obj, "Idle");
}
}
PlayAnimation() restarts the animation from frame 0.SetAnimation() avoids restarting if the animation is already active.getComponent(obj, "AnimationController") to check whether the object has animations before calling PlayAnimation().AnimationController with animation names
Idle, Walk, Jump and Fall,
the engine’s movement scripts are already prepared to activate them automatically.
| Method | Description |
|---|---|
PlayAnimation(obj, name) | Plays the specified animation starting from frame 0. |
SetAnimation(obj, name) | Changes the animation only if it is not currently active. |
getComponent(obj, "AnimationController") | Gets the object's AnimationController component. |
function Update(obj, dt) {
const anim = getComponent(obj, "AnimationController");
if (!anim) return;
if (!obj.onGround) {
if (obj.vy < 0) SetAnimation(obj, "Jump");
else SetAnimation(obj, "Fall");
}
else {
if (Math.abs(obj.vx) > 0.1) SetAnimation(obj, "Walk");
else SetAnimation(obj, "Idle");
}
}
This system simplifies animation control, preventing unnecessary restarts and keeping everything synchronized with the character’s physics and movement.