Position
Every component in TCJSgame has a position defined by its x
and y
coordinates.
These values represent the top-left corner of the object on the canvas. Understanding how to work with
positions is fundamental for moving objects, detecting collisions, and aligning elements in your game.
You can update positions directly, or let them be controlled by speeds, gravity, or move utilities.
For example, player.x += 2;
moves the player right, while player.y -= 2;
moves
it up. Positions can also be reset to teleport objects or reposition them after a collision.
Syntax
component.x = value;
component.y = value;
Example
player.x = 100;
player.y = 200;
function update() {
if (display.keys[39]) player.x += 2;
}
Notes
- Coordinates are measured from the top-left corner of the canvas.
- Use positions to reset a character to a spawn point.
- Combine with camera for scrolling levels.