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


Just like in the previous chapter, we will now add movement to our hero. He'll walk over the ground plane and turn around when he reaches the edge of the screen.

The Hero3D class

We'll start by creating a Hero3D class that will encapsulate all the behavior for movement. This class will inherit from GameObject3D class, and just like Hero2D, we will use composition.

Fields

The class has three fields, the model, the direction in which the player will move, and the walk speed.

private GameModel _heroModel;
private int _direction = 1; //1 = Right / -1 = Left
private const int Speed = 75;

Initialize

In the Initialize method we will create a new GameModel instance and set its position.

_heroModel = new GameModel("Game3D/Vampire");
_heroModel.Position = new Vector3(0, -147, -100);

LoadContent and Draw

In the LoadContent and Draw methods we just call LoadContent and Draw methods on _heroModel.

Update

The update is where all the magic happens. In this method, we will calculate the...

lock icon The rest of the chapter is locked
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