| XNA Game-Themed CS1 Examples (XGC1) | |
|
Release 2.0 (XNA V3.1) |
|
References:
Goals:
1. Obtain the example code
When the game starts, you'll see a screen that
looks similar to this:

As you can see, this program draws five soccer balls across the screen. The program also draws a basketball on the screen, and allows you to move the basketball using the right thumbstick. You may also move the fifth soccer ball using the left thumbstick. The major change for the player in this game is that pressing the A button both increments the value stored in element #0 in the array, and also adjusts the size of soccerball #0 to reflect that value. Similarly, the B button controls the second array element and soccer ball, the X button the third, and the Y button the fourth.
2. Examining The Program:
Let's examine the
C# source code that produces the behavior we see on-screen
|
// Notice the way array is declared in C# private int[] m_MyArray; // an array of Circles to represent the SoccerBalls private XNACS1Circle[] m_SoccerBalls; |
|
// initialization for the soccer balls float xOffset = 0.0f; // Allocate memory for the soccer ball array m_SoccerBalls = new XNACS1Circle[ARRAY_SIZE];// here is the for loop and accessing the array for (int i = 0; i < ARRAY_SIZE; i++) { m_SoccerBalls[i] = CreateABallAt(xOffset + BALL_INIT_X, "SoccerBall");m_SoccerBalls[i].Label = m_MyArray[i].ToString(); xOffset = xOffset + SPACE_BETWEEN_BALLS; } |

This creates a new XNACS1Circle
object, initializes that object with a starting location, radius, and
texture (image), and then returns a reference to that object back to
InitializeWorld, which then assigns it to the zeroth element of the array.
Once this line has finished, the variables will look like the picture
below (Note that the Label of the new object is not yet set)

is executed. Since
the value of i is currently 0,
m_MyArray[i] means the same thing as
m_MyArray[0]. Calling the
ToString() method on the integer stored in element zero of the m_MyArray
array will convert the number which is stored there (which is 5,
currently) to a string, which will then be assigned to the 'Label'
property of the new object. Thus, the new object will look like:


This creates a new XNACS1Circle object, initializes that object with a starting location, radius, and texture (image), and then returns a reference to that object back to InitializeWorld, which then assigns it to the first element of the array.
is executed. Since the value of i is currently 1, m_MyArray[i] means the same thing as m_MyArray[1]. Calling the ToString() method on the integer stored in element one of the m_MyArray array will convert the number which is stored there (which is 5, currently) to a string, which will then be assigned to the 'Label' property of the new object.

|
#region
control the soccer balls: 0 to 3 with buttons: A, B, X, Y and 4's
location with left thumbstick String msg = "Clock on buttons to increment array: A-0 B-1 X-2 Y-3!"; EchoToTopStatus(msg); int nth = -1;if (GamePad.ButtonAClicked()) nth = 0; if (GamePad.ButtonBClicked()) nth = 1; if (GamePad.ButtonXClicked()) nth = 2; if (GamePad.ButtonYClicked()) nth = 3; if (nth >= 0) { m_MyArray[nth] += 1; m_SoccerBalls[nth].Radius = m_MyArray[nth]; m_SoccerBalls[nth].Label = m_MyArray[nth].ToString(); }
// control location of soccer-4 with right thumbstick m_SoccerBalls[4].CenterX += GamePad.ThumbSticks.Right.X; m_SoccerBalls[4].CenterY += GamePad.ThumbSticks.Right.Y; #endregion |
m_SoccerBalls[4].CenterX += GamePad.ThumbSticks.Right.X;
m_SoccerBalls[4].CenterY += GamePad.ThumbSticks.Right.Y;
if (GamePad.ButtonAClicked())
nth = 0;
if (GamePad.ButtonBClicked())
nth = 1;
if (GamePad.ButtonXClicked())
nth = 2;
if (GamePad.ButtonYClicked())
nth = 3;
{
m_MyArray[nth] += 1;
m_SoccerBalls[nth].Radius = m_MyArray[nth];
m_SoccerBalls[nth].Label = m_MyArray[nth].ToString();
}
for the purposes of this tutorial, let's assume that the user has pushed
the X button, and that all the variables and objects look like so:

will use the current value of nth (which is 2), to retrieve the value stored at m_MyArray[2] (which is 5), then add one to that number (which is then 6), then store that number back into m_MyArray[2].
will use the current value of nth (which is sitll 2), to retrieve the value stored at m_MyArray[2] (which is now 6), convert that integer to a string, and then assign that to the Label property of the circle object referred to by slot 2 of the m_SoccerBalls array.

FURTHER EXERCISES: