After a week of absolutely no productivity to speak of, I have finally been able to sit down and work on game #3 some more. Very excited about this game as I am learning a lot about different aspects of game programming.
Today, I had a simple goal of adding boundaries to the world. Before the player could just go off in one direction to their hearts content (or until their position was a number too big for the processor I suppose).
Maybe I hadn’t looked at the code in a bit too long, or maybe allergies are affecting my work. Regardless, the first solution I implemented seemed to do absolutely nothing. So I did the only debugging thing that I know to do when nothing else is working.
When in Doubt, Print Debugging Information
My first solution was to turn the players velocity around if their position crossed the boundaries.
This actually did have an effect. When you took your finger off the screen (or lifted it off the mouse button), the character would stop (may add this as default behavior).
The problem was, as long as you kept your finger down the game would just let you go and go still. So I printed the velocity and position to the screen to see what was happening.
Feedback Is Important
The moral of the story is that feedback and information beats guessing every time. It will help you make faster, better corrections.
With the feedback to the screen, I could see what was happening as I made code changes. It helped reason through what the next move should be.
Since changing the velocity was not working, I decided to just set the players position to the edge and their velocity in the X or Y direction to zero (depending on the edge they hit).
This had the wonderful affect of making the camera move without the player when you hit a boundary.
Fortunately, swapping the order the player and camera were updated solved the problem.
Until next time, keep making awesome things.