Hi all!
I am still quite new to AS3, but trying to learn the essentials.
What I have trouble with is this:
I have a box on the stage (or frame 1 in timeline). Inside this box I made a dynamic textfield. I made a movieclip of these two together and called it watch (instance name). Also exported to actionscript with name of the class StopWatch.
Now, in actions in the timeline I do this:
import classes.stopwatch.StopWatch;
var watch:StopWatch = new StopWatch();
watch.setTimerStart();
I made StopWatch as an own class. It looks like this:
package classes.stopwatch {
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
public class StopWatch
{
private var timer:Timer;
public var timeToday:Number;
private var _root:Object;
public function StopWatch()
{
//_root = MovieClip(root);
//timer = new Timer(1); // ticks every 1 millisecond
}
public function setTimerStart():void
{
//set the time in milliseconds
timer = new Timer(10);
timer.addEventListener("timer", timerHandler);
timer.start();
}
public function timerHandler(eventArgs:TimerEvent):void {
//trace("Total time: " + timer.currentCount + timer.delay);
timeToday = timer.currentCount * timer.delay;
//trace(timeToday);
this.displayTime.text = "Time: " + String(timeToday);
//timer.removeEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
//var d:Date = new Date(0);
//d.setMilliseconds(timer.currentCount);
//trace( d.getMilliseconds());
//trace("counting has finished");
}
public function setTimerStop(stopTime:Number):void
{
//timer.reset(delay, count);
timer = new Timer(stopTime, 1); //(tid til stop, antall ganger)
timer.addEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
}
public function completeHandler(event:TimerEvent):void {
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
trace("counting has finished");
}
public function start():void
{
timer.start();
}
public function getTimeStamp():int // returns timestamp in milliseconds
{
return timer.currentCount * timer.delay;
}
public function reset():void // resets the stopwatch
{
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, completeHandler);
timer.reset();
}
}
}
Now I don’t use all of this yet, and it is not important. What I can’t do is call
this.displayTime.text = "Time: " + String(timeToday);
Actually I get an error which says that setTimerStart(); does not exist or something around those lines. If you need an exact error msg I can get that for you later.
So, what do I do to be able to use the textfield from the StopWatch class?