Sign in to follow this  
Reims

For those who know XNA...

Recommended Posts

Is it possible to draw a sprite after the Game class is ran? Of course it is, but how? I need to draw a sprite during the execution time. I have been testing with adding the sprites in the Game.Components, and putting this code inside the Draw method of the game: protected overridable Draw(GameTime gameTime) { spriteBatch.Begin(); foreach (Sprite sprite in Components) { sprite.Draw(gameTime); } spriteBatch.End(); base.Draw(gameTime); } It works but only if the Sprite was added into the Game.Components BEFORE the game is run. But I add a new sprite on the Game.Components after the game is ran, this is one is not drawn. I also tried putting Game.Tick() after I add a new sprite in the Components, to update and draw again every sprite (including the new one), but I receive this error message: "An unknown error has occurred. The result code was -2147467259". Help please =/ .

Share this post


Link to post
Share on other sites
Make sure you call Initialize on the components before you add them to the Components collection. Should work fine.

I'd guess that Game.Initialize does this on all installed components in its own Initialize. So if you add more after that, you'll have to initialize them yourself.

You'll probably have to call LoadGraphicsContent on them as well.

Share this post


Link to post
Share on other sites
No, it doesn't work. And thats kind of what I don't want to do, to make something manually. I don't want hard code.

What I want to do is really clear: Draw a sprite whenever I want. WITHOUT INSTANTIATING IT. Shouldn't that be easy?

Share this post


Link to post
Share on other sites
You could always not use the the component model at all and implement your own system. Then you have absolute control over when it's initialised and such.

In fact, I talked to an MS developer, and he admitted that the Game class isn't designed to be very flexible, and that it's perfectly acceptable and sensible to not be bound by it if you don't want to. As he put it, Game doesn't have any special knowledge of the Xna framework or do anything magical. It doesn't do anything you couldn't do yourself.

Granted, it does make setting up the device and content pipeline easier, so in your case I wouldn't totally abandon it, but you don't have to use xna's component system to render sprites if you don't want to.

(In my case, I don't use the Game class at all, as it isn't flexible enough to easily incorporate into an existing engine.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this