XNA Game-Themed CS1 Examples ( XGC1 )

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.2.Input_Output_PrimitiveDataTypes
Example: Ex_4.InstanceVariableAssignment

Instance Variable Assignment


References:

Goals:



1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

Once we compile and run this project, the program displays " ThumbStick Position X:0 Position Y: 0 " at the top of the screen, and " Thumbstick as Vector2: {X:0 Y:0} " at the bottom of the screen.

If you move the left thumbstick towards the right, you'll see the X value change to be a positive number in response (on both the top and the bottom of the screen). If you move the left thumbstick towards the left, you'll see the X value change to a negative value. Similarly with moving the left thumbstick forwards & backwards (sometimes described as "up and down"), you'll see the Y values change appropriately. 


2. Background :  


The output of this tutorial is almost exactly the same as the output from the prior tutorial.  For this tutorial, we will continue to focus on variables, instead of output.  Since we examined local variables in detail in the prior tutorial, we'll be focusing on instance variables in this tutorial.

Because different information needs to be kept around for different lengths of time, there are different types of variables.  For example, a player's score should exist throughout the entire game, so we will need to use a long-lived instance variable to keep track of the score.  What we would like to have is a variable that will exist through the entire lifetime of the game .  We will use an instance variable to store this long-lived data.  What this means is that the space in the computer's memory that stores the instance variable will be found when the Game1 object is first created, and will continue to exist until the Game1 object is destroyed.  Since our Game1 object is our game, this means that any instance variables we create here will be available throughout the game. 


3. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen


FURTHER EXERCISES::  

  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without referring the tutorial's code.
    • Repeat this exercise daily for several days, so that you really get the hang of this.  As you go on, periodically review this by re-doing this exercise.
  2. State clearly what the difference between local and instance variables are, in terms of the lifetime of the variables.  Given an example of a situation where you'd want to use local variable (and also explain why an instance variable isn't a great choice for that situation), then give a situation where you'd want to (or need to) use an instance variable (and also explain why a local variable isn't a great choice for that situation)
  3. When talking about variables, we can discuss their scope .  Loosely put, a variable's scope is the region of source code in which we can use the variable (i.e., no compiler errors and no logical errors result from using the variable).
    In your own words, describe the scope of instance variables.
  4. Let's say that you have a program, which is included in the box below.  For each comment, you should be able to describe whether an instance variable (such as thumbX ) is in scope, or not.
    // Location A: Is thumbX in scope here?

    using System;

    using System.Collections.Generic;

    using Microsoft.Xna.Framework;

    using Microsoft.Xna.Framework.Audio;

    using Microsoft.Xna.Framework.Content;

    using Microsoft.Xna.Framework.GamerServices;

    using Microsoft.Xna.Framework.Graphics;

    using Microsoft.Xna.Framework.Input;

    using Microsoft.Xna.Framework.Net;

    using Microsoft.Xna.Framework.Storage;


    using XNACS1Lib;


    // Location B: Is thumbX in scope here?


    namespace InstanceVariableAssignment

    {


        // Location C: Is thumbX in scope here?


        public class Game1 : XNACS1Base

        {

            private float thumbX;

            private float thumbY;

            private Vector2 thumb;

            // Location D: Is thumbX in scope here?


            public Game1() : base ( new Vector2 (0, 0), 100.0f)

            {

                // Location E: Is thumbX in scope here?

            }


            protected override void InitializeWorld()

            {

                // Location F: Is thumbX in scope here?

                thumbX = 0.0f;

                thumbY = 0.0F;


                thumb = new Vector2 (0.0f, 0.0f);

                // Location G: Is thumbX in scope here?

            }


            protected override void UpdateWorld()

            {

                // Location H: Is thumbX in scope here?

                if (GamePad.ButtonBackClicked())

                    this .Exit();

                thumb = GamePad.ThumbSticks.Left;

                thumbX = GamePad.ThumbSticks.Left.X;

                thumbY = GamePad.ThumbSticks.Left.Y;


                // Location I: Is thumbX in scope here?

                EchoToTopStatus( "ThumbStick Position X:" + thumbX + " Position Y:" + thumbY);

                EchoToBottomStatus( "ThumbStick as Vector2: " + thumb);


            }

        }

        // Location J: Is thumbX in scope here?

    }

  5. Starting with the project that's used by the above tutorial, try leaving out the InitializeWorld code, and see if the program still works.  Feel free to comment the method out, so that you cant put it back in if you want. 
    Why does it still work (or not work)? 
    Why is it good to put in the initialization even if it's not strictly required?
  6. Starting with the project that's used by the above tutorial, try removing the private word on the instance variable declaration.  Does the program still work (i.e., does it compile?  Does it run without crashing?  Does it produce the same/correct results?).  
  7. Writing Code On Your Own: The Right Thumbstick

    Start this exercise using Exercise_ 6 's starter project , which is a nearly identical copy of the project that was used in the above tutorial.  Change the program so that instead of updating the top and bottom status bars based on the right thumbstick, instead of the left thumbstick.  You can access the right thumbstick using the phrase , GamePad.ThumbSticks.Right in the appropriate places.


Project home page : The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010