Google+

Monday, September 19, 2011

Let's Make A Game! Episode 21: A More Elegant Kludge For A More Civilized Age

If you'll remember, my character was accidentally wandering off the screen. I slapped in some code to keep him in the right place, but it wasn't very elegant.

It occurred to me: There's already a function for clamping values within a valid area: MathHelper.Clamp.

I fiddled with MathHelper.Clamp a little bit. First, I did this:
player.Position.X = MathHelper.Clamp(player.Position.X + 50, 0,GraphicsDevice.Viewport.Width - player.Width);
That was pretty stupid of me. It just made the character rocket across the screen every time it went through a loop.

I realized that the value was ALREADY built in to this function. How could I be so stupid? It's right here:
player.Position.X = MathHelper.Clamp(player.Position.X, 0,GraphicsDevice.Viewport.Width - player.Width);
Changed that to 50. Boom. Done.

Another thing I changed: I decided to switch my sounds. When a character gets hit, he screams bloody murder but when he dies, he just lets out a little groan. I switched the two sounds and renamed them appropriately so I could keep track of them. Much better (and less annoying).

Next up... making the game end.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.