XNA 4.0 organization question

Started by
5 comments, last by OneSidedDice 12 years, 7 months ago
I've just recently installed XNA, and after looking through it, a question came to mind: Are you suppose to fit any entire game 5 methods provided to you? Is it possible to draw graphics and such from within, say, an enemy object instead of within the Draw() method inside the main game object?
Advertisement

I've just recently installed XNA, and after looking through it, a question came to mind: Are you suppose to fit any entire game 5 methods provided to you? Is it possible to draw graphics and such from within, say, an enemy object instead of within the Draw() method inside the main game object?


Yes, of course it is. Once you get a bit more experienced with programming this will all appear pretty obvious to you.

I think you are a bit confused by the principles in front of you. XNA is an API, not a programming language - the language is C#. What you get when you use the XNA Game Project wizard is the crude skeleton of a game in XNA. You are under no obligation to use it, but most people do. You could potentially write the whole thing from scratch if you wanted.

There is no obligation to use only the 5 methods it starts with, no part of the framework or the compiler will try to force you to do that.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!

[quote name='OneSidedDice' timestamp='1314483635' post='4854567']
I've just recently installed XNA, and after looking through it, a question came to mind: Are you suppose to fit any entire game 5 methods provided to you? Is it possible to draw graphics and such from within, say, an enemy object instead of within the Draw() method inside the main game object?


Yes, of course it is. Once you get a bit more experienced with programming this will all appear pretty obvious to you.

I think you are a bit confused by the principles in front of you. XNA is an API, not a programming language - the language is C#. What you get when you use the XNA Game Project wizard is the crude skeleton of a game in XNA. You are under no obligation to use it, but most people do. You could potentially write the whole thing from scratch if you wanted.

There is no obligation to use only the 5 methods it starts with, no part of the framework or the compiler will try to force you to do that.
[/quote]

I understand that much, I'm not new to C#. What I mean is, when you start a new project using the XNA template, there are 7 (I think it was) methods within the main game class. Every tutorial so far has told me to put my graphics drawing code in the Draw() method. I was wondering if I could make multiple Draw() methods? That way each object can draw graphics, instead of just this 1 method. So far, I haven't gotten it to work.

edit: I looked around in the XNA code. The Draw() method is overriding another method. I don't think I can override the same method twice in another object, which leads me to believe I cannot create multiple draw methods.

That way each object can draw graphics, instead of just this 1 method. So far, I haven't gotten it to work.


What are you doing?

it looks something like this right?

public class Enemy
{
//stuff
public void Draw(SpriteBatch sb)
{
sb.Draw(EnemyTexture, Position, Color.White);
}
}

public class Game1 : Game
{
//stuff
protected override Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);

spriteBatch.Begin();
enemy.Draw(spriteBatch);
spriteBatch.End();

base.Draw(gameTime);
}
}

public class Enemy
{
//stuff
public void Draw(SpriteBatch sb)
{
sb.Draw(EnemyTexture, Position, Color.White);
}
}

public class Game1 : Game
{
//stuff
protected override Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);

spriteBatch.Begin();
enemy.Draw(spriteBatch);
spriteBatch.End();

base.Draw(gameTime);
}
}


I am trying to "duplicate" the Draw() method. See how you had to put "enemy.Draw(spriteBatch);" in the Game1 class' Draw() method? Is it possible I can give objects their own Draw() method so that I can draw the graphics IN that object?

I am trying to "duplicate" the Draw() method. See how you had to put "enemy.Draw(spriteBatch);" in the Game1 class' Draw() method? Is it possible I can give objects their own Draw() method so that I can draw the graphics IN that object?


No, sometime during your Game1.Draw() the enemy.Draw() function has to be called to be drawn. Now you don't have to make that call directly in the Game1.Draw() function, you can have an EnemyManager class that has a Draw(SpriteBatch) function and in that function you would draw the enemies. Though you would still have to do "enemyManager.Draw(spriteBatch);" in the Game1.Draw() function.

[quote name='OneSidedDice' timestamp='1314492404' post='4854597']
I am trying to "duplicate" the Draw() method. See how you had to put "enemy.Draw(spriteBatch);" in the Game1 class' Draw() method? Is it possible I can give objects their own Draw() method so that I can draw the graphics IN that object?


No, sometime during your Game1.Draw() the enemy.Draw() function has to be called to be drawn. Now you don't have to make that call directly in the Game1.Draw() function, you can have an EnemyManager class that has a Draw(SpriteBatch) function and in that function you would draw the enemies. Though you would still have to do "enemyManager.Draw(spriteBatch);" in the Game1.Draw() function.
[/quote]
I was afraid I might have to do that. A manager object would definitely help out with organization. Thanks for the help everyone.

This topic is closed to new replies.

Advertisement