A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Displaying elapsed time

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    18

    Displaying elapsed time

    Hi again,

    in my game (where alienships fly across the screen and I shoot them with a crosshair) I need to display the time elapsed since the start of the game. Here is the code of the Main class, the spaceship class isn't needed. I'm trying this but it only displays zero (0) and never changes.

    This is the Main class:

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.ui.Mouse;
        
    import flash.events.TimerEvent;
        
    import flash.utils.Timer;
        
    import flash.utils.getTimer;
        
    import flash.events.Event;
        
    import flash.media.Sound;
        
    import flash.net.*;
        
    import flash.media.SoundChannel;

        public class 
    Main extends MovieClip
        
    {
            
            var 
    mycounterCounter = new Counter();
            var 
    initial_time;
            var 
    current_time;
            var 
    game_time;  
            
            var 
    total:int 200;
            var 
    naves = new Array;
            var 
    tempo:Timer;
            var 
    tempo2:Timer;

            public var 
    pontos:int 0;
            
            var 
    musica_fundo:Sound = new Sound();
            var 
    tiro:Sound = new Sound();

            function 
    Main():void
            
    {
                
    play_btn.addEventListener(MouseEvent.CLICKjogar);
                
                function 
    jogar(evt:MouseEvent):void
                
    {    
                    if (
    currentFrame == 1)
                    {
                        
    gotoAndStop(totalFrames);
                    }
                    else
                    
                    
    Mouse.hide();
                    
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mexe_mira);
                    
    stage.addEventListener(MouseEvent.CLICK,som);

                    
    init_naves();
                    
                    
    initCounter();
                    
                    
    tempo = new Timer(700,total);
                    
    tempo.addEventListener(TimerEvent.TIMER,voa);
                    
    tempo.start();

                    
    tempo2 = new Timer(900,total);
                    
    tempo2.addEventListener(TimerEvent.TIMER,voa2);
                    
    tempo2.start();

                    
    game_time.addEventListener(Event.ENTER_FRAME,contador);                


                    
    tiro.load(new URLRequest("SFX/tiro4mp.mp3"));
                }
            }

            private function 
    initCounter() {
                
                
    mycounter.stage.width 30;
                
    mycounter.40;
                
    addChild(mycounter);
                
                
    start_timegetTimer();
                
    game_time0;
                
    mycounter.text start_time.toString();
                
    mycounter.mouseEnabled false;
            }

            function 
    som(event:MouseEvent)
            {
                
    tiro.play(0);
            }

            function 
    contador(event:TimerEvent)
            {
                
    game_timecurrent_time start_time;
                
    //mycounter.embedFonts = false;
                
    mycounter.text game_time.toString();
            }

            private function 
    voa(e:TimerEvent):void // função que põe as naves a "voar"
            
    {
                var 
    nave naves.pop();
                
    nave.voa();
            }

            private function 
    voa2(e:TimerEvent):void
            
    {
                var 
    nave naves.pop();
                
    nave.voa2();
            }


            private function 
    init_naves():void // função criadora das naves
            
    {
                var 
    nave1;
                var 
    nave2;

                for (var 
    0totali++) // criação de naves com x definido e y aleatório
                
    {
                    
    nave1 = new Nave  ;
                    
    nave1.= -30;
                    
    nave1.Math.floor(Math.random() * this.stage.stageHeight);

                    
    nave1.addEventListener("clicked"onNaveClicked);
                    
    this.addChild(nave1); // criação de nave
                    
    naves.push(nave1);
                }
                
                
    //            musica_menu.load( new URLRequest("Musica/MI.mp3"));
    //            sndChannel.stop();
                
    musica_fundo.load(new URLRequest("Musica/Carmina.mp3")); // load da música de jogo
                
    musica_fundo.play(); // iniciação da reprodução da música
            
    }
            
            private function 
    onNaveClicked(event:Event):void
            
    {
                
    this.pontos++;
                
    trace(this.pontos);
                
    pontos_txt.embedFonts false;
                
    pontos_txt.text String(pontos);
            }

            private function 
    mexe_mira(event:MouseEvent// função que define a posição da mira, seguindo o rato
            
    {
                
    Mouse.hide();
                
    mira.mouseX;
                
    mira.mouseY;
            }
        }

    Can anyone help? What am I doing wrong?

    Thanks, Chiapa

  2. #2
    Pillow Daddy m_andrews808's Avatar
    Join Date
    May 2001
    Location
    London England
    Posts
    924
    you never update your current_time property, though I don't actually think you need it judging from the code you've posted.

    just replace the first line of code in your contador function with this and see what happens:

    game_time = getTimer() - start_time;

    by the way, getTimer returns the time in milliseconds, so you might want to format that before you display it


  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    18
    Hi, thanks for your reply

    Actionscript Code:
    import flash.utils.getTimer;

    private var _startTimer:uint;
    var elapsedtime;

    public class Main extends MovieClip
    {
    _startTimer = getTimer();
    ...
    stage.addEventListener(Event.ENTER_FRAME,counter);
    }


    function counter(e:Event)
            {
                elapsedtime = (getTimer() - _startTimer) * 0.001;
                trace(elapsedtime);
            }

    Something like this? This traces the elapsed time. But I need it to be displayed on a dynamic text... HELP!
    Last edited by Chiapa; 07-16-2012 at 01:45 PM. Reason: I got it

  4. #4
    Pillow Daddy m_andrews808's Avatar
    Join Date
    May 2001
    Location
    London England
    Posts
    924
    glad you got it working


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center