Skip to main content
August 6, 2009
Answered

Timer issues

  • August 6, 2009
  • 1 reply
  • 707 views

I'm still trying to wrap my head around AS3 and timers.  What I'm trying to do is have an image fade in, pause for 6 seconds and then fade out.  My 6 seconds is doing something, but it's not what I want it to do and I'm not sure why.  If anyone knows of a really good timer tutorial dealing with images, I'd appreciate it.

Here's my code:

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween:Tween;

var xmlRequest:URLRequest = new URLRequest("imageData.xml");
var xmlLoader:URLLoader = new URLLoader(xmlRequest);
var imgData:XML;
var imageLoader:Loader;
var rawImage:String;
var rawH:String;
var rawW:String;

var imgNum:Number = 0;
var checkSec:Timer = new Timer(100);
var delay:Timer = new Timer(6000);
var numberOfChildren:Number;


xmlLoader.addEventListener(Event.COMPLETE, xmlLoadedF);

function xmlLoadedF(event:Event):void {
    checkSec.start();
    checkSec.addEventListener(TimerEvent.TIMER, checkerF);
    imgData = new XML(event.target.data);
}


function packagedF():void{
    checkSec.removeEventListener(TimerEvent.TIMER, checkerF);
    rawImage = imgData.image[imgNum].imgURL;
    numberOfChildren = imgData.*.length();
    rawW = imgData.image[imgNum].imgW;
    rawH = imgData.image[imgNum].imgH;
    imageLoader = new Loader;
    imageLoader.load(new URLRequest(rawImage));
    master_mc.addChild(imageLoader);
    imageLoader.x = (stage.stageWidth - Number(rawW)) / 2;
    imageLoader.y = (stage.stageHeight - Number(rawH)) / 2;
    imageLoader.alpha = 0;
    myTween = new Tween(imageLoader,"alpha", Regular.easeOut, imageLoader.alpha, 1, 3, true);
    delay.start();
    delay.addEventListener(TimerEvent.TIMER, nextImgF);
    myTween.addEventListener(TweenEvent.MOTION_FINISH, fadeOut);
   
}

function checkerF(event:TimerEvent):void{
    if(imgNum == 0){
        packagedF();
    }else if(imgNum < numberOfChildren){
        imageLoader.unload();
        packagedF();
    }else{
        imageLoader.unload();
        imgNum = 0;
        packagedF();
    }
}

function nextImgF(event:TimerEvent): void {
    checkSec.addEventListener(TimerEvent.TIMER, checkerF);
    imgNum++;
}

function fadeOut(e:Event):void{
    imageLoader.alpha = 1;
    myTween = new Tween(imageLoader,"alpha", Strong.easeOut, imageLoader.alpha, 0, 3, true);
}

This topic has been closed for replies.
Correct answer Ned Murphy

I don't have a good source for much of anything, just the help docs and Google are what I usually need to get things done.... though helping in these forums offers new learning opportunities.

The thing with arguments is, when you have an event listener listening for an event of class X (X being Event, MouseEvent, TweenEvent, etc...), it throws an argument to the event handler that is of the same class.  So you need to make sure the event handler function is set up to receive the class of event that will be sent by the listener.

If you are confused by the difference between using evt:Event, e:Event, or event:Event... there really isn't any difference.  evt, e, and event are just variable names... you could use anything you like within the boundaries of variable naming (junk:Event, stuff:Event, cheese:Event, are just as valid).  Just be sure that if you specify evt:Event as the argument, your code within the function uses evt if it needs to use the argument

1 reply

Ned Murphy
Legend
August 6, 2009

I don't have a tutorial or detailed solution to offer, just a suggestion.  Have your tween-in listener ignite the timer, have the timer listener ignite the tween-out, and have a tween-out listener ignite the next tween-in.

August 6, 2009

Thanks for the help.

Your reply made sense and it will give me something to work on.  And that's a lot more than I had this afternoon.

I reply back when I get it to work

Thanks again.