How to centre the player on screen

I'm trying to put the player at the centre of the screen, and have them stay there. I'm currently using gfx.setDrawOffset(-player.x + 200 ,-player.y + 120) after the sprite update call to do this, though this ends up putting the player slightly ahead of the "camera", and I'm not really sure how to fix this. It would also be nice if there was some way to give the camera logic (I'd imagine you could do this via a camera class?), though all I really need for this project is to centre the player on screen.

1 Like

Well, if you set the offset after sprite.update then you are already late by one frame. This is because the function actually calls update AND draw on every sprite. :playdate:

You don't really need a camera class, but it might help to use a global variable for the camera position. E.g. something like this:

camera = playdate.graphics.geometry.vector.new(0, 0)

function playdate.update()
   playdate.graphics.sprite.update()
end

Set the position at the end of your Player class' update

function Player:update()
  ...
  camera.x = -self.x + 200
  camera.y = -self.y + 120
end

Then draw all the sprites with the correct offset. You have to set the offset in every sprite you want the camera to affect because the draw order is not defined. Remember to reset the offset for sprites that should ignore the camera.

function MySprite.draw()
   playdate.graphics.setDrawOffset(camera.x, camera.y)
   ...
end
2 Likes