I'm having a mental block. I'm writing a C++ breakout game, I've got the basics all done, and have got something that's sort of working. However, for the player paddle, I obviously want to stop it at the edges of the game window. In Java, which I've been doing alot of recently, it was easy, just put an if statement around the move command with the limits, and it would stop. But when I try something like this in C++, the paddle will reach the edge, and then be stuck there unable to move back off the other way. I know why this is happening, because I'm stopping movement at the limits, once it reaches the limit, its always there and so no movement is allowed and it gets stuck. I'm not just moving the sprite either, my direction keys control a velocity, which I use to move the paddle. Which is part of the problem too, as if I was simply controlling the position of the paddle with the keys, it would be easy, but I wouldn't be able to implement any ball bouncing physics very easily. I don't want the paddle to bounce off the edge, as the player may want to stay at the edge to catch the ball. So it needs to stop at the edge, but be able to move away from the edge. So, I need some ideas on what to do. General overview of my coding: p1 has a velocity, and a position. The new position is calculated in the update function, by vel*time. When the left key is pressed, the velocity is set to negative, when right is pressed its set to positive. Screen limit is 5 for the left side and (width-5) for the right side. Thanks
Instead of stopping movement when the paddle reaches the edge, try evaluating the movement then committing it if it is valid. So if moving right (eg 5px) is input, and the paddle is at 476px, while the edge is at 480px, then see that moving 5px right isn't valid, so don't do it. Then if the user inputs left, -5px of 476px is fine, thus, commit it. For more advanced movement, instead of not allowing the 4px of movement, translate the 5px right command into the 4px of remaining space. So trying to go right 5px at 476px would translate to 4px (maximum allowed) and thus set you at the limit of 480px. If you posted some source code I might be able to give you a working example with the code. Good luck with it anyway!