Game Status: Not Started

What is an update() function?

-An update function is a piece of code that runs repeatedly, usually once per frame in a game or animation, to keep things moving and changing. It’s where the program constantly checks conditions and updates positions, states, or behaviors of objects on the screen.

  • An update function runs over and over while the game is playing.
  • It makes things move and change on the screen.
  • It keeps the game working in real time.

Steps to Add a Enemy Into Your Game:

STEP 1: Defining the Enemy Sprite

One of the imporntant steps of the process would be to define all the special properties of the sprite that you want to be your enemy. Here are some examples below..

  • Image source
  • Size and scaling
  • Hitbox for collision detection

NPC Details

STEP 2: Enemy Follwing the Player

This works by checking how far the enemy is from each player using their x and y positions. Then the enemy moves toward the closest player a little bit every frame.

The enemy scans through the update() function:

  • Look through all game objects
  • Find objects that are players
  • Determine which player is closest
  • Move toward that player

Update

STEP 3: Collision Features

Collision is done by measuring the distance between the player and the enemy and if the two are close enough to one another on the screen it will be thought of as a collision. Here is further explanantion…

  • Measure the distance between the enemy and the player
  • If the distance is small enough, they are touching

STEP 4: Killing Player on Collision

When the enemy collides with the player which is defined in step 3, the player will be killed and the game will stop.

  • Stop further updates to prevent repeated triggers
  • Restart the game

Distance

STEP 5: Add Enemy to Level The last step is adding the enemy sprite to the whole level’s object list, ensuring that the new added enemy is continually being loaded and working during gameplay.

Last

SYNTAX INFORMATION:

Update Function Syntax Breakdown

Code / Syntax What it means (simple)
update: function () {} A function that runs every frame to update game behavior
if (this.isKilling) return; Stops everything if the object is in a “killing” state
this.gameEnv.gameObjects All objects currently in the game
.filter(obj => obj.constructor.name === "Player") Keeps only objects that are players
if (players.length === 0) return; Stops the function if there are no players
let nearest = players[0]; Starts by assuming the first player is the closest
let minDist = Infinity; Sets a very large number to compare distances against
for (const player of players) Loops through every player
const dx = player.position.x - this.position.x; Horizontal distance to the player
const dy = player.position.y - this.position.y; Vertical distance to the player
Math.sqrt(dx * dx + dy * dy) Calculates straight-line distance to the player
if (dist < minDist) Checks if this player is closer than the current closest
minDist = dist; Updates the closest distance found so far
nearest = player; Sets this player as the closest target
const speed = 2.2; How fast the object moves each frame
Math.atan2(dy, dx) Finds the angle toward the target
Math.cos(angle) * speed Moves left/right toward the player
Math.sin(angle) * speed Moves up/down toward the player
this.position.x += ... Updates X position (moves object)
this.position.y += ... Updates Y position (moves object)

Activity 1: Find the Bug

update: function () {
  if (this.isKilling) return;

  const players = this.gameEnv.gameObjects.filter(
    obj => obj.constructor.name === "Player"
  );

  if (players.length === 0) return;

  let nearest = players[0];
  let minDist = Infinity;

  for (const player of players) {
    const dx = player.position.x - this.position.x;
    const dy = player.position.y - this.position.y;
    const dist = Math.sqrt(dx * dx + dy * dy);


    if (dist > minDist) {
      minDist = dist;
      nearest = player;
    }
  }

  const speed = 2.2;
  const dx = nearest.position.x - this.position.x;
  const dy = nearest.position.y - this.position.y;
  const angle = Math.atan2(dy, dx);

  this.position.x += Math.cos(angle) * speed;
  this.position.y += Math.sin(angle) * speed;
}

Activity 2: Find the Bug

update: function () {
  if (this.isKilling) return;

  const players = this.gameEnv.gameObjects.filter(
    obj => obj.constructor.name === "Player"
  );

  if (players.length === 0) return;

  let nearest = players[0];
  let minDist = Infinity;

  for (const player of players) {
    const dx = player.position.x - this.position.x;
    const dy = player.position.y - this.position.y;
    const dist = Math.sqrt(dx * dx + dy * dy);

    if (dist < minDist) {
      minDist = dist;
      nearest = player;
    }
  }

  const speed = 2.2;
  const dx = nearest.position.x - this.position.x;
  const dy = nearest.position.y - this.position.y;
  const angle = Math.atan2(dx, dy); // 

  this.position.x += Math.cos(angle) * speed;
  this.position.y += Math.sin(angle) * speed;
}

Summary

An update function runs over and over while the game is playing, usually many times per second. It controls what happens in the game like movement, chasing, and other behavior. This is important for enemies because it lets them move toward the player, track their position, and react in real time instead of just staying still. Without it, enemies wouldn’t be able to follow or interact with the player and the game would feel frozen.