Understanding JavaScript Objects and Methods:
1. Methods
A method is a function that belongs to an object. Methods define behaviors or actions that an object can perform. These functions can be used to manipulate object properties or perform specific tasks related to the object. By defining methods within an object, you encapsulate functionality and make the object more useful.
Example From Game Code:
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
}
if (GameEnv.currentLevel.tag == "narwhalboss") {
jumpHeightFactor = 0.50;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
}
2. Instantiating Objects
In JavaScript, you can create (instantiate) objects using object literals, the constructor function, or classes. Object literals provide a quick way to define objects with key-value pairs. Constructor functions and classes allow for the creation of multiple instances of objects with shared behaviors. Each method has its advantages depending on the complexity and reusability of the object.
Example From Game Code:
destroy() {
const index = GameEnv.gameObjects.indexOf(this);
// GameEnv.gameObjects.removeChild(this)
// GameEnv.gameObjects.splice(index, 1)
if (index !== -1) {
// Remove the canvas from the DOM
this.canvas.parentNode.removeChild(this.canvas);
GameEnv.gameObjects.splice(index, 1);
}
}
3. Using Objects
Once an object is instantiated, you can access or modify its properties and call its methods. Objects store data in key-value pairs, which can be accessed using dot notation or bracket notation. Modifying properties allows objects to change states dynamically, making them more adaptable for various programming tasks.
Example From Game Code:
if (GameControl.randomEventId === 3 && GameControl.randomEventState === 1) {
this.destroy();
GameControl.endRandomEvent();
};
4. Calling Methods
A method is called using dot notation (object.methodName()). When a method is invoked, it executes the function defined within the object. Methods can interact with the object’s properties or perform standalone tasks, making them essential for implementing object behaviors.
Example From Game Code:
GameEnv.customTimeout(() => {
this.x = GameEnv.innerWidth + 1; // handles alert to next level
}, 2000);
5. Parameters in Methods
Methods can accept parameters to perform actions based on input values. Parameters allow functions to work with dynamic values instead of fixed data. This flexibility enables objects to perform calculations, process user input, or manipulate data efficiently.
Example From Game Code:
playerTubeCollision(player, tube) {
// Check if there is a collision between the player and the tube
const playerRect = player.canvas.getBoundingClientRect();
const tubeRect = tube.canvas.getBoundingClientRect();
const collisionDetected = (
playerRect.right > tubeRect.left &&
playerRect.left < tubeRect.right &&
playerRect.bottom > tubeRect.top &&
playerRect.top < tubeRect.bottom
);
}
6. Return Values
A method can return a value that can be used later in the program. The returned value allows for further processing or storing results in variables. By using return statements, methods can provide meaningful outputs, making them integral to object-oriented programming.
Example From Game Code:
trashCount = [];
if(GameEnv.trashCount.length < 4) {
this.setX(0)
this.setY(500)
this.state.animation = 'idle'
break
}