310 likes | 404 Views
Chapter 14. Classes, Objects, and Games. XNA Game Studio 4.0. Objectives. Find out about making programs using software objects. Learn some software engineering terms and what they mean when we write programs. Use objects to add some new elements to our game easily. Lecture 2.
E N D
Chapter 14 Classes, Objects, and Games XNA Game Studio 4.0
Objectives • Find out about making programs using software objects. • Learn some software engineering terms and what they mean when we write programs. • Use objects to add some new elements to our game easily.
Classes and Structures • In C#, classes and structures are two different kinds of object. • They can both hold data fields and contain methods. • However, there are some crucial differences between the two. • One is that structures are managed in terms of value, whereas classes are managed in terms of reference.
Creating and Using a Structure • The BackgroundSpriteStruct structure was created to hold information about the background display in our game. • This is the simplest display element in the game; it just displays the tablecloth texture behind our game sprites
Creating and Using a Structure • Once the structure has been set up, the program can declare variables of this type: public BackgroundSpriteStruct Background;
Creating and Using an Instance of a Class • We can make a tiny change to the C# code shown previously by converting the background sprite to a class. classBackgroundSpriteClass { // rest of object just as before } • You cannot use a struct object the same way as a class object. • The program compiles correctly, but when we try to run it, the following exception is thrown: System.NullReferenceExceptionwas unhandled
Creating and Using an Instance of a Class • To understand what is happening, you need to know what is performed by this statement: BackgroundSpriteClassBackground; • What you actually get when the program obeys the statement is a new reference variable called Background. • This reference variable is allowed to refer to instances of the BackgroundSpriteClass. • You can think of a reference a bit like a luggage tag, in that it can be tied to something with a piece of rope.
Creating and Using an Instance of a Class • A program uses a reference by following the rope to the object it is tied to. • When you create a reference, you don’t actually get one of the things that it refers to, you just get a tag. • When the program runs and tries to follow the reference to get to an object, it fails, because the reference does not actually go anywhere. • A reference is initially set to the value null.
Creating and Using an Instance of a Class • You can solve this problem by creating an instance of the class and then connecting our tag variable to it. • The best place to do this is in the Initialize method of the game.
Constructors 5.02 Apply procedures to initialize objects using constructors. (5%)
Constructors are class methods that are executed when an object of a given type is created. Constructors have the same name as the class, and usually initialize the data members of the new object. Default constructor is a constructor with no parameters. A class can only have one default constructor. A class must have at least one constructor. What is a Constructor?
Parameterized constructors are constructors that have parameters. • A class can contain as many parameterized constructors as needed. • The parameter list must be unique for each. • Signature • The method name with its parameter list. Constructors
When you declare (instantiate) an object, the computer uses one of the class’s constructors to initialize that classes (object’s) private variables. If there is more than one constructor, the computer looks at the parameter list. The computer looks for a match of the order, data type, and number of parameters. Constructors
Your Constructor will use the same name as your class. Syntax for creating a constructorpublic className{ //initializes class variables } Constructors
Example – Default Constructorpublic className(){dblWoot = 0 //initializes class variables} Example – Parameterized Constructorpublic className(double dblWoot) {Woot = dblWoot; //Uses the parameter to //initialize class variable} Constructors
Constructing Objects • One Line classNameObj = new className(); Rectangle picRect = new Rectangle(); • Two Lines classNameObj;Obj = new className(); Rectangle picRect;picRect = new Rectangle();
Multiple References to an Instance Same Object – Two Tags
Multiple References to an Instance • Any changes made to temp would also change Background. • No Limit the number of references that can be attached to a single instance.
No References to an Instance • What happens if an object has no references to it?
No References to an Instance • The first instance is shown “hanging” in space, with nothing referring to it. As far as using data in the instance is concerned, it might as well not be there. • When your program is running, a special process called the garbage collector has the job of finding such useless items and disposing of them.
Why Bother with References? • A program can load textures, sounds, and other large objects and they can be left sitting in memory. • If a method needs to be given a particular object to work with (for example, if a large sound effect needs to be played), the method can be supplied with a reference to the sound to be used. • A downside with using references is that whenever a program wants to use an actual value, it needs to follow the reference to get to the item. • This can slow a program down slightly.
Value and Reference Types • Two kinds of objects • Those managed by value • Those managed by reference • Objects managed by value include all the low-level data types such as int, float, and double, along with slightly more complex XNA data types such as Color. • Any object created as a C# struct is also managed by value.
Value Types • By default, whenever a program does something with an object that is managed by value, the value of that object is used. • The assignment operation copies the value of a variable from one to another. • Value types are used in situations where you are working with small amounts of data (for example, numeric values), and the effort of following references would slow things down.
Reference Types • Objects managed by reference include large and complex types such as the SoundEffect and Texture2D items in an XNA game. • The reference assignment makes the variable being assigned to refer to the same object as the source. • Whenever a program does something with an object that is managed by reference, a reference to that object is used. • This means that large objects can be used within a program without the effort of actually copying their contents around in memory.
Should Our Game Objects Be Classes or Structures? • Classes are objects that are managed by reference. • Structures are objects that are managed by value.
Value Argument • Game Objects Should Be Structures Managed by Value. • Value types are a good idea when the objects don’t hold much data, your program makes heavy use of them, and you want to have lots of them in memory. • The objects in our game are actually quite small, only a few tens of bytes in size, and a game could have hundreds, perhaps thousands of them on the screen at once. • All the game objects are updated and drawn 60 times a second, and so they are used a lot. • This would be much quicker if the program didn’t have to waste time following references to find each one.
Reference Argument • The Great Programmer explains that if you use classes, you can build a class hierarchy where you can create a child class that inherits the behaviors of a parent. • The great thing about this, she says, is that you only have to write a behavior inthe parent and then the child class can pick this up and just has to add any new behaviors that it needs. • In other words, it is much easier to reuse code.
Reference Argument • Using a class hierarchy means your code is lines shorterand much simpler. • Using classes makes adding new game elements really easy.