chapter 14 n.
Skip this Video
Download Presentation
Chapter 14

Loading in 2 Seconds...

play fullscreen
1 / 31

Chapter 14 - PowerPoint PPT Presentation

  • Uploaded on

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Chapter 14' - kail

Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 14

Chapter 14

Classes, Objects, and Games

XNA Game Studio 4.0

  • 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 structures1
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
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 structure1
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
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.



// 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 class1
Creating and Using an Instance of a Class
  • To understand what is happening, you need to know what is performed by this statement:


  • 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 class2
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 class3
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.

5.02 Apply procedures to initialize objects using constructors. (5%)

what is a constructor

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.

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.


Your Constructor will use the same name as your class.

Syntax for creating a constructorpublic className{ //initializes class variables }


Example – Default Constructorpublic className(){dblWoot = 0 //initializes class variables}

Example – Parameterized Constructorpublic className(double dblWoot) {Woot = dblWoot; //Uses the parameter to //initialize class variable}

constructing objects
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
Multiple References to an Instance

Same Object – Two Tags

multiple references to an instance1
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
No References to an Instance
  • What happens if an object has no references to it?
no references to an instance1
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
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
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
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
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
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
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
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 argument1
Reference Argument
  • Using a class hierarchy means your code is lines shorterand much simpler.
  • Using classes makes adding new game elements really easy.