Archived

This topic is now archived and is closed to further replies.

Ammo question

This topic is 6558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In a nutshell : yes.

In several paragraphs :

Basically what you want to do is make it so that when your guy fires his weapon, he creates a new sprite for the ammo and attaches it to a bullet linked list of his. This allows you to have as many bullets as you want on screen at once.

An optimization can be made : instead of new and deleteing the sprite headers, have a seperate linked list of free bullets. On starting the game, "new" and place into the list a reasonable number of bullet sprite structures. Then, when your guy needs to shoot his weapon, you simply retrieve&remove from the free list and add it to his own list. When the bullet expires, you remove it from his list and add it to the free list. This removes all the memory-management overhead that new and delete would impose. And since adding a element (and removing a node if you already are at that element) in a list is a constant-time operation, your list management imposes no performance hit. Not too big a deal for small numbers, but if you have hundreds or more of bullets flying around, its a big deal. If you go to fire a weapon and the free bullet list is empty, then -- and only then -- do you go ahead and "new" a new sprite header. When its done, it will then go back into the free bullet pool, so that the list will expand depending on what the max bullets is you've been using so far this game session.

------------------
- Remnant
- (Steve Schmitt)

Share this post


Link to post
Share on other sites

Here's an easy question. My sprite has been set up so depending on his direction he can fire his weapon in that direction. No big deal. My problem is that if I fire again before the ammo leaves the screen the ammo disappears because it resets to a starting point dictated by the sprite's x position and basically starts over. I just included seperate x,y, dx, & dy variables to use with the player and the ammo. This seems to be my final stumbling block before I get to add sound to my game. Should I make a class out of just the ammo and use "new" every time the fire button is hit?

------------------
Still Learning...

Share this post


Link to post
Share on other sites