4 Replies Latest reply: Feb 26, 2013 4:23 AM by juliuz0001 RSS

    How to add Score and Timer in Adobe flash

    juliuz0001 Community Member

      Hi guys I am making a android game. I am new in actionscript programming and i can't find to get this right.


      I need to have a score and timer in this flash file >>> http://www.filedropper.com/eggrun


      It should have coins for adding the score and a countdown timer for it's gaming time.


      When the countdown timer goes to zero. The game stops then the stage will remove the game scene then a new layer will pop up with the total score in it.


      Please help me guys


      I am nowhere close to getting this right.

        • 1. Re: How to add Score and Timer in Adobe flash
          Eddy.Li

          There are many ways to do countdown timer. Here are two examples to countdown from 60 seconds to zero. Theoretically they should work the same, but practically not always. Since heavy processing can slow down frame rate and hence method #2 will not be accurate. Really depends on your need.

           

          #1, using Timer class

           

          var delay:Number = 1000; //the delay between timer events, in milliseconds.

          var remainingTime:int = 60;

          var countDownTimer:Timer = new Timer(DELAY);

           

          countDownTimer.addEventListener(TimerEvent.TIMER, countDownUpdate);

          countDownTimer.start();

           

           

          function countDownUpdate(evt:TimerEvent):void{

               //1 second passed, do something

               remainingTime--;

               if(remainingTime == 0){

                    //60 seconds passed, do something

                    countDownTimer.stop();

                    countDownTimer.removeEventListener(TimerEvent.TIMER, countDownUpdate);

               }

          }

           

          ///////////////////////////////

           

          #2, using onEnterFrame event

           

          var frameCount:int= 0;

          var remainingTime:int = 60;

          addEventListener(Event.ENTER_FRAME, countDownEnterFrame);

           

           

          function countDownEnterFrame(e:Event):void{

               frameCount++;

               if(frameCount == Stage.frameRate){

                    //1 second passed, do something

                    frameCount = 0;

           

                    remainingTime--;

                    if(remainingTime == 0){

                         //60 seconds passed, do something

                         countDownTimer.stop();

                         countDownTimer.removeEventListener(TimerEvent.TIMER, countDownUpdate);

                    }

               }

          }

           

          ////////////////////////////////

           

          As for your score part. It really depends on what exactly you're looking for. There are a lot of ways to do it, and for each way to do it there are even more methods to implement it. Maybe you can try to describe in more details of what you have in mind, so people can help you easier. Don't worry if one method doesn't work for you, since there are so many ways to do things in Flash with AS3. Good luck

          • 2. Re: How to add Score and Timer in Adobe flash
            juliuz0001 Community Member

            look at this >>> http://www.filedropper.com/test_37. CS5

             

            I now have some coins for the score, and the score is now adding up..

             

            I also have the countdown timer, but the timer doesn't stop at 0 it still goes on. It goes on to negative numbers (-1, -2, and so on...)

             

            Please help me fix it guys..

            • 3. Re: How to add Score and Timer in Adobe flash
              Eddy.Li Community Member

              I'm assuming your "count" variable is used to keep track of score. So your

               

                   var myTimer:Timer = new Timer(1000, count);

               

              should be,

               

                   var myTimer:Timer = new Timer(1000, time);

               

              One advice, try to avoid using special reserved words for your variables. Since the uncertainty of compilers is not always predictable. For example,

               

                   function countdown(event:TimerEvent):void{}

               

              Would be safer if it's written as,

               

                   function countdown(evt:TimerEvent):void{}

               

              Or even,

               

                   function countdown(e:TimerEvent):void{}

               

              Since the word "event" is a reserved word in AS3, it could behave differently if used in special situations. These reserved words usually show up in a special color in Flash Pro or other AS3 specific editor.

              • 4. Re: How to add Score and Timer in Adobe flash
                juliuz0001 Community Member

                Thanks sir.. It is now working.. This are the codes that i input:

                 

                var time:Number = 10; //Count down from 60

                var myTimer:Timer = new Timer(1000,time);// Timer intervall in ms

                 

                myTimer.addEventListener(TimerEvent.TIMER, countdown);//EventListener for intervalls

                myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete);

                myTimer.start();//start Timer

                 

                 

                function countdown(evt:TimerEvent):void {

                countDownTextField.text = String((time)-myTimer.currentCount);

                //Display Time in a textfield on stage,

                //i'll call it countDownTextField. U will have to create it first

                }

                 

                 

                function countdownComplete(evt:TimerEvent):void {

                   // fires when the countdown is finished

                   stage.removeEventListener(Event.ENTER_FRAME,onenter);//stops the game

                   myTimer.removeEventListener(TimerEvent.TIMER, countdown);//remove listers

                   myTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, countdownComplete);

                }

                 

                 

                Now the problem is when the countdown timer goes to zero or when the health bar is zero, there should be a new layer/scene that will show the Score and the Game time the player has played..

                 

                Please help me sir..