Hi, having spent many hours, with books and the web Iād like to ask for your help with a bit of code that wont run as I want it to.
The code is a simple Timer function, that works when I use it directly on the main timeline, but for the life of me I cannot get it to work as a class. I wish to have it as a standalone Timer class that I call when ever I want to have a delay (and ideally send it the delay). At the moment it wont even run with trace statements in the functions. The following is the code that works when on the main time line.
//start of Timer script
var delay:Number = 87;
var timer:Timer; // sets up a new Timer variable.
function myTimer()
{
timer = new Timer(delay, 10); // sets up a new Timer instance with a delay of 87 ms and how many repetitions (0 = continuous loop)
timer.addEventListener(TimerEvent.TIMER, onTimer); // adds a listener to the Timer and runs the function when the Timer starts
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete); // this listens for when the Timer is complete (i.e. when the second number has been reached)
timer.start(); // this starts the timer running.
trace(āinside timerā);
}
function onTimer(event:TimerEvent):void
{
trace(ātickā);
}
function onTimerComplete(event:TimerEvent):void
{
trace(āFinished!ā);
}
myTimer(); // runs the myTimer functionā
// end of script
the above all works fine.
But when trying to use as a class it gives the following errorā¦
"TypeError: Error #1034: Type Coercion failed: cannot convert 87 to generalTimer. "
I have tried various types (Number, int, String) and it still says the samething.
Hereās my class fileā¦
// class file
package
{
//trace(ācalled as fileā);
import flash.display.Sprite;
import flash.utils.;
import flash.events.;
public class generalTimer extends Sprite
{
//public static const w:Number = 10 * 1000;
public var timer:Timer;
trace(āinside timer classā);
public function generalTimer(w:Number)
{
timer = new Timer(w, 10);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
timer.start();
trace(āinside timerā);
}
private function onTimer(event:TimerEvent):void
{
trace(ātickā);
}
private function onTimerComplete(event:TimerEvent):void
{
trace(āFinished!ā);
}
}
}
// end of class file
In the .fla file i have the followingā¦
// start of .fla file
var w:Number = 87;
generalTimer(w);
// end of .fla file
why cant I send it a number to act on as it does inside my .fla file or like arguments sent to a function?
Also even when testing it with the code as a standalone class as follows, it doesnāt do anything?
//
public class generalTimer extends Sprite
{
public static const w:Number = 10 * 1000;
public var timer:Timer;
trace(āinside timer classā);
public function generalTimer()
{
timer = new Timer(w, 10);
//
Iāve tried āgeneralTimer;ā which doesnāt do anything. Iāve tried āgeneralTimer();ā which gives me ātoo many argumentsā error.
Im eventually looking to have it return something(not sure yet) when the āonTimerCompleteā event happens too, but thatāll be my next problem.
Please help as all logic has gone out the window it seems. ;(
Thanks in advance.
Trev.