chapter 14 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 14 PowerPoint Presentation
play fullscreen
1 / 68
Download Presentation

Chapter 14 - PowerPoint PPT Presentation

Download Presentation

Chapter 14

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 14 Classes, Objects, and Games XNA Game Studio 4.0

  2. Lecture 3

  3. 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.

  4. Methods in Classes 5.03 Apply procedures to create methods and instantiate an object from a class. (5%)

  5. Methods are also known as subs, sub-routines, and functions. • There are several types of methods. • Accessors • Gets information about your object’s properties. • Mutators/Modifiers • Change or set your object’s properties or state. • Helper • A method only used within the class – not really used by the object. • Immutable • Special classes that have NO mutator methods • String Class is an immutable class. Methods

  6. Method Signatures • The method signature is the header of the method. • It specifies • Access level • Return type, if any • Name of the method • Parameters, if any Using Methods in Classes

  7. Every method is written in basically the same form.access_modifierreturn_type name (parameters) • Access Modifier • Public – accessible outside the class • Private – accessible only within the class • Return Type • If a value is to be returned, the Return_type is the data type of that value. • If there is no value to be returned, the return_type is void. • Name • You should name the method appropriately to identify its function • Parameters • These are the values that are to be passed in the “call” to the method. Method Syntax

  8. When you specify a return type you will add a return statement. Method Header Example: public string Signature() { return “Created by J Smith”; } Return data type and what is returned MUST match. Methods that Return a Value Return Type

  9. When a method returns a value, the method call must be part of an assignment statement. Method Call Example:lblResult.Text = Signature(); Methods that Return a Value

  10. Methods with Parameters 5.04 Apply procedures to create class methods with value, reference and default parameters. (5%)

  11. Parameters define the values you wish to pass to the method. • There are two ways to pass parameters • By Value • The value is sent to the method. • The method can manipulate that value however the value stays in the method. It cannot be passed back. • By Reference • The values is sent to the method • The method can manipulate that value AND send that value back. Parameters

  12. Think of a value parameter as a one-way street. • The value can only move one way. • Think of a reference parameter as a two-way street. • The value can pass over, then pass back. • Java has call by value Value vs. Reference Parameters

  13. public class Dog{ private string name; public void setName(string nm) { name = nm; } public string getName () {return name; } Example

  14. Parameters provide information to a method that is necessary to do its job. The method call provides the values – called actual arguments. You will add the “formal arguments” or “parameters” to the method header. public void Signature(string Name ) Methods with Parameters

  15. Method: public void Signature(string Name) {lblResult.Text = “Written by: ” + Name; } Call: Signature(strName); The arguments and parameters MUST match in order, data type and number. Methods with Parameters

  16. This is the default way to pass arguments in C#. • A copy of the value is made and sent to the method. • Any changes are not passed back to the call. • Think of this as a one way street; the value only moves in one direction. Pass-By-Value

  17. The method has the ability to access and modify the original variable from the call. • Reference-type variables store references to objects. • Think of it as sending the actual address of the object for the method to access the value. The method can then manipulate that value and send it back. Pass-By-Reference

  18. To pass a variable by reference use the keywordref. • Apply the ref keyword to the parameter declaration allows you to pass that variable by reference. • The ref keyword is used for variables that already have been initialized in the calling method. • It must be initialized, otherwise the compiler will generate an error. Pass-By-Reference

  19. Using Methods in Classes

  20. Remember, an object is made up of properties/attributes and methods that allow you to change those properties. • Syntax objectName.methodName (arg, arg, …) • Arguments are listed in the appropriate order that matches the order, number and data type of the parameters in the method signature. Using a Method in Classes

  21. Value & Reference Parameter Example

  22. Default parameters are optional. The method can be called with or without that parameter defined. Always default parameters should be placed to the right. Default Parameters in Classes

  23. Overloading methods 5.05 Apply procedures to overload class methods. (3%)

  24. You can create a method with the same name as long as the parameters are different in one of the following ways • Number of parameters, • Order of the parameters, • Data type of parameters Overloading Methods

  25. For example, in the student class, you could have the following methods:public void addGrade(double grade)public void addGrade(int grade) The compiler can tell the difference between the data types when the method is executed. Overloading Methods Examples

  26. //Overloaded Method public void setRatingLevel (string l) { level = l; } //Overloaded Method public void setRatingLevel (int r) { rating = r; } Game Example

  27. Applying methods in xna classes

  28. Creating a Sprite Class Hierarchy 6.01 Apply inheritance and composition to create derived classes. (5%)

  29. The BaseSpriteClass • Here is the base class – Note it does not do much. Method Accessor: public Return: void Name: LoadTexture Parameters: Texture2D inSpriteTexture

  30. The BaseSpriteClass • The BaseSprite is the simplest type of sprite. It contains the bare minimum of sprite behaviors. • It can be given a texture and a destination rectangle and be asked to draw itself. • It also declares an update behavior, although in this version of the sprite, it doesn’t do anything. • It serves as the starting point for all the sprite classes.

  31. The BaseSpriteClass • Use the BaseSpriteto Store the Background. • The BaseSprite class is perfect for the game’s background. • This is created, set to the size of the display, and then drawn at the start of each call of the game’s Draw method.

  32. Extending the BaseSpriteto Produce a TitleSprite • Exactly the same as the BaseSprite, except that it has an Update behavior that checks to see if the player has pressed the A button on the gamepad.

  33. Extending the BaseSpriteto Produce a TitleSprite • Note there is only one method – Update. • The rest are inherited from the parent sprite class. • Syntax to extend a base classpublic class NewClass:ParentClass public class TitleSprite:BaseSprite

  34. Overriding Methods from a Parent Class • We need provide a replacement Update method that works for the TitleSprite. • The empty method in the BaseSprite class has been marked as virtual, as shown here in bold. • A method that is virtual can be overridden by a method with the same name in a child class.

  35. Abstract Class & Interfaces 6.05 Understand Overriding and Shadowing. (3%)

  36. What is Overriding? • The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event. • An override method provides a new implementation of a member that is inherited from a base class. • The method that is overridden by an override declaration is known as the overridden base method. • The overridden base method must have the same signature as the override method.

  37. Using Overriding • In the Base Class public virtualvoid Draw(SpriteBatchspriteBatch) • Note the use of the keyword virtual. • An override declaration cannot change the accessibility of the virtual method. • Both the override method and the virtual method must have the same access level modifier.

  38. Using Overriding • In the derived class public override void Draw(SpriteBatchspriteBatch) • This method will take precedence over the base class Draw method when called by an object constructed from the derived class.

  39. Shadowing • Shadowing is the same as “Hiding” in C#. • Shadowing uses the newkeyword • In the derived class public new void Draw(SpriteBatchspriteBatch) • When used as a modifier, the new keyword explicitly hides a member inherited from a base class. • When you hide an inherited member, the derived version of the member replaces the base-class version.

  40. Shadowing Example public class BaseC { public int x; public void Invoke() { } } public class DerivedC : BaseC { newpublic void Invoke() { } }

  41. Overriding Methods from a Parent Class • When a program calls the Update method on a reference to a TitleSprite instance, this Update method is used instead of the one in BaseSprite. • In other words, the TitleSprite class can contain a new version of the Update method that behaves in the way it needs. Note the keyword override

  42. Building a Class Hierarchy • Next in the hierarchy is the child class MovingSprite. • These are sprites that need to move around the screen. • They have extra properties and methods that allow them to be set. • The BatSpriteand the BallSprite • The only method that differs between these classes is Update. • In the BatSpriteclass, the Update method reads the gamepad and uses it to control the position of the bread bat. • In the BallSpriteclass, the Update method bounces the cheese ball around the screen and checks for collisions between the ball and other game objects.

  43. Using Protected Members from a Parent Class • If the parent class contains private members, they are not visible to code in the child classes. • Members of a class can be marked as protected, which means that they are visible to code in children of the class, but not to any code in classes outside the hierarchy.

  44. Adding a Deadly Pepper • How the deadly pepper works • Sometimes the pepper is green, at which point it is harmless, but at other times it is red. • If the bread bat collides with the pepper when it is red, the player loses a life. • Shooting the pepper with the cheese when it is red gains 50 points and turns the pepper green again.

  45. Creating a DeadlySpriteClass • There is a class called MovingSpritethat provides all the elements required to make a sprite that moves across the screen. • This includes working out the size of the sprite and how fast it should move. • Start by extending the MovingSpriteclass to make a new class called DeadlySprite. • This needs to contain an extra data field that records whether or not the pepper is deadly. • This needs to contain an extra data field that records whether or not the pepper is deadly. private boolisDeadly;

  46. Drawing the Deadly Pepper Sprite • Updated Draw • Pepper is red if deadly, otherwise green • The first thing you do is convert your image of the pepper to black and white.

  47. Setting Up the Deadly Pepper Sprite • Next, provide the method that sets up the pepper at the beginning of the game. • The MovingSpriteclass provides a method called StartGameto set up a moving sprite. • It works out the size of the texture to use, and also calculates the speed of movement. • Override the StartGamemethod in MovingSpriteand replace it with one that does everything the parent method does, plus the action of setting isDeadlyto false.

  48. Setting Up the Deadly Pepper Sprite • If you start to override a method, XNA Game Studio provides Intellisense to help you choose the method to override. • When you type “public override” into the code inside the DeadlySprite class, XNA Game Studio shows you a menu of methods that can be overridden.

  49. Setting Up the Deadly Pepper Sprite • Select that from the list and press Enter. • XNA Game Studio makes an empty version of the method to get you started • The basekey word is used to call the method that has been overridden.

  50. Setting Up the Deadly Pepper Sprite • You don’t want to have to replace the entire StartGamemethod; you just want to add something to set the isDeadlyvalue. • The base keyword means that you can use the behavior of the parent method and then add something extra.