Game Development Reference
In-Depth Information
set
{
if ( value != Color.Red && value != Color.Green && value != Color.Blue)
return ;
color = value ;
if (color == Color.Red)
currentColor = colorRed;
else if (color == Color.Green)
currentColor = colorGreen;
else if (color == Color.Blue)
currentColor = colorBlue;
}
So if someone would execute the following instruction:
cannon.Color = Color.Black;
the color would not change. You might wonder why this is useful. After all, you are
the one writing the instructions that use this class, right? That is true in this case, but
if you plan to work in a team with other developers later on (which is quite normal
in the game industry), then the developers that use your classes may do things that
they are not supposed to do. By building these kinds of security into your classes,
you know for certain that the cannon will never be any other color than red, green,
or blue. The problem in this case is that there is no way to warn the other developers
that they are not supposed to use any other colors. There are several ways to solve
this: you can either document the conditions in the software documentation, but
there is also another way, which depends on a coding mechanism called exception
handling . For now it is too early to get into that topic, but later on in this topic, we
are going to see how this is done.
Now that we have this Color property, we can use it in the HandleInput method to
manipulate the current color of the cannon object:
if (currentKeyboardState.IsKeyDown(Keys.R) &&
previousKeyboardState.IsKeyUp(Keys.R))
cannon.Color = Color.Red;
else if (currentKeyboardState.IsKeyDown(Keys.G) &&
previousKeyboardState.IsKeyUp(Keys.G))
cannon.Color = Color.Green;
else if (currentKeyboardState.IsKeyDown(Keys.B) &&
previousKeyboardState.IsKeyUp(Keys.B))
cannon.Color = Color.Blue;
7.4.5 Adding More Properties
Now that we are at it, we could add a few more properties to the Cannon class to
access other data members. For example, we also want to be able to modify the
Search WWH ::




Custom Search