Google+

Wednesday, August 10, 2011

Let's Make A Game! Episode 5: The Answers You Seek

I finally got tired of not knowing what the arguments in those methods were. I was following along with the tutorial and hoping that they would explain them. I mean, any monkey can just type things into a box, but if you want to learn how to program, you need to learn the underpinnings of what you're typing.

I asked Matt, a programmer friend of mine, and he directed me to the place I should have looked in the first place: Google, or more specifically the method/argument documentation that you can find through there.

See, Microsoft documents EVERYTHING. If there's ever any file or command that you see and you don't know what it means, the MSDN more than like has what you need in spades.

For example, last time I was trying to figure out what this meant:
Vector2(GraphicsDevice.Viewport.TitleSafeArea.X,GraphicsDevice.Viewport.TitleSafeArea.Y +GraphicsDevice.Viewport.TitleSafeArea.Height / 2)
It looks pretty complex, right? Let's break down the structure of the Vector2 method. According to this page, here's what the command represents
Vector2 (Single, Single)
The first single is the x, or horizontal, value. The second is the y, or vertical value. Therefore, we can break down the above command a lot easier now.

The method:
Vector2
The X Value:
GraphicsDevice.Viewport.TitleSafeArea.X
The Y Value:
GraphicsDevice.Viewport.TitleSafeArea.Y+GraphicsDevice.Viewport.TitleSafeArea.Height / 2
For the X value, we're putting the player in the Viewport in the "TitleSafeArea," or first available safe place on the screen. That would be all the way on the left side of the screen, essentially at position 0.

For the Y value, we don't want to put the player in the first safe area on the screen. That would dump them on the bottom of the screen, meaning we'd be starting the player in the bottom left corner. It's kind of a sloppy place to start. Instead, we want to put them in the middle. How do we do that?

They chose to add two things: The first safe area (for the sake of argument, the number zero) plus HALF of the HEIGHT of the safe area. This way, no matter how big the screen is, the player will always end up in the middle of the screen. Pretty cool, huh?

It also explains a command in the next lesson about player movement. Here's the command that had me stumped:
MathHelper.Clamp(player.Position.X, 0,GraphicsDevice.Viewport.Width - player.Width)
"You want MathHelper.Clamps? YOU GET THE CLAMPS!"

So what does all this mean? Here's the syntax for the method:
public static float Clamp (
         float value,
         float min,
         float max
Basically, the Clamp command is keeping a value within certain parameters. This is what keeps us from taking the player off the screen accidentally.

So, breaking down the method, here's what the previous command means:

The Method:
MathHelper.Clamp
The value we're checking:
player.position.X
The minimum value we want to allow for X:
0
The maximum we want to allow for X:
GraphicsDevice.Viewport.Width - player.Width
All right so what does the last part mean? Basically, if we just say "GraphicsDevice.Viewport.Width is the maximum amount we want for X," we would have the right side of the player completely off the screen, since it appears we're determining X from the left side of the character.

However, if we subtract the width of the player off of the width of the screen, now the player will stay on the screen entirely instead of accidentally getting partly obscured.

Now that we're armed with this information, we'll be able to understand much more about what we're doing and where we're going.