Acceleration
Acceleration is the rate of change of speed over time. In TCJSgame, you can simulate acceleration by
gradually increasing or decreasing speedX
and speedY
each frame. This gives
smoother motion than instantly setting a new speed, which feels more natural for cars, planes, or
physics-based movement.
While there isn’t a dedicated acceleration property, you can implement it easily inside your
update()
function. For example, pressing the right arrow key could increase
speedX
little by little until a maximum speed is reached.
Example
if (display.keys[39]) {
player.speedX += 0.1; // accelerate right
if (player.speedX > 5) player.speedX = 5; // cap speed
} else {
player.speedX *= 0.9; // friction
}
Notes
- Acceleration creates smoother, more realistic controls.
- Always cap max speed to prevent runaway values.
- Use friction (multiplying by 0.9) to slow objects gradually.