AS 3 Flash Countdown Timer For Game Over Screen - actionscript-3

I am currently in a Flash game programming class with action script 3 and can't seem to find a clock anywhere that counts down and then does an action. I've been using Lynda tutorials and that hasn't been helpful and I have also Google searched quite a bit, but no luck. I have a Countdown clock and have been try "if" statements, but no luck.
Can someone guide me in the right direction on what I am doing wrong?
//game timer
var count:Number = 60; // amount of time
var myTimer:Timer = new Timer(1000,count); // time in ms, count is calling from line above
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
}
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}

This should help ;) Without any magic numbers.
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(2);
}

Add your if statement inside countdown like so:
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
}

Related

ActionScript 3.0 End Game Countdown Timer

I'm making a game in actionscript 3.0. I currently have a countdown timer (counts down from 30 seconds). Once the 30 seconds is up I want the frame to move from frame 2 to frame 3. I have put gotoAndStop(3) in the code under the timer but when the timer starts it goes to frame 3 straight away. It doesnt go to frame 3 when the 30 seconds is up. I would appreciate any help at all!
var nCount:Number = 30;
var myTimer:Timer = new Timer(1000, nCount);
timer_text.text = nCount.toString(nCount);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void
{
nCount--;
timer_text.text = nCount.toString();
gotoAndStop(3);
}
You are calling gotoAndStop(3); on the very first timer event, i.e. after one second since you are not checking the value of nCount. You need to call gotoAndStop(3); only when nCount is zero.
function countdown(e:TimerEvent):void {
nCount--;
timer_text.text = nCount.toString();
if (nCount == 0) {
gotoAndStop(3);
}
}

How do I display a button at a certain time, then have it disappear again in Actionscript 3?

I am trying to make a game that involves hitting keys/clicking buttons at the right time. I want to have the button/indicator hidden until the right time but am having a bit of trouble getting it to display. This is the code I have for it so far.
var count:Number = 5;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent): void {
timer_txt.text = String((count)-myTimer.currentCount);
}
btnthing.addEventListener(MouseEvent.CLICK, btnclick);
btnthing.visible=false;
while (((count)-myTimer.currentCount)==1) {
btnthing.visible=true;
}
function btnclick(e:MouseEvent): void {
if (((count)-myTimer.currentCount) == 1) {
myTimer.stop();
btnthing.visible=false;
time_txt.text = "Yay!";
} else {
myTimer.stop();
gotoAndStop(3);
}
}
So far my code starts the timer and displays a countdown. If I remove the part that hides/shows the button, everything works fine.
var count:Number = 5;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent): void {
timer_txt.text = String((count)-myTimer.currentCount);
}
btnthing.addEventListener(MouseEvent.CLICK, btnclick);
function btnclick(e:MouseEvent): void {
if (((count)-myTimer.currentCount) == 1) {
myTimer.stop();
btnthing.visible=false;
timer_txt.text = "Yay!";
} else {
myTimer.stop();
gotoAndStop(3);
}
}
If anyone could help me get this right that would be awesome. Thanks!
You're not using the 'while' loop correctly. Change it to a 'if' statement and put it inside the countdown function like this:
function countdown(event:TimerEvent): void
{
timer_txt.text = String((count)-myTimer.currentCount);
if (((count)-myTimer.currentCount)==1) {
btnthing.visible=true;
}
}
so that your code tests for the currentCount on each tick of the timer and finally does something about it.
Read up on 'while' loops - they don't do exactly what you think they do and it's easy to get 'em into infinite repitions if you don't increment the thing that you're changing within the loop.

Action Script 3 Timer Not Stopping

Okay, so I am a very amateur AS3 programmer. I have a timer setup, and after 45 seconds it should move to scene 6, however if it calls hitTestObject, it should stop and reload from 0 when the scene reloads. EDIT: I know this code is probably really bad coding, I'm also taking tips on how to fix this code up. Here's my code:
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, onEnterFrame);
myTimer.start();
{MAIN FUNCTION}
function onEnterFrame(e:Event):void {
var startTime:int=getTimer();
var currentTime:int=getTimer();
trace(currentTime);
if (currentTime>45000){
gotoAndStop(1, "Scene 6");
}
}
My issue, is that the timer keeps running when it hits test object and so when scene 3 is reloaded, the timer just keeps going. Therefore you only have to play for a total of 45 seconds, no matter how many times you die. It should be that once you die the timer reloads when you reload scene 3. Any ideas on what I can do?
Hope this helps:
var myTimer:Timer;
function resetTimer():void
{
// check if timer already initialize
if(myTimer != null)
{
// just reset
myTimer.stop();
myTimer.reset();
}
else
{
// initialize timer and set a 45 sec delay;
myTimer = new Timer(45*1000,1);
myTimer.addEventListener(TimerEvent.TIMER, handleTimerTick);
}
myTimer.start();
}
function handleTimerTick(event:TimerEvent):void
{
// stop and null timer;
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
myTimer = null;
// goto my 6th scene
gotoAndStop(1, "Scene 6");
}
// whenever a hit test is performed and a timer reset is needed just call
resetTimer();

Some errors in flash AS3 game, almost completed with lot of effort and help received

First I really want to thank you for all the help you have given me so far, since I did not know anything about AS3 (basics gotoAnd stuff only) I came to Stackoverflow searching for some code already made but I was encouraged by some members to make the code by myself, now after almost 2 weeks and thanks to a lot of great people my soccer penalty kick game is almost finished, I really love this place.
I know I have to work on some collisions and other stuff since currently the game is not the best (remember I’m just a newbie), but Unfortunately while checking the game functioning by playing it over and over again, I have found the following:
1- When you get 3 fails, then game is over and a play again button appears after some animation, you click on it and everything seems to be fine, but when you continue playing the second time you reach 3 fails, when you click the button a new cursor appears??? Please help
2- I tried millions of times to make the ball move with speed and to animate its trajectory but was unable to make it, any help on this will be highly appreciated. I have speed variables and gravity but I didn’t know how to use them
3- I'm getting a actionscript error related to a removeChild, I tried many times removing some lines but I´m unable to fix it.
4- I'm using too many timers, I don't know if this is recommendable.
Here is the .fla file https://rapidshare.com/files/1702748636/kicks.fla just in case anybody want to try the game (this is really simple since it is my 1st AS project) and want to help me with the code and help me improving the game, and here is the code if somebody does not need to get into the file (I know this place is full of really smart people), once I finish it I know I will be able to do a lot of stuff with AS3.
var score:Number;
var angle:Number;
var speed:Number;
var cursor:MovieClip;
var failed:Number;
var ballRotation:Boolean = false;
function initializeGame( ):void
{
ball.x = 296.35;
ball.y = 353.35;
score=0;
failed=0;
cursor = new Cursor();
addChild(cursor);
cursor.enabled = true;
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
stage.addEventListener(MouseEvent.CLICK, kick);
}
function dragCursor(event:MouseEvent):void
{
cursor.x = this.mouseX;
cursor.y = this.mouseY;
}
initializeGame();
var mouse = this.Mouse;
function kick(evt:Event)
{
removeChild(cursor);
pateador_mc.play();
var timer:Timer = new Timer(500,1);
timer.addEventListener(TimerEvent.TIMER, delayedAction);
timer.start();
function delayedAction(e:TimerEvent)
{
moveBall();
}
}
speed=-100000;
var ease:int = 100;
var gravity:Number = 0.5;
function moveBall()
{
var targetX:Number = mouseX;
var targetY:Number = mouseY;
var angle = Math.atan2(targetY,targetX);
ball.x = mouseX + Math.cos(angle);
ball.y = mouseY + Math.sin(angle) ;
ballRotation = true;
stage.removeEventListener(MouseEvent.CLICK, kick);
if (ballRotation==true)
{
keeper.gotoAndStop(1 + Math.floor(Math.random() * keeper.totalFrames));
ball.play();
}
if (ball.hitTestObject ( keeper)){
ball.y=keeper.x-ball.height- ball.width;
trace ("Tomela");
}
if (ball.hitTestObject(goalie) && ball.y>69 /*&& ball.y<178 && ball.X>139 && ball.x<466*/)
{
gol_mc.play();
score ++;
showScore();
var timer3:Timer = new Timer(3000,1);
timer3.addEventListener(TimerEvent.TIMER, delayedAction3);
timer3.start();
function delayedAction3(e:TimerEvent)
{
ball.x = 296.35;
ball.y = 353.35;
stage.addEventListener(MouseEvent.CLICK, kick);
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
addChild(cursor);
keeper.gotoAndStop(1);
}
}
else
{
fails_mc.play();
failed++;
var timer2:Timer = new Timer(3000,1);
timer2.addEventListener(TimerEvent.TIMER, delayedAction2);
timer2.start();
function delayedAction2(e:TimerEvent)
{
ball.x = 296.35;
ball.y = 353.35;
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
stage.addEventListener(MouseEvent.CLICK, kick);
addChild(cursor);
keeper.gotoAndStop(1);
}
trace(failed);
if (failed==3) {
gameFinished();
trace("YOU LOST");
}
}
function showScore():void{
goles_txt.text ="" +score;
}
trace (score);
function gameFinished(){
gameOver.play ();
stage.removeEventListener(MouseEvent.CLICK, kick);
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
timer2.stop();
Mouse.show();
this.mouseX=cursor.x ;
this.mouseY=cursor.y;
again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain);
}
function playAgain():void{
gameOver.gotoAndPlay(31);
fails_mc.gotoAndStop(1);
keeper.play();
var timer4:Timer = new Timer(1000,1);
timer4.addEventListener(TimerEvent.TIMER, delayedAction3);
timer4.start();
function delayedAction3(e:TimerEvent)
{
initializeGame();
}
}
}
I’ll really appreciate it guys , I promise I won’t be bothering again for a long time
1/3.
Problem 1 & 3 are the same problem. Looks like your trying to remove the cursor from the stage (removeChild) every click (so it will error after the first click because it's no longer a child of anything). Your adding it back on your delayedAction2 which doesn't run unless your hit test is true and only after 3 seconds. On initialize game you create a whole new cursor and add that to the stage which is why you get a duplicate after the first game.
Rather than removeChild the cursor, it might better to just set it's visibility to false/true and only create it once.
You'll need to use an EnterFrame handler, or timer, or tween for this. I can post an example later.
I can't figure out why you're using timers at all or need to delay your functions, except maybe to allow time for the kick animation?
You're code is very disorganized, naming functions things like 'delayedAction' is bad as it doesn't really tell you anything about the purposed of the function. You also have way too much functions inside of other functions. Here is a quick refactoring of your code I've done to hopefully teach a few things. I've also added the tween for the ball animation.
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
var score:Number;
var cursor:MovieClip;
var failed:Number;
var ballRotation:Boolean = false;
var ballTweenX:Tween;
var ballTweenY:Tween;
var targetCursor = new Cursor(); //only want one of these and you want it to exist the whole time so keep out here.
addChild(targetCursor);
initializeGame();
function initializeGame( ):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
stage.addEventListener(MouseEvent.CLICK, kick);
ball.x = 296.35;
ball.y = 353.35;
score=0;
failed=0;
targetCursor.visible = true;
Mouse.hide();
}
function dragCursor(event:MouseEvent):void
{
targetCursor.x = this.mouseX;
targetCursor.y = this.mouseY;
}
function kick(evt:Event)
{
//removeChild(targetCursor);
targetCursor.visible = false;
pateador_mc.play();
stage.removeEventListener(MouseEvent.CLICK, kick); //move this here, because you don't the option kick again while already kicking
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor); //added this, you probably don't want the target moving after the click...
setTimeout(moveBall, 500);//cleaner and more efficient than using a timer for a one time delayed call.
}
function moveBall()
{
var targetX:Number = mouseX;
var targetY:Number = mouseY;
var angle = Math.atan2(targetY,targetX);
targetX = mouseX + Math.cos(angle);
targetY = mouseY + Math.sin(angle) ;
ballRotation = true;
ballTweenX = new Tween(ball, "x", null, ball.x, targetX, .3, true);
ballTweenY = new Tween(ball, "y", null, ball.y, targetY, .3, true);
ballTweenY.addEventListener(TweenEvent.MOTION_FINISH, ballTweenDone,false,0,true);
if (ballRotation==true)
{
keeper.gotoAndStop(1 + Math.floor(Math.random() * keeper.totalFrames));
ball.play();
}
}
function stopBallTween():void {
ballTweenX.stop();
ballTweenY.stop();
}
function ballTweenDone(e:TweenEvent):void {
if (ball.hitTestObject ( keeper)){
ball.y=keeper.x-ball.height- ball.width;
trace ("Tomela");
}
if (ball.hitTestObject(goalie) && ball.y>69 /*&& ball.y<178 && ball.X>139 && ball.x<466*/)
{
gol_mc.play();
score ++;
showScore();
}else
{
fails_mc.play();
failed++;
trace(failed);
if (failed==3) {
gameFinished();
trace("YOU LOST");
return; //added this because you don't want the rest of this function running if it's a game over
}
}
setTimeout(resetShot, 3000); //you had the code I put in resetShot repeated twice
trace(score);
}
function resetShot():void {
ball.x = 296.35;
ball.y = 353.35;
targetCursor.visible = true;
keeper.gotoAndStop(1);
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
stage.addEventListener(MouseEvent.CLICK, kick);
}
function showScore():void{
goles_txt.text ="" +score;
}
function gameFinished(){
gameOver.play();
stage.removeEventListener(MouseEvent.CLICK, kick);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragCursor);
Mouse.show();
//this.mouseX=cursor.x ;
//this.mouseY=cursor.y; //These are read only properties, your can't set the mouse position...
again_btn.addEventListener(MouseEvent.MOUSE_DOWN, playAgain);
}
function playAgain(e:Event = null):void{
gameOver.gotoAndPlay(31);
fails_mc.gotoAndStop(1);
keeper.play();
setTimeout(initializeGame, 1000);
}

Flex 4 timers keep Firing

I'm trying to create a simple flex4 project which involves some timers that trigger other functions.
I don't have much experience with Action Script and even less with timer events.
Here is a bit of my code it seems to be working for the most part but you lines were I'm adding up the total score (score = score +1;) seems to just keep adding and adding when I test the application. I think its because the timers keep firing the function but I'm not sure.
private var score:int = 0;
private function submit():void {
this.currentState = 'loading';
var timer:Timer = new Timer(2200);
timer.addEventListener(TimerEvent.TIMER, removeLoading);
timer.start();
}
private function removeLoading(event:TimerEvent):void{
removeloading.play();
var timer1:Timer = new Timer(1000);
timer1.addEventListener(TimerEvent.TIMER, viewResults);
timer1.start();
this.currentState = 'results';
}
private function viewResults(event:TimerEvent):void{
if (q1_t.selected == true){
answer1m.text = 'You Answer the Question Correctly.';
score = score +1;
} else {
answer1m.text ='The Correct answer was: '+ q1_t.label;
}
if (q2_f.selected == true){
answer2m.text = 'You Answer the Question Correctly.';
score = score +1;
} else {
answer2m.text ='The Correct answer was: '+ q2_f.label;
}
finalscore.text = score.toString();
}
So I did a bit more research and turns out I hadn't included the second timer parameter.
The second parameter is the number of times that the TimerEvent.TIMER event will be dispatched before stopping. If you set the second parameter to 0 (zero) or omitted it completely, the timer would run forever (or until you called the stop() method on the timer instance.
Since I only want to run the event once I need to add 1.
From this:
var timer:Timer = new Timer(2200);
To this:
var timer:Timer = new Timer(2200,1);