# XNA: My bullets lag the game the first time they're fired, but not after

I'm working on a SpaceWar-esque project to get comfortable with XNA. I have a Ship DrawableGameComponent and a Bullet DrawableGameComponent. The player's Ship is created at the start of the game, and the Bullets are created for each Ship as part of its constructor.

In my Ship constructor, I have the following code:

for (int i = 0; i < 5; i++)
{
activeBullets[i] = new Bullet(game);
}


The relevant part of my Ship's shoot() function is as follows:

activeBullets[bullet].Visible = true;
activeBullets[bullet].Enabled = true;
activeBullets[bullet].position = this.position;


The first five times I shoot() (that is, once for each bullet object that the ship is allotted), there is lag that I think is due to the bullet sprite being loaded. I've tried setting Visible and Enabled to true in the ship constructor loop where the Bullets are instantiated, but the first-five-shots lag persists. Any shots beyond the first five are completely lag-free.

How do I load the Bullets properly ahead of time so that there is no lag on the first shots?

• Load the bullet texture in LoadContent()? – BlueRaja - Danny Pflughoeft Jul 26 '11 at 4:16
• Bullet's texture is loaded in bullet's LoadContent(). Shouldn't this happen when the Bullet() is instantiated, or barring that, Enable'd in the Ship constructor? – Bill Jul 26 '11 at 4:34
• Sorry, should have read your post a bit closer. I'll post a real answer. – BlueRaja - Danny Pflughoeft Jul 26 '11 at 5:02

DrawableGameComponent's come with a certain amount of overhead - aside from the memory, every time you add a component to the collection it must be resorted (because components have a DrawOrder). You don't want to be constantly adding and removing components.

For this reason, you don't want Bullet to be a DrawableGameComponent. Instead, you should make something like a ProjectileManager, which loads all the projectiles (including their textures), and manages creating/destroying/updating/drawing them. This simultaneously solves your texture-loading problem, as the textures can (and should) be loaded only once, in LoadContent().

• @Bill: You could do that, but it comes with the obvious limitation of only allowing five bullets on the screen at once; a much better solution would be to create a ProjectileManager class. In either case, LoadContent() is called after all components have been added to the collection (in Initialize()), before the game starts. Override it and load your content there. Store the texture in a private field. – BlueRaja - Danny Pflughoeft Jul 26 '11 at 5:56