Movement and Collisions
Make a playable scene.
Position and Velocity
Movement in LÖVE means changing an object's position over time. Store x and y, then add velocity in love.update scaled by dt.
Keeping velocity as dx and dy per second makes diagonal motion and physics clean and frame-rate independent.
function love.update(dt)
player.x = player.x + player.dx * dt
player.y = player.y + player.dy * dt
endInput-Driven Movement
Combine polling with velocity to steer an object. Read direction keys in love.update and apply speed times dt.
Because love.keyboard.isDown is checked every frame, the player moves smoothly while the key is held.
function love.update(dt)
if love.keyboard.isDown('left') then player.x = player.x - player.speed*dt end
if love.keyboard.isDown('right') then player.x = player.x + player.speed*dt end
endAll lessons in this course
- The LOVE Game Loop
- Drawing Shapes and Images
- Handling Input
- Movement and Collisions