Consider points A and B as walls and point O as player inside walls.
So they look like A O B.
I want player to move left when he touches rightPoint B. And move right when he touches leftPoint A.
The screen is being rendered and player position is either incremented by 5 for right direction or by -5 for left direction.
What I tried to do: I put it in if else if statement. If distance between A and O is zero, player position gets +5 incrementation. If distance between O and B is zero, player position gets -5 incrementation. But as soon as he touches a wall(say right wall), he moves back -5 and then 5 and again -5 and then 5.
I understand why this is happening but I do not have any logic to implement this.
CODE: Sorry I could not post actual code. Im on mobile internet. I dont have computer internet.
Suppose leftwall at 50, 0 and rightwall at 550, 0 and player at 50, 0.
//this all is being rendered.
If (rightWall - PlayerPos <=0){
PosIncrement = -5;
}
If (leftWall - PlayerPos <=0){
PosIncrement = 5;
}
translateX (PlayerPos);
If player should be between walls, it implies that the left wall should always have a position that is lower the the player position. Player position shuld be equal or larger than 50 and equal or smaller 550
so the correct logic can be