0

I'm working in AS3, Flash AIR 3.2 for iOS SDK. I'm trying to run part of the program only after myLoader finishes loading an image. I have a myTimer.start(); which runs inside myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);.

What seems to be the problem at the moment is that the program is ignoring the 1000ms. The program is running after myLoader is finished at the moment, but it just seems to be doing its own thing in terms of the delay.

EDIT: Being more precise here... The program seems to be ignoring the Timer delay. Even if Timer is set to 100000ms. It seems to be running the rest of the program right after the image is loaded.

EDIT: I still had my methods running inside my Main() as well as the timerListener() in the code. Thought I commented them out. Whoops!

var myTimer:Timer = new Timer(1000);

public function Main()
    {
        init();
        displayImage();

        myTimer.addEventListener(TimerEvent.TIMER, timerListener);
        myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);

    }

public function displayImage():void {

        myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);

        myLoader.load(fileRequest);
    }

public function onLoaderComplete(e:Event) {
        //start Timer event here
        myTimer.start();
    }

public function timerListener (e:TimerEvent):void{
        trace("Timer is Triggered");
        myTimer.stop();

        aMethod();
        anotherMethod();
        moreMethods();
    }
ArrayOutOfBounds
  • 133
  • 4
  • 19
  • Nothing jumps out at me as being wrong, except that maybe you don't need that TIMER_COMPLETE event handler, since you stop the timer in your TIMER event handler. To verify whether it's really waiting 1sec or not, try using the [getTimer()](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getTimer()) method - trace() out the value that getTimer() returns in your onLoaderComplete() and timerListener() methods. – Sunil D. Feb 04 '13 at 22:42
  • Yeah, the TIMER_COMPLETE doesn't actually run. I forgot to remove that. I've got it tracing inside onLoaderComplete() and timerListener(). The Timer seems to be running at the correct delay interval, but the problem that I describe in my edit in my post still persists. – ArrayOutOfBounds Feb 05 '13 at 11:30

1 Answers1

2

You don't really clarify what you mean by "the timer is doing its own thing." Is the timer shorter or longer than you expect?

What I think is likely going on here is that your timer tick and your frame rate are out of synch. If you're familiar with the concept of the elastic racetrack, you know that the single-threaded nature of Flash (unless you're using worker threads) means that the screen can't update during a script and vice-versa. This means that if your Timer fires while the Display list is updating, it just has to wait until the display list finishes and may even have to wait until other scripts have run, depending on how Flash is prioritizing the different things in its queue.

From the Timer API:

Depending on the SWF file's framerate or the runtime environment (available memory and other factors), the runtime may dispatch events at slightly offset intervals. For example, if a SWF file is set to play at 10 frames per second (fps), which is 100 millisecond intervals, but your timer is set to fire an event at 80 milliseconds, the event will be dispatched close to the 100 millisecond interval. Memory-intensive scripts may also offset the events.

Amy Blankenship
  • 6,485
  • 2
  • 22
  • 45
  • Sorry I wasn't being very clear. I've had a look at the elastic racetrack link and I see what you mean. However, this doesn't actually seem to be the problem – for example, even if I set the Timer() to a huge ms interval like 100000, it doesn't work. The program seems to be ignoring the Timer delay and running as soon as the image is loaded. – ArrayOutOfBounds Feb 05 '13 at 11:20
  • I actually had duplicate code. (D'oh!) There's still a little "lag" after timerListener() is fired which I assume is the display list updating and loading as you explained above. – ArrayOutOfBounds Feb 05 '13 at 11:37