-
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 mycounter: Counter = 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.CLICK, jogar);
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.x = stage.width - 30;
mycounter.y = 40;
addChild(mycounter);
start_time= getTimer();
game_time= 0;
mycounter.text = start_time.toString();
mycounter.mouseEnabled = false;
}
function som(event:MouseEvent)
{
tiro.play(0);
}
function contador(event:TimerEvent)
{
game_time= current_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 i = 0; i < total; i++) // criação de naves com x definido e y aleatório
{
nave1 = new Nave ;
nave1.x = -30;
nave1.y = 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.x = mouseX;
mira.y = mouseY;
}
}
}
Can anyone help? What am I doing wrong?
Thanks, Chiapa
-
Pillow Daddy
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
-
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
-
Pillow Daddy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|