🏛️ Object-Oriented Programming

Methods & Parameters

Project Evidence Required
Implement methods with parameters (e.g. collisionHandler(other, direction)).
Assessment Method
Code review: Method signatures with 2+ parameters

What it is

Methods are functions defined inside a class. Parameters are the inputs they accept; return values are the outputs they produce. Methods with parameters are how an object’s behavior becomes flexible — the same method handles many different cases depending on what you pass in.

Why it matters

A method that takes no parameters can only do one specific thing. A method that takes parameters can adapt: collisionHandler(other, direction) handles any collision against any direction, not just one hard-coded case. This is what makes code reusable.

How GateGame implements it

The spline() helper takes two parameters — an id string and a points array of [relX, relY] pairs — and returns a fully-configured barrier object. Internally it maps each relative coordinate to pixel space using Math.round(px * width). NPC interact() methods accept dialogue strings, button arrays, and callbacks, making each NPC’s behavior data-driven rather than hard-coded.

Code Example

// Parameters: id (string) + points (array of [x,y] pairs)
// Returns: a barrier config object the engine consumes
function spline(id, points) {
  return {
    id,
    splinePoints: points.map(([px, py]) => ({
      x: Math.round(px * width),
      y: Math.round(py * height)
    })),
    visible: true,
    color: '#8B4513',
    lineWidth: 22,
  };
}

Key Takeaway

Parameters convert one specific function into a tool that handles many cases. spline('seg1', [...]) and spline('seg2', [...]) reuse the same logic.