Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Shooting only hits for the first or last bullet 2d space shooter

    Coding
    4
    25
    2257
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • poof92
      poof92 last edited by

      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!
      20211124_214551.jpg

      1 Reply Last reply Reply Quote 0
      • vinicity
        vinicity F last edited by vinicity

        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!

        1 Reply Last reply Reply Quote 1
        • Ben 2.0
          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.

          1 Reply Last reply Reply Quote 1
          • poof92
            poof92 last edited by

            Could I possibly get a simple example?

            1 Reply Last reply Reply Quote 0
            • Ben 2.0
              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.

              1 Reply Last reply Reply Quote 1
              • poof92
                poof92 last edited by

                Ok sounds good thank you I will also continue to try some things

                1 Reply Last reply Reply Quote 2
                • vinicity
                  vinicity F last edited by vinicity

                  If you want to look at an example, check out my last game jam entry, Parker Pumpkin'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.

                  https://fuzearena.com/catalogs/view/4104

                  1 Reply Last reply Reply Quote 0
                  • poof92
                    poof92 last edited by

                    Ok thank you I'll give it a go

                    1 Reply Last reply Reply Quote 1
                    • poof92
                      poof92 last edited by

                      I'm sorry I can't seem to get it to work

                      1 Reply Last reply Reply Quote 0
                      • vinicity
                        vinicity F last edited by

                        Share your code and someone can take a look at it. We are all kind of busy with the game jam right now, though...

                        poof92 1 Reply Last reply Reply Quote 0
                        • poof92
                          poof92 @vinicity last edited by

                          @vinicity my code is f1563mndsg

                          1 Reply Last reply Reply Quote 1
                          • waldron
                            waldron F last edited by

                            Been meaning to take a look bud but been fighting for time at the moment, hang in there your be shooting enemies left right and center soon enough 😁

                            poof92 1 Reply Last reply Reply Quote 0
                            • poof92
                              poof92 @waldron last edited by

                              @waldron alright sounds good thanks a ton

                              1 Reply Last reply Reply Quote 0
                              • waldron
                                waldron F last edited by

                                Took a look, I'v started writing out some helpful create/remove sprite functions at the top should hopefully share it tomorrow just want to make it clear for you to understand 🙂

                                1 Reply Last reply Reply Quote 1
                                • poof92
                                  poof92 last edited by

                                  Awe thanks so much

                                  1 Reply Last reply Reply Quote 0
                                  • poof92
                                    poof92 last edited by

                                    Awesome is what I meant to say lol

                                    1 Reply Last reply Reply Quote 2
                                    • waldron
                                      waldron F last edited by waldron

                                      9gy43mnd5u
                                      project: SSU-multiple sprite detection
                                      Plus sprite creation/removal

                                      poof92 1 Reply Last reply Reply Quote 2
                                      • poof92
                                        poof92 @waldron last edited by

                                        @waldron wow this is awesome thanks so much

                                        1 Reply Last reply Reply Quote 1
                                        • Ben 2.0
                                          Ben 2.0 last edited by

                                          Sorry the demo I promised still isn't finished. I have been extra busy with school work lately so I have not had much time. If you still want it I could try to finish it by the end of the week.

                                          1 Reply Last reply Reply Quote 1
                                          • poof92
                                            poof92 last edited by

                                            Hmm the code doesn't work

                                            waldron 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post