Player in the middle of the screen when he walks
-
Thanks for your help but for some reason in my case it doesn't work properly. The player moves a bit faster than the camera! Haha
May I share my code with you?
loop
clear()
drawMapLayer(0)
playerPos = getSpriteLocation(player)
setSpriteCamera(playerPos.x, playerPos.y, 1)sm = State_Machine[player.state]
playerPos.x += sm.x * 0.75
playerPos.y += sm.y * 0.75
//where sm.x or sm.y are 0, -1 or 1
setSpriteLocation ( player, { playerPos.x, playerPos.y })
drawSheet(....)
update()
repeat -
I set it out like this:
Loop
Clear()
Updatesprites()
Setspritecamera(plr.x,plr.y,1)
Drawmap()
Drawsprite's()
Update()
Repeat
I think with your code the getspritelocation with your player.Pos might be upsetting the mix. I'd use plr.x_speed instead of moving it using the players.x -
-
@skullo drawsheet() is used for images so would behave differently any particular reason you want to use drawsheet over drawsprite's()?
-
@waldron Yeah, my sprite have 28 tiles for walking, attacking, etc... I have always worked with a state machine and drawSheet function to recreate animation and move the sprite over the map. In this new project I want the current player to be in the centre of the screen due to the map goes beyond the screen size so I need the camera to follow the current player when walking on the map. There would be more players on the map so I need to play with getSpriteLocation functions in order to calculate distances between them.
-
@Skullo Is it the order that you're doing it? Currently you're changing the camera position based on the player position then updating the player position based on your state machine then drawing it. I think you need to update your player position then your camera position then do the drawing each time you go through the loop.
-
@richard It doesn't work either. I have the feeling that setSpriteCamera moves your sprites instead of moving itself. It is a bit weird. I don't know if someone else think the same.
-
-
If you want different targets for the camera I'd set up a variable and key which one you want it to follow. Wouldn't that work?
-
If followP == 0 then
Setspritecamera(p1.x,p1.y,1)
endif
So on -
The problem is that the camera doesn't follow the target but it moves the map so every sprite drawn moves along the camera :(
-
If setspritecamera() moves the map, then why not always draw the player at the same coordinates but still update player.x and player.y to update the camera. This may be the totally wrong way of looking at it but it could be a step in the right direction.
-
Yeah, I'm drawing the current player at the centre of the screen and using player.x and player.y to update the camera. The problem comes when I try to draw the rest of sprites, if the camera moves, they all do. I need some kind of camera function that only moves the camera.
Edit: CentreSpriteCamera works fine when using DrawSprites, but in my project I have tiled images that need DrawSheet function to be drawn. Using CentreSpriteCamera along with DrawSheet produce the following wrong behaviour:
-
@Skullo you can use set sprite image in a very similar way to draw sheet.
setSpriteImage(handle, imageHandle, tileNumber)
If you used sprites in this way, you could still use your state machine for the specific tile you wanted to show and they would work with the sprite camera along with your map.
-
@richard It works! Thank you very much everyone for your help!!!
-