Shooting only hits for the first or last bullet 2d space shooter
I'm working on a 2d space shooter I've been having alot of fun but I ran into a snag I want to shoot multiple bullets and I've succeeded but now only the first and the last bullet seem to hit the ship!
What you are doing there is that you create ten sprites in the loop, but you put the handle to each one of them in the same variable (Bullet), overwriting the previous one.
There are two problems with that:
First of all, you create lots of sprites, but then you lose the handles to them. The sprites still remain in memory though, so for each bullet you create, it will eat up more and more Fuze memory. After a sprite is no longer useful, you need to destroy it with
removeSprite(). Doing that returns resources and memory taken up by that particular sprite to be used again.
Secondly, since you lose the handles to all sprites but the most recently created one, you can not do any collision detection for them.
So, the solution is to instead have an array to store your sprites in. Then, when doing collision detection, you can loop over the array and perform your detection code for each bullet sprite. There are many ways to solve this, one could for instance have a large fixed array that you populate with bullet sprites at the start, and then you have a sprite property called .active or similar that you set to true when the bullet is fired.
Or one could have an array with dynamic length that just contains the visible shots. The tricky part here is that it is easy to add new items to an existing array, but a bit trickier to remove items, but it is certainly doable as well. If you are doing it this way, you need to use
removeSprite()when the bullet hits or leaves the screen, and then remove it from the array.
So, in conclusion, read up on arrays, and every sprite that is created with
createSprite()either needs to be reused later or destroyed with
removeSprite()when no longer needed.
Feel free to ask if you need further assistance!
Ben 2.0 last edited by
If you want to see the arrays that @vinicity is talking about, I suggest looking at some gamejam projects that have multiple of similar sprites. Gamejam games are simple so you should be able to understand the logic better and many of them feature this concept.
Could I possibly get a simple example?
Ben 2.0 last edited by
I could work on something that might help. Might be a couple of days though as I am focused on the Gamejam.
Ok sounds good thank you I will also continue to try some things
If you want to look at an example, check out my last game jam entry, Parker Pumkin’s 115th Dream
Look at how I handle the array called enemies. The enemies in that game are basically bullets shot from the corners of the screen. When they fly outside the screen I use removeSprite and then remove them from the enemies array.
Ok thank you I'll give it a go
I'm sorry I can't seem to get it to work
Share your code and someone can take a look at it. We are all kind of busy with the game jam right now, though...