AS3 TimerEvent not 'firing'

Hi (again),
as previously stated, I am learning AS3.
I have written some code (it’s messy and no where near professional looking but for now, I am settling for it just working!) and it isn’t working.

Basically,
I need to have a timer display on the stage and count down/up according to my calculations.
I believe this is done by having the function run over and over with a TimerEvent.
However, I have included it and it doesn’t work.
I have spent all day trying to get it to work with no joy.

Can anyone point out where I am going wrong?
I really don’t want to quit learning this and so have persevered all day … but I just need a shove in the right direction now!

Any help would be appreciated beyond belief!!

Code is below …

package  {

import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class timestuff extends MovieClip {
	
	
	public function timestuff() {			
		
		var myTimer:Timer=new Timer(1000, 0);
		myTimer.addEventListener(TimerEvent.TIMER, displayTime);		
		myTimer.start();
		
					
		var myText:TextField = new TextField();
		var myTextFormat:TextFormat = new TextFormat();	
		
		var focsSemStart:int = 9;
		var focsSemEnd:int = 16;
		
		var date:Date = new Date();
		var secsNow = date.seconds;
		var minsNow = date.minutes;
		var hoursNow = date.hours;
		
		var minsToGo = int;
		var hoursToGo = int;
		var secsToGo = int;
			
		// embed this into another if statement for the lessons.
		
		function textStuff()
		{
			myText.width = 500, myText.height = 200, myText.x = 150, myText.y = 5;
			myTextFormat.size = 20;
			myText.setTextFormat(myTextFormat);
		}
		
		function HourMaths(a:int, b:int):int
		{			
		if (a < b)
		{
			hoursToGo = (b - a);	
			minsToGo = 59 - minsNow;
			secsToGo = 60 - secsNow;
			
		}
		else if (a > b)
		{
			hoursToGo = a - b;
			minsToGo = minsNow;
			secsToGo = secsNow;
		}
		if (secsNow == 60)
		{
			secsToGo = 0
			minsToGo = minsToGo + 1
		}			
		if (secsToGo < 10)
		{
		secsToGo = "0" + secsToGo;
		}
		if (minsToGo < 10)
		{
		minsToGo = "0" + minsToGo;
		}			
		return hoursToGo;
		return minsToGo;
		return secsToGo;
		}
			
		
		HourMaths(hoursNow, focsSemStart);			
					
		function displayTime(e:TimerEvent)
		{					
		if (hoursNow < focsSemStart && minsToGo == 0)
		{
			myText.text = "Lesson start countdown " + (hoursToGo) + ":00:" + (secsToGo);
			textStuff();
		}
		else if (hoursNow < focsSemStart)
		{
			myText.text ="Lesson start countdown " + (hoursToGo -1) + ":" + (minsToGo)+ ":" + (secsToGo);
			textStuff();
		}else if (hoursNow > focsSemStart && minsNow == 0)
		{
			myText.text = "Lesson time elapsed  " + (hoursToGo) + ":00" + ":" + (secsNow);
			textStuff();
		}
		else if (hoursNow > focsSemStart && minsNow > 0)
		{
			myText.text = "Lesson time elapsed  " + (hoursToGo) + ":" + (minsToGo) + ":" + (secsToGo);
			textStuff();
		}
		else if (hoursNow == focsSemStart && minsNow == 0)
		{
			myText.text = "IT'S LESSON TIME";
			textStuff();
		}
		else if (hoursNow == focsSemStart && minsNow > 0)
		{
			myText.text = "Lesson time elapsed 00:" + (minsToGo) + ":" + (secsToGo);
			textStuff();
		}
		
		addChild(myText);
		
		}
				
	}
}

}

Writing your code this way will likely mess up the rest of your code that uses those variables. What you’re doing here is assigning those variables values of type Class, instead of saying that the variables are of type int. I realize that you might not have learned about the distinction yet, but what you probably want to write is:

var minsToGo:int;
var hoursToGo:int;
var secsToGo:int;

They’ll be set to 0 by default.

I don’t really know what you’re trying to do here, but only the first return reached outside of an if/else sort of conditional will be executed, because return immediately exits execution of the function in which it’s enclosed.


In general, if you want to find out whether or not an event is firing, the first thing you want to do is put a trace call on the very first line of your event handler function. A problem with the way you’re diagnosing the problem now is that you are assuming that the event handler is not fired, but the only evidence you seem to have of that is buried deep within the internal logic of the handler function displayTime. Adding more trace calls throughout your code is usually a good idea when debugging, especially if you haven’t learned how to use the step-by-step debugger included in some Flash coding environments, like Flash Pro, Flash Builder, and (I assume) Flash Develop.

I haven’t worked through the logic of your code in detail, but I suspect that some of your assumptions about hoursNow, minsNow, and secsNow are being invalidated by the way you declared and initialized those variables.

Thank you for the help.
Sorry, I didn’t thank you sooner but it’s been one assignment after another!!
I do feel like I am learning though … slowly but surely!

1 Like