PDA

View Full Version : Timer trouble in AS3


dprichard
12-04-2008, 11:14 PM
I'm creating a site that contains data parsed from XML. The XML contains information that creates text boxes and a Sprites that holds images into a scrollpane. The text and images are referenced in the xml as well.

I've created a timer that places another image in the Sprite every 5000 milliseconds. All of this works correctly. When a user select another page from the menu, new xml for the selected page is loaded and the new content is loaded into the scrollpane.

My problem is removing the original event listener for the timer and adding the new one. I've used removerEventListener on the timer but it doesn't seem to be working. Ultimately, every time the user selects a different page, they add another event listener and the images do not load in correctly. I've included my code below (please forgive it's crudeness, I'm by no means an expert)

var pictimer:Timer = new Timer(5000);

//function to load in each individual page.
//This function is called from the menu.xml file.
function loadPageInfo(xmlFilePath):void {
var xmlLoader:URLLoader = new URLLoader();
var xmlRequest:URLRequest = new URLRequest(xmlFilePath);
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
xmlLoader.load(xmlRequest);

var imagesXML:XML = new XML();
imagesXML.ignoreWhitespace;

function xmlLoaded(evt:Event):void {

nextImage = 0;
imagesXML = XML(xmlLoader.data);//Create XML object.
var pageHolder:Sprite = new Sprite();//Create Sprite to hold layout.
imageCount = imagesXML.headerContent.length();//determine how many images in XML

// create a new LoaderProV3 instance to hold fading images.
var picFader:LoaderProV3AS3 = new LoaderProV3AS3();
var scaleMCTE:MCTE = new MCTE(picFader, false, "show", "Blur", 6, 16, "Regular", "easeNone", 2);
picFader.visible = true;
picFader.setSize(imagesXML.layoutSettings.imagewid th, imagesXML.layoutSettings.imageheight);
picFader.move(imagesXML.layoutSettings.imagex, imagesXML.layoutSettings.imagey);
picFader.transitionType = "MCTE";
picFader.source = imagesXML.headerContent[nextImage].headerimage;//Adds 1st image to picFader
picFader.addEventListener(LoaderEvents.TRANSITION_ END,transitionEndHdl);
function transitionEndHdl(args: LoaderEvents):void {
DisplayObjectContainer(mcPageLoader.content).getCh ildAt(2).htmlText = imagesXML.headerContent[nextImage].headertext;
DisplayObjectContainer(mcPageLoader.content).getCh ildAt(2).setTextFormat(labelFormat);
}
pageHolder.addChild(picFader);//add picFader to the Sprite

//attach gradient bar.
var gradBar:gradientBar = new gradientBar();
gradBar.x = imagesXML.layoutSettings.imagex;
gradBar.y = imagesXML.layoutSettings.imagey;
gradBar.width = imagesXML.layoutSettings.imagewidth;
gradBar.height = 35;
pageHolder.addChild(gradBar);

//Create Textbox for image labels.
var tbLabelContent:TextField = new TextField();
tbLabelContent.type = TextFieldType.DYNAMIC;
tbLabelContent.autoSize = TextFieldAutoSize.LEFT;
tbLabelContent.selectable = false;
tbLabelContent.antiAliasType = AntiAliasType.NORMAL;
tbLabelContent.embedFonts = true;
tbLabelContent.wordWrap = false;
tbLabelContent.width = imagesXML.layoutSettings.imagelabelwidth;
tbLabelContent.height = imagesXML.layoutSettings.imagelabelheight;
tbLabelContent.x = imagesXML.layoutSettings.imagelabelx;
tbLabelContent.y = imagesXML.layoutSettings.imagelabely;
tbLabelContent.setTextFormat(labelFormat);
pageHolder.addChild(tbLabelContent);//add textbox to the Sprite.


//Create Textbox for body Content.
var tbBodyContent:TextField = new TextField();
tbBodyContent.autoSize = TextFieldAutoSize.LEFT;
tbBodyContent.embedFonts = true;
tbBodyContent.wordWrap = true;
tbBodyContent.multiline = true;
tbBodyContent.width = imagesXML.layoutSettings.textwidth;
tbBodyContent.height = imagesXML.layoutSettings.textheight;

tbBodyContent.x = imagesXML.layoutSettings.textx;
tbBodyContent.y = imagesXML.layoutSettings.texty;

tbBodyContent.htmlText = imagesXML.bodyContent.bodyCopy;
tbBodyContent.setTextFormat(textFormat);
pageHolder.addChild(tbBodyContent);//add textbox to the Sprite.


mcPageLoader.source = pageHolder;//Adds the Sprite to the scrollpane.
pictimer.removeEventListener(TimerEvent.TIMER, loadNextImage);
pictimer.addEventListener(TimerEvent.TIMER, loadNextImage);
pictimer.start();
trace("started");
}
function loadNextImage():void {
nextImage++;
trace("loadNextImage fired " + nextImage + " " + imageCount);
if (nextImage >= imageCount) {
nextImage = 0;
}
DisplayObjectContainer(mcPageLoader.content).getCh ildAt(2).htmlText = "";
DisplayObjectContainer(mcPageLoader.content).getCh ildAt(2).setTextFormat(labelFormat);
DisplayObjectContainer(mcPageLoader.content).getCh ildAt(0).source = imagesXML.headerContent[nextImage].headerimage;
}
}

Can someone please assist me in removing the event listener? Thanks.

wvxvw
12-05-2008, 07:27 AM
First of all it was a wrong move to declare function within other functions. This makes code difficult to understand and modify. More than that the removing of objects created in such manner is complicated because once the function that declares another function exits you loose external pointers to the functions it declares.
So, rewrite your code in a way you don't do:
function somefunction():void
{
function anotherFunction():void
{
....
}
}
This may as well solve your original problem.