Monday 1 February 2010

Flash Game: The Timer and The Finishing Line

So the timer. The thing which my game style circles around because it times how fast people complete the track. Below is the code I used to get it working:

var myTimer:Timer = new Timer(1000);

myTimer.start();
function laptime(event:TimerEvent):void
{
timerTxt.text = String(myTimer.currentCount);
}


I would use other lines of code to stop and reset it at a later date.

Now onto the finish line. The idea for this was that the user would complete the track and then the finishing stages of the game would occur. This means that collision detection needed to come back again so that when the car hit the finishing line all of these finishing stages would occur. I will let the code I used do the talking here and I will explain after:

function finishline(evt:Event):void
{
if (mc_car.hitTestObject(mc_finishline)){
mctheEnd.x = 139;
mctheEnd.y = 138;
mctheEnd.text = "Congratulations your Lap Time was: \n" + myTimer.currentCount + " seconds";
mc_lighten.x = 200;
mc_lighten.y = 200;
mc_lighten.alpha = 0.95;
myTimer.stop();
btn_playagain.x = 273;
btn_playagain.y = 270;
mc_car.visible = false;
channel2.stop();
}
}


So the first thing I wanted was for some text to appear. To do this I created a text box off screen and then once the car hit the point it would be moved on screen. I created a dynamic text box. This would also allow me to state what text would appear which is the next line of my code. I got it to show the current point on my timer along with a congratulations message.
The next part of my code is mc_lighten. This was a white box that I again had off screen and moved on screen. The purpose of this was to lighten (hence the name) the game in the background so that the message would be easier to read. The change the opacity of this white box i used the .alpha code and set it to 0.95 opacity.
The next line is pretty much self explanatory even to those who do not understand actionscript! I basically stop the timer so that it doesn't continue going up and that the final time can be displayed in my message as mentioned above.
I then created a play again button and again moved that on screen from offscreen this is so the user can play the course again to try and beat their score.
Next I decided I would change the car's visibilty to 0 so that the user wouldn't be able to tell that they can move the car once they've finished the track. This was not really needed but I felt the need to do it anyway.
The final bit of code is to stop a sound playing which I will mention in a later post.

And so thats it for the timer and the finishing line. The next post will be all about how I got my obstacles working and the splash screens I created to go with the game.

No comments:

Post a Comment