Event Listener to wait for array to fill from parsed xml

By SFranz | Jan 12, 2009

actionscript3

Hello,

I have an subclass that is called from the constructor of the main class. The subclass is loading and parsing a .xml file for images. A Event Listener is waiting for the .xml file to load and the fill the image URL’s into an array. This code works fine (tested), but here is my problem.

While the subclass is called in the constructor, I also have code that should display two of the loaded images by default. However, while the xml file is parsing and filling the array, the main code already continues on and tries to display the two default images on the stage. But this causes the program to break, because the code executes before the array is filled from the .xml file.

this is the error the program spits out : TypeError: Error #2007: Parameter url must be non-null.

using trace I see that it is trying to access the array but its undefined, because it hasnt been filled yet.

Here are the code snippets, concerning that problem.

//Main class code

//the constructor of the main code, which calls the loader class for the .xml
public function GameOfGames()
{
//create the TokenLoader Class (this automatically parses the xml file)
m_tokenLoader = new TokenLoader();

//call all the Constructor Helper Functions
openNameScreen();

createPlayers();
}

public function openNameScreen()
{
//add Name Screen MovieClip and attach the Event Listeners to it
m_NameScreen = new NameScreen();
addChild(m_NameScreen);

m_NameScreen.greenLeft_btn.addEventListener(MouseE vent.CLICK,
greenLeftButtonPress);

m_NameScreen.greenRight_btn.addEventListener(Mouse Event.CLICK,
greenRightButtonPress);

m_NameScreen.redLeft_btn.addEventListener(MouseEve nt.CLICK,
redLeftButtonPress);

m_NameScreen.redRight_btn.addEventListener(MouseEv ent.CLICK,
redRightButtonPress);

m_NameScreen.rules_btn.addEventListener(MouseEvent .CLICK,
rulesButtonPress);

m_NameScreen.start_btn.addEventListener(MouseEvent .CLICK,
startButtonPress);

//THIS IS WHERE THE CODE BREAKS, BECAUSE THE ARRAY ISNT
FILLED YET
m_NameScreen.redCircle.addChildAt(m_tokenLoader.lo adToken(0),1);
}

//SubClass Code

public function TokenLoader()
{
trace("tokenloader constructor called");

//load the image links for the tokens from the xml file
m_tokenImageXMLLoader.load(new URLRequest("GameOfGames/Tokens
/tokenSource.xml"));

//add Event Listener to know when the file is loaded

m_tokenImageXMLLoader.addEventListener(Event.COMPL ETE,
tokenImagesLoaded);
}

//this will fill the array after its loaded
private function tokenImagesLoaded(event:Event):void
{
trace("tokenimagesloaded started");

//read in the entire xml document
m_tokenImageXML = XML(event.target.data);

//parse the children into a list
m_tokenImageList = m_tokenImageXML.children();

//fill the source links array with the parsed data
for(var i:int = 0; i < m_tokenImageList.length(); i++)
{
//push each parsed entry into the links array
m_tokenLinks.push(m_tokenImageList[i].attribute("source"));
}

trace("tokenimagesloaded finished");
}

//this is the load image function that cannot access the array because it hasnt been filled yet

public function loadToken(tokenIndex:Number):Loader
{
trace(m_tokenLinks[0]);
m_tokenImageLoader = new Loader();

m_tokenImageLoader.load(new
URLRequest(m_tokenLinks[0]));

return m_tokenImageLoader;
}

Is there any way to make the code in the main class wait for the array to finish filling up before the main code moves on?

thanks for any help,
Steffen

actionscript3

Please reply at our Forum

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

© 2007 ActionScript 3.0