Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building your First Mobile Game using XNA 4.0

You're reading from   Building your First Mobile Game using XNA 4.0 A fast-paced, hands-on guide to building a 3D game for the Windows Phone 7 platform using XNA 4.0

Arrow left icon
Product type Paperback
Published in Jan 2013
Publisher Packt
ISBN-13 9781849687744
Length 158 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Thomas Goussaert Thomas Goussaert
Author Profile Icon Thomas Goussaert
Thomas Goussaert
Brecht Kets Brecht Kets
Author Profile Icon Brecht Kets
Brecht Kets
Arrow right icon
View More author details
Toc

Adding movement to the hero


We have a nice static scene now, but it doesn't make much of a game. Let's add some movement to the hero and make him walk over the screen. When he reaches an edge, he will turn back automatically.

The Hero2D class

So we want to add movement to the player. It is best to encapsulate all that new behavior in a class. Let's call class the Hero2D. This class will be responsible for loading the texture, updating the position, and drawing the texture. Make sure the class inherits from GameObject2D class. We could make it inherit from the GameSprite class, but we won't do that. Instead we will add a field of type GameSprite. This is called composition .

In the context of composition, Herb Sutter has said the following:

Prefer composition to inheritance

The reasons why will become obvious when we implement the scene graph and when we make an animated sprite. For now, just go with it.

Fields

The class has three fields, a game sprite, a direction that determines if the player...

You have been reading a chapter from
Building your First Mobile Game using XNA 4.0
Published in: Jan 2013
Publisher: Packt
ISBN-13: 9781849687744
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image