🎞️ Animation Control via Code (New System)

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.

💡 Suggested file: doc_animation_control.html
This document explains how to use the new runtime animation system. The functions are built into the engine and ready to be used from any script.

📜 Basic Example

// ======================================================
// 🕹️ 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");
  }
}

🧠 Description

🎨 Advanced Example

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");
  }
}

🔧 Useful Tips

🧩 Automatic animations: if an object has an AnimationController with animation names Idle, Walk, Jump and Fall, the engine’s movement scripts are already prepared to activate them automatically.

🎬 Main Methods

MethodDescription
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.

💬 Full Integration Example

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.