views:

29

answers:

1

ok so im using real basic code for a small game and ive got a timer set up on one room and cant get it to display in the endgame room please help?

this is the code i used

var gameStartTime:uint;
var gameTime:uint;

var gameTimeField:TextField;

gameTimeField = new TextField();
gameTimeField.x = 900;
gameTimeField.y = 50;


addChild(gameTimeField);

gameStartTime = getTimer();
gameTime = 0;

addEventListener(Event.ENTER_FRAME,showTime);

function showTime(event:Event) 
{gameTime = getTimer()-gameStartTime;
gameTimeField.text = "Time: "+clockTime(gameTime);
}
function clockTime(ms:int)
{
    var seconds:int = Math.floor(ms/100);
    var minutes:int = Math.floor(seconds/60);
    seconds -= minutes*60;

    var timeString:String = minutes+":"+String(seconds+100).substr(1,2);

    return timeString;
}
A: 

If all your code is happening on the timeline of one MovieClip you could store the gameTime as a member of the movieclip instead of in a timeline var. This is possible since MovieClip is an 'open' class, it allows you to add members (variables, functions) to instances it.

so instead of

var gameTime:uint;
gameTime = getTimer()-gameStartTime;

you have to use 'this' to refer to the current MovieClip, so use:

this.gameTime = getTimer() - gameStartTime;

now you can reference this.gameTime on other frames in your MovieClip to read the value.

In general I would not suggest programming your game code like this - when your code starts to grow it is better to add an actual file that contains the code otherwise you'll quickly lose track of what is where, read http://www.adobe.com/devnet/flash/quickstart/external_files_as3/ for a quick introduction.

Simon Groenewolt