30 Replies Latest reply on Oct 17, 2008 8:00 PM by kglad

    Limited Ammo

    coolburn
      I have a shooting game that I would like to limit the ammo and have the game end after the last shot is fired. I'm new to using actionscript and I'm using flash 5. The game is an example used in a book that I have but it doesn't include how to limit the ammo. I have attached the code that is used on a frame in the main timeline. The rest of the code is in a movieclip and button that are placed off the stage. Thank you for any help you can give me with this problem.


      coolburn
        • 1. Re: Limited Ammo
          kglad Adobe Community Professional & MVP
          create a maxBullets variable, assign it a number and use an if-statement in shootBullet() to check if nextBullet exceeds maxBullets.
          • 2. Re: Limited Ammo
            coolburn Level 1
            First, thanks klad for your help. I did what you said and it seems to work but I'm having a new
            problem with this game. After you have fired all of the bullets and the game ends, some of the
            bullets that have been fired and the balloons that have not been hit, stay on the screen, even
            after you restart the game. How can I fix this problem? Thanks for any help you can give me. I
            have attached the code that I changed, in case it's something that I did.

            coolburn
            • 3. Re: Limited Ammo
              kglad Adobe Community Professional & MVP
              to remove those movieclips call the following function:

              • 4. Re: Limited Ammo
                coolburn Level 1
                Kglad, thanks again for your help. I put the code in that you gave me but I get the following error message when I test my game: Scene=Scene 1, Layer=objects, Frame=3: Line 225: '(' expected
                function remove BalloonsAndBulletsF(){

                I thought it might be because of where I added the code that you gave to the other code. I have attached the code as it is right now. I appreciate any help you can give me in adding the code you gave me to the other code. I also needed to find out, how I would make it so that the player could see the number of bullets that they start with, count down as they fire them. Ex: show 40 before they fire and the count down as they fire 39,38, etc.. Thanks again for all of the help that you have given me and any future help you can give me.
                • 5. Re: Limited Ammo
                  kglad Adobe Community Professional & MVP
                  there should not be any space between remove and BalloonsAndBulletsF(){

                  var tl:MovieClip=this;

                  function removeBalloonsAndBulletsF(){
                  for(mc in tl){
                  if(mc._name.indexOf("balloon")>-1||mc._name.indexOf("bullet")>-1){
                  tl[mc].removeMovieClip();
                  }
                  }
                  }
                  • 6. Re: Limited Ammo
                    coolburn Level 1
                    Thanks kglad for helping me again. I changed the code like you said and the error message went away, but the balloons and bullets still stay on the screen, even after restarting the game. This problem wasn't happening until I added the code to limit the bullets. I'm not sure what is going on.

                    coolburn
                    • 7. Re: Limited Ammo
                      kglad Adobe Community Professional & MVP
                      use the attach code option to show your shootBullet() function.
                      • 8. Re: Limited Ammo
                        coolburn Level 1
                        Kglad, here is the shootBullet function code.

                        coolburn
                        • 9. Re: Limited Ammo
                          kglad Adobe Community Professional & MVP
                          where are you calling removeBalloonsAndBulletsF()?
                          • 10. Re: Limited Ammo
                            coolburn Level 1
                            I have an actions movie clip off the stage with the code: _root.removeBalloonsAndBulletsF();
                            Then I placed the code that you gave me at the end of all the code that I already had for the game. There is a stop(); after all of this code including the code you gave me.
                            • 11. Re: Limited Ammo
                              kglad Adobe Community Professional & MVP
                              when does that movieclip execute that code?
                              • 12. Re: Limited Ammo
                                coolburn Level 1
                                The movieclip executes the code as soon as the game starts. It initializes the game when it loads and then calls the series of functions every frame. I have attached the code that is in that movieclip.
                                • 13. Re: Limited Ammo
                                  kglad Adobe Community Professional & MVP
                                  you don't want to remove all those things fps times per second.

                                  and below var tl:MovieClip=this;

                                  attach

                                  trace(tl);

                                  and in shootBullet();

                                  attach

                                  trace(this);

                                  to ensure those are referencing the same timeline.
                                  • 14. Re: Limited Ammo
                                    coolburn Level 1
                                    Kglad,
                                    I put the trace commands where you said and the output window only shows: _level0 and nothing else.
                                    • 15. Re: Limited Ammo
                                      kglad Adobe Community Professional & MVP
                                      change the removeBulletsAndBalloonsF() function to:

                                      • 16. Re: Limited Ammo
                                        coolburn Level 1
                                        Kglad,
                                        I changed the code like you said and nothing changed. The balloons and bullets still stay on the screen.
                                        • 17. Re: Limited Ammo
                                          kglad Adobe Community Professional & MVP
                                          use the trace() function to debug:

                                          • 18. Re: Limited Ammo
                                            coolburn Level 1
                                            I used trace() like you showed me and the only thing it shows in the output window is: _level0
                                            It only appears once in the window and then blank spaces fill the window until the game ends. When I did the first trace() that you had me do, it showed _level0 everytime I shot the bullet and nothing else appeared in the window.
                                            • 19. Re: Limited Ammo
                                              kglad Adobe Community Professional & MVP
                                              is the swf that contains that code being loaded into another swf?
                                              • 20. Re: Limited Ammo
                                                coolburn Level 1
                                                No, the swf that contains that code is not being loaded into another swf?
                                                • 21. Re: Limited Ammo
                                                  kglad Adobe Community Professional & MVP
                                                  post a link to your fla and specify the code's location if it's not obvious.
                                                  • 22. Re: Limited Ammo
                                                    coolburn Level 1
                                                    The only places where any actionscript used by the game are located is in the "actions" movieclip off the top left of the stage and in frames 2, 3, and 4 of the main time line. I have changed this into a halloween game. The balloons have been changed into ghosts and the fox has been changed into a boy wearing a ghost costume. GameTest

                                                    I got the actionscript from a flash 5 game book that I have, which it uses a fox and balloons. I kept all of the code exactly the same and in the exact same places as it is in the book, and the game works fine until I try to add the limited ammo code. I made this for a NewGrounds Halloween Game.
                                                    • 23. Re: Limited Ammo
                                                      coolburn Level 1
                                                      Kglad,

                                                      I'm sorry but I didn't catch it until this morning, if you download the fla file from the link I gave you it won't work. I forgot that geocities will only allow you to upload 5mb files and mine is just over 6mb. I have no where else to upload the file. I can't afford to hire you to look at it, which I would if I could. Is there some other way I can have you look at the file? If not, I understand. Thanks again for all of the help you have given me.
                                                      • 24. Re: Limited Ammo
                                                        kglad Adobe Community Professional & MVP
                                                        remove all sound files from your fla and use file/save and compact to see if you can get your fla under 5 mb.
                                                        • 25. Re: Limited Ammo
                                                          coolburn Level 1
                                                          Kglad,

                                                          First, thank you for your help with getting my file size down. I was able to upload the fla file.
                                                          Here is the link: GameTest
                                                          • 26. Re: Limited Ammo
                                                            kglad Adobe Community Professional & MVP
                                                            here's your corrected file: www.kglad.com/Files/GameTest.fla

                                                            the main problem is you're publishing for flash 5/as1 and failed to mention that.

                                                            there was also a problem calling the remove function in an onEnterFrame loop that was mentioned previously.
                                                            • 27. Re: Limited Ammo
                                                              coolburn Level 1
                                                              I'm sorry that I didn't tell you which version of actionscript I'm was using, I thought you would be able to tell by the coding. The reason I'm publishing in flash 5/as1, is because I'm using flash 5. I downloaded the file from your link and I get the following error message: Unexpected file format. I get the same error message when I try to open it in flash mx 2004.
                                                              • 28. Re: Limited Ammo
                                                                kglad Adobe Community Professional & MVP
                                                                your coding is as2 but will work with as1 publishing.

                                                                anyway, i have flash9 and it won't publish a swf earlier than flash 8.

                                                                change your remove function to the following and call it from your "game over" frame (removing the repeated calls in your onEnterFrame loop:

                                                                • 29. Re: Limited Ammo
                                                                  coolburn Level 1
                                                                  First, I would just like to clear something up. The script that I'm using is not as2 but is as1, because flash 5 which I have and used to make this game, only uses as1 and not as2. The book that I took the script from was written two years before as2 even came out. AS2 wasn't available until flash mx 2004(also known as Flash 7) which I also have. Here is a link that shows that I know what I'm talking about: ActionScript History

                                                                  I also just wanted to let you know, that none of the scripts you gave me to remove the bullet and balloon movieclips worked. Thank you for trying to help anyway.
                                                                  • 30. Re: Limited Ammo
                                                                    kglad Adobe Community Professional & MVP
                                                                    you're welcome.