Transformation functions allow you to dynamically modify the position,
scale, and opacity of objects in the scene.
They are API shortcuts to change obj values in a clear and controlled way.
setPosition(obj, x, y)Changes the object's position in the world.
setPosition(player, 200, 300);
setScale(obj, sx, sy)
Changes the object's horizontal (sx) and vertical (sy) scale.
setScale(enemy, 2, 2); // double size
setOpacity(obj, alpha)Changes the object's opacity (between 0 and 1). 0 = fully invisible, 1 = fully opaque.
setOpacity("UIMessage", 0.5); // semi-transparent
moveTowards(obj, targetX, targetY, speed, dt)
Moves the object toward a target point at a fixed speed (pixels/second).
Uses dt to ensure FPS-independent behavior.
// Chase the player
moveTowards(enemy, player.x, player.y, 100, dt);
function Update(obj, dt) {
// Move the object to the right
obj.x += 100 * dt;
// Make it fade out gradually
setOpacity(obj, obj.opacity - 0.1 * dt);
// Teleport if the player presses Z
if (isKeyPressed("z")) {
setPosition(obj, 400, 200);
}
}
obj.x, obj.y, obj.scaleX, obj.scaleY, and obj.opacity.moveTowards is very useful for chasers, guided projectiles, or moving UI elements towards a point.cellSize or the object's base size.