Comments: reader mail: loading XML

I have a flash game that I made. The levels of the game are loaded from an xml file (which is fine). The only problem is that I wish to post this game on a website (newgrounds.com) and it'll only accept the .swf. I can't load the xml files. How do I make the xml run independently without using the files? can I paste the files and put them in a funtion?

Posted by Millionaire Hoy at November 2, 2003 08:04 PM

Normally you'd setup a socket and call a function that would block (not return) until there is data available from the socket.. This basically causes java to take the thread that is running and put it on a "wait" until there is data. So if you had a thread that was running:

gXML.load(xmldata);
do something else here;

the do something else here; wouldn't run until the load function thread started running again.. In actionscript you can not have this functionality since there isn't all these threads to play around with..

Posted by Josh at July 10, 2003 04:52 PM

So, the answer to the original question ('how to "get out" of them'..."your'e pretty much stuck in a timeline of chaining functions"), is yes. You finish the chain and wait for an event (or frame) to trigger another. The emphasis that http loads await the end of a frame is much appreciated.

Then, I'd like to extend the question with a current concern of mine...since AS is single threaded what happens when I spin off tasks (function calls)? I guess they get done sequentially unless they require an http load, in which case they wait for the end of the frame. Thus, there is no need to block and make into “atomic units” any routines that can be multiply accessed.

But, what if these routines are interrupt-driven? Specifically, if a routine is executing and a user action causes an interrupt triggering re-entry into and changes to the data of the same routine. What happens? Is another instance created? I suspect *not* since there was recently significant discussions about how to use the list of listeners (to address what happens if a listener is removed while in the process of broadcasting…copy the list and broadcast to the copy was the solution, as I remember it.) All of which brings me back to blocking and atomization for multiply-accessed, interrupt-driven routines. It seem to be necessary after all!

I will look forward to all comments and suggestions. And, I'd like to thank Colin for all of us learning from his tutorial comments. (I've already shown my appreciation by buying and highly recommending your books!)


Posted by Ralph at July 9, 2003 11:34 AM

hey jon,
if you're using a nested function literal, you can store that information in a local variable. e.g.,

function getData (url) {
var gXML = new XML();
var targetClip = _root;
gXML.ignoreWhite = true;
gXML.onLoad = function (loaded) {
if (loaded) {
trace(targetClip); // Displays: _level0
} else {
trace("load error");
}
};
gXML.load(url);
}

if not, you could use a global variable, which may not be best oop practice, but gets the job done.

you could also set a property on the XML object to store the movie clip.

of course, ideally you'd want an XML.addListener() method that let you handle the load completion in another object that had the reference to the clip you want. i suppose you could subclass XML and override onData to do that.

Posted by colin at July 9, 2003 10:29 AM

what's more annoying is having to do something like this inside your onLoad handlers:

//get reference to timeline
//"this" won't work because it's the xml object
var context = eval(_target)

// ...snip...
context.component.setData(data1,data2,data3)


i can't count the number of times my xml parsing functions have "failed" because I wasn't properly referencing my timeline.

Posted by jon williams at July 9, 2003 09:29 AM

I wonder what java would do at this point:

gXML.load(xmldata); // <- does java go to sleep ?
trace(gXML.hasChildNodes());

Does it go to sleep ?

Posted by bokel at July 9, 2003 04:29 AM