Arrays in My Game
How arrays are used in my JavaScript ocean game.
Arrays in My Game
What Are Arrays?
Arrays are lists that store multiple values inside a single variable.
Example:
const numbers = [1, 2, 3];
Arrays are extremely important in games because they allow programs to manage:
- enemies
- players
- objects
- collectibles
- animations
- game systems
My game uses arrays to organize and control multiple game objects.
Why Arrays Are Important
Without arrays, the game would need separate variables for every object.
For example:
- enemy1
- enemy2
- enemy3
This would become very difficult to manage.
Arrays allow the game to:
- store many objects together
- loop through objects
- update systems efficiently
- scale gameplay easily
Example 1: Storing Game Objects
My game stores objects inside arrays.
this.gameEnv.gameObjects
Explanation
This array contains:
- players
- enemies
- NPCs
- collectibles
- backgrounds
The game uses this array to manage everything in the world.
Example 2: Filtering Enemy Arrays
Arrays are used to find enemies.
const enemies = this.gameEnv.gameObjects.filter(
obj => obj.spriteData?.id?.includes("EnemyElon")
);
Explanation
This creates an array containing:
- only enemy objects
- filtered from all game objects
The game can then:
- check collisions
- move enemies
- update AI behavior
Example 3: Filtering Player Arrays
Enemies search for players using arrays.
const players = this.gameEnv.gameObjects.filter(
obj => obj.constructor.name === "Player"
);
Explanation
This array stores:
- all player objects
- objects matching the Player class
Enemies use this array to:
- target players
- calculate movement
- perform AI behavior
Example 4: Creating Multiple Goldfish
Arrays help generate many objects automatically.
const goldfishList = Array.from({ length: 10 }).map((_, i) => ({
class: Npc,
data: {
id: `Goldfish${i}`
}
}));
Explanation
This creates an array containing:
- 10 goldfish objects
- unique IDs for each fish
- collectible NPCs
Arrays make large systems scalable.
Example 5: Looping Through Arrays
The game loops through arrays repeatedly.
for (const enemy of enemies)
Explanation
This loop:
- accesses each enemy in the array
- updates movement
- checks collisions
- controls gameplay logic
Arrays and loops work together constantly in games.
Example 6: Finding Objects in Arrays
Arrays can search for specific objects.
const fish = gameEnv.gameObjects.find(obj =>
obj.spriteData?.id === `Goldfish${i}`
);
Explanation
This searches the array for:
- a specific goldfish
- matching IDs
- collectible objects
Arrays make object management efficient.
Example 7: Organizing Enemy Systems
Arrays help organize enemy AI.
let nearest = players[0];
Explanation
This accesses:
- the first player inside the array
- target data for enemy movement
Arrays allow enemies to compare multiple targets.
Why Arrays Help My Game
Arrays improved my project by:
- organizing large numbers of objects
- simplifying enemy and player systems
- enabling scalable gameplay
- improving collision detection
- making loops and AI systems possible
Without arrays, managing multiple game objects would be extremely difficult.