If the player's ( x position + player height ) reaches 436
stop the player and start scrolling.
If the player's x position goes lower then 213
stop the player start scrolling backwards
Heres what happens. everything is working fine, then the player goes forward and when he reaches 416 is starts scrolling fine. when he released the right button he is uncontrollably moved backwards and keeps going backwards until he reaches 213 when he stops and the scrolling starts scrolling backwards.
You haven't given enough information for a spot diagnosis.
But it is also something you should try and work out yourself. You conditional logic is making the wrong decision for some reason, so what you need to do is add some/more diagnostic logging. Either one of your tests is wrong, or some of the maths (obviously?)
Start off logging the test which decides whether to move again or not. If that doesn't provide enough information, add logging to the places where the relevant variables are modified.
With enough logging the bug in the decision making process should become clear.
The exact implementation depends on how you want the camera to behave. But generally you would treat the Camera as its own object. Typically the Camera follows the player, so the camera might need to get information from the player... but the logic for the player and camera should be separate.
This is a simple enough problem so that I feel I shouldn't have to walk you through it.
My advice:
- Forget about the camera when writing player/input code. Player/input code should not care about where the camera is or what it's doing.
- Update the camera every frame, just like you update other in-game objects
- When you update the camera, have it move to follow the player around in whatever manner you desire.
- With that, it's easy to stop the camera from going offscreen. Note again the camera going off screen has nothing to do with where the player is.. it's only about where the camera is.
A "Camera" just indicates what part of the world is visible on screen. In a 3D world it's easy to conceptualize (3D games often even give you control over the camera), but the concept applies equally well to 2D games.
The most basic of basic 2D cameras would just have an X,Y position. But cameras can have other attributes as well, like velocities, rotation, whatever.
The idea is you have an object that represents the camera, and when it moves, the on-screen image scrolls around.