🚀 Example Script - 2D Space Player with Movement & Shooting

This script implements the classic controller for shoot’em up and space shooter games. It allows full 4-direction movement and bullet shooting with a configurable fire rate.

💡 Tip: Attach this script to the player object for smooth movement and frame-rate-independent shooting. You may use an object named Bullet or spawn one inside the Shoot() function.

📜 Full Code

// ======================================================
// 🚀 VERTICAL_SPACE_PLAYER.js — 2D movement + shooting
// ======================================================

let speed = 120;

// ------------------------------------------------------
// 🏁 Start — runs once
// ------------------------------------------------------
function Start(obj) {
  obj.type = "player";
  obj.vx = 0;
  obj.vy = 0;
  obj.canShoot = true;     // prevents infinite rapid shooting
  obj.shootDelay = 0.25;   // seconds between shots
  obj.shootTimer = 0;
}

// ------------------------------------------------------
// 🔄 Update — main logic
// ------------------------------------------------------
function Update(obj, dt) {

  // ======================================================
  // 🎮 MOVEMENT — keyboard + gamepad
  // ======================================================
  obj.vx = 0;
  obj.vy = 0;

  if (IsAnyPressed("ArrowLeft", "a", "gamepad_left", "gamepad_dpad_left")) {
    obj.vx = -speed;
  }
  if (IsAnyPressed("ArrowRight", "d", "gamepad_right", "gamepad_dpad_right")) {
    obj.vx = speed;
  }
  if (IsAnyPressed("ArrowUp", "w", "gamepad_up", "gamepad_dpad_up")) {
    obj.vy = -speed;
  }
  if (IsAnyPressed("ArrowDown", "s", "gamepad_down", "gamepad_dpad_down")) {
    obj.vy = speed;
  }

  // apply movement
  obj.x += obj.vx * dt;
  obj.y += obj.vy * dt;

  // ======================================================
  // 🧱 LIMIT movement inside camera view
  // ======================================================
  const viewLeft   = -offsetX;
  const viewTop    = -offsetY;
  const viewRight  = -offsetX + cameraWidth;
  const viewBottom = -offsetY + cameraHeight;

  obj.x = clamp(obj.x, viewLeft, viewRight - obj.w);
  obj.y = clamp(obj.y, viewTop, viewBottom - obj.h);

  
  // ======================================================
  // 🔫 SHOOTING
  // ======================================================
  if (obj.shootTimer > 0) obj.shootTimer -= dt;

  if (obj.shootTimer <= 0 &&
      IsAnyPressed(" ", "gamepad_a", "gamepad_button_a")) {

    CallFunction(obj, "Shoot"); // call Shoot()
    obj.shootTimer = obj.shootDelay;
  }
}

// ------------------------------------------------------
// 💥 OnTriggerEnter / OnCollisionEnter (optional)
// ------------------------------------------------------
function OnTriggerEnter(obj, other) {
  // ...
}

function OnCollisionEnter(obj, other) {
  // ...
}

// ------------------------------------------------------
// 🔫 Bullet firing function
// ------------------------------------------------------
function Shoot(obj) {
  // Example: create a bullet going upward
  // CreateObject("Bullet", obj.x + obj.w/2, obj.y);
}

📘 Quick Summary

💬 Tip: Combine this with enemy spawners or power-ups to build a complete vertical shooter.
⚙️ Optional extension — animated tilt:
if (obj.vx < 0) PlayAnimation(obj, "Left");
else if (obj.vx > 0) PlayAnimation(obj, "Right");
else PlayAnimation(obj, "Idle");