13 Replies Latest reply on Oct 3, 2009 8:27 AM by Shootiry

    Need help with lives!

    Shootiry

      I want that if my lives = 0, it goes to end screen.

      I have enter this code to my "score" layer:

      if (lives == 0){

      removeMovieClip(face);

      gotoAndStop(5);

      }

        • 1. Re: Need help with lives!
          Shootiry Level 1

          I mean: when my score goes to 0, it doesn't change frame. And when i lose one more live, lives go to minus.

          • 2. Re: Need help with lives!
            Ned Murphy Adobe Community Professional & MVP

            That code you showed will only execute once if you remain in that frame during the game.  What you probably want to do is place that code in a function  and call the function each time you lose a life.

            • 3. Re: Need help with lives!
              Shootiry Level 1

              ok... I'm not a advanced coder, so can you explain a little more?

              thanks

              • 4. Re: Need help with lives!
                Ned Murphy Adobe Community Professional & MVP

                There was nothing advanced about my explanation.  What part of it confuses you?

                • 5. Re: Need help with lives!
                  Shootiry Level 1

                  to place code in a function and call function

                  • 6. Re: Need help with lives!
                    Ned Murphy Adobe Community Professional & MVP

                    To place the code in a function you just define a function, let's name it "checkLives"...

                     

                    function checkLives(){

                         if (lives == 0){

                              removeMovieClip(face);

                              gotoAndStop(5);

                         }

                    }

                     

                    And to call that function, you would be doing it every time you change the lives value, so where/however you do that now, let's say you use... lives = lives - 1;   then you follow that line of code with a call to the function to check if the lives are 0 yet...

                     

                    lives = lives - 1;

                    checkLives();

                    • 7. Re: Need help with lives!
                      Shootiry Level 1

                      I changed my code in my layer to this:

                       

                       

                      lives=5;

                      function checkLives(){

                           if (lives == 0){

                                removeMovieClip(face);

                                gotoAndStop(5);

                           }

                      }

                       

                       

                      And in my "gun" to this:

                       

                       

                      if (this.hitTest(_root["glad"+i]) && _root["glad"+i].dead == 0) {

                              _root.score += -5;

                              _root.lives = _root.lives - 1;

                                checkLives();

                               _root["glad"+i].gotoAndPlay(2);

                      }

                      And i have still the same thing...

                      • 8. Re: Need help with lives!
                        Ned Murphy Adobe Community Professional & MVP

                        You need to make things agree in terms of targeting, so if the checkLives() function lives in another timeline, you need to target that other timeline when calling it....  I don't know what your setup/hierarchy is, but you seem to be using _root to target things.  If that function is not on the _root timeline, then you will need to adjust things for that as well.  Unfortunately I cannot tell you how you have things set up, that's something only you know at the moment, so I cannot tell you how you need to target things properly.  At this stage I can only present the concept, and it's up to you to work out tying things together.  The code will work, you just need to figure out how you need to implement it.

                         

                        If the function is too much for you to resolve, maybe you can try buildiing it into the code where you change the lives value instead....

                         

                        if (this.hitTest(_root["glad"+i]) && _root["glad"+i].dead == 0) {

                                _root.score += -5;

                                _root.lives = _root.lives - 1;

                                  if(_root.lives == 0){

                                       _root.removeMovieClip(face);

                                       _root.gotoAndStop(5);

                                  }

                                 _root["glad"+i].gotoAndPlay(2);

                        }

                         

                        I have no idea if using _root in what I just showed applies to all cases I used it for

                        • 9. Re: Need help with lives!
                          Shootiry Level 1

                          Thanks a lot!!!

                          • 10. Re: Need help with lives!
                            Ned Murphy Adobe Community Professional & MVP

                            You're welcome.  Just as an added note, you can also rewrite the following lines...

                             

                            _root.score += -5;

                            _root.lives = _root.lives - 1;

                             

                             

                            as...

                             

                            _root.score -= 5;

                            _root.lives -= 1;

                            • 11. Re: Need help with lives!
                              Shootiry Level 1

                              ok thanks!

                              But now i have another problem (once again) in my game

                              When i press restart, my timer counts down twice as fast as it counted. Game: http://www.kongregate.com/games/Saastamoinen/shootiry

                              Timer Code:

                              timer = 60; countdown = function(){

                              timer--; if(timer==0){

                              clearInterval(countdownInterval);

                              removeMovieClip(face); gotoAndStop (5);

                              }

                              }

                              countdownInterval = setInterval(countdown,1000)

                              • 12. Re: Need help with lives!
                                Ned Murphy Adobe Community Professional & MVP

                                If that's true, then you probably have more than one setInterval working.  What you probably need to do is clear your interval for any condition of a game ending/restarting.

                                 

                                If you have new problems, your best bet is to start a new posting with a title that reflects the problem so that it stands out as a new posting looking for help and so that others might benefit from it someday (if they ever get the search functionality working more smoothly).

                                • 13. Re: Need help with lives!
                                  Shootiry Level 1

                                  I got it! thanks again!