I came across a problem in my work today that had me very
frustrated. In the card game I am programming, there were cards placed out on
the screen and when you put the cursor over them, it would display the card’s
information on a text field next to it. The problem was it was only showing the
information for the last card in the loop. I was checking it by looping through
every card and checking collisions with an if/else statement (Rectangle
intersection collisions). So, I double checked and triple checked and quadruple
checked my rectangles and collisions. Everything was fine! Why wasn’t it
working!?
I figured it was too much for me right now, so I went
outside. I just sat down on the porch and sipped some ice cold water, watching
the cars pass by. I kept thinking of what the problem might have been. What
else could I do to debug this? XNAFramework debugging is so difficult sometimes
because you deal with objects that you just can’t output to the console and
update/draw loops that execute 30 times a second. Even the console output is
erased as soon as you stop debug mode (with Visual Studios).
After a while of being outside, I walked back inside, this
time with confidence. I sat down and just walked through my code, specifically
the loop, by simply looking at it. I couple sense a logic error somewhere.
There! All the cards before the last would not display the contents because the
loop wouldn’t end until the last card! If the mouse was over the first card, it
would show the stats for a split second and then end with the stats of the last
card which was nothing because the mouse was not on it.
So, in conclusion, I cannot have these collision checks in
loops. If you are ever stuck and can’t focus, just go for a walk. Take the
opportunity to go outside and enjoy not having to worry about the problem. When
you come back, it’ll all work out for you. Also, plan for this to happen - don’t
plan to program for 8 hours straight. Figure you’ll program for two hours and
probably get stuck. Go for a 30 minute walk every 2 hours. That’s about an hour
and a half to two hours of breathing time. Take it and use it sparingly. With
this tool on your belt, you will be able to solve any problem you come across.