Reputation: 3509
I want to create a simple game where my character can walk, jump, shoot. But I got stuck on how to call the update method from another class? At the moment all of my code is in Game.cs (the main class), but I want to create different classes, depending on my needs. How can I do this?
Upvotes: 1
Views: 2502
Reputation: 14153
Your update code is called many times each second automaticly (Depending on your frame rate). Unlike regular C# programming that uses events and must update on its own, in XNA your update code will loop every few milliseconds.
If you want to have other classes make their own updates you can use a GameComponent, or simply create a method in another class.
Here is an example of your main class:
public class Game1 : Game
{
Level level;
protected override void Initialize()
{
// TODO: Add your initialization logic here
level = new Level();
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
level.Update(gameTime);
base.Update(gameTime);
}
And your other Level
class
public class Level
{
Vector2 CharacterPosition;
public Level()
{
}
public void Update(GameTime gameTime)
{
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
//Move the character
ChactacterPosition.X += elapsed * speed;
}
}
Upvotes: 1
Reputation: 26
you could create custom classes with an update and a draw method to call on every update from game.cs. this is the best place to start learning xna:
http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/getting_started
if you go through the first parts of the tutorial, you'll get exactly the examples you need for your question.
Upvotes: 0