🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Ammo question

Started by
2 comments, last by frizb 24 years, 6 months ago
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)

- Remnant- (Steve Schmitt)
Advertisement
It might be best to keep track of your shots using a linked list.
William Reiach - Human Extrodinaire

Marlene and Me



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...

Still Learning...
Thanks. I'll give it a go this weekend.

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

Still Learning...

This topic is closed to new replies.

Advertisement