Async operation in Actionscript

删除回忆录丶 提交于 2021-02-11 16:40:19

问题


I've read some things about async waits in Actionscript but can't really do it in practice. Here's a short, simplified version of my code I have in a class:

private function getXML():void {
    var xmlLoader:URLLoader = new URLLoader();
    xmlLoader.addEventListener(Event.COMPLETE, loadXML);
    xmlLoader.load(new URLRequest("test.xml"));
}

private function loadXML(evt:Event):void {
    var xmlData:XML = new XML(evt.target.data);
    this.parseResults(xmlData);
}

private function parseResults(resultsInput:XML):void {      
    this.text = resultsInput.Node.attributes()[0];
}

It's a standard get xml, when done fire an event and then post the first attribute to a node in the class variable text.

The function I want to call this with is:

// assume doSomething("circle") was called
private function doSomething(shape:String):String {
    this.getXML();

    if (this.text == shape) {
        // draw circle
        return "Drew circle";
    } else {
        return "Not a circle in the node";
    }
}

The problem is I always get Not a circle in the node because the processing step below getXML() executes faster than the event can load, fire, and then store into class variable to be checked. I've read everything from using anonymous functions to wait or using states or add event listeners to functions (is that possible?) but I can't implement them correctly (or did not understand how to implement correctly) so can someone give me an example of how I would do this with the above code?

Things I've tried and haven't worked:

Declaring a function variable in getXML() so the function won't be done until the xml is read

var f:Function = function loadXML(evt:Event):void {... etc.
then calling f in getXML

Doesn't work, I'm not sure how to call function variables in actionscript to makethis work

Declaring a state variable xmlLoaderState

in getXML() 
    if (this.xmlLoaderState == "waiting") {
        this.xmlLoaderState = "busy";
    }
in parseResults at the end
   this.xmlLoaderState = "waiting"
then in doSomething()
    while (this.xmlLoaderState  == "busy") {}

This infinite loops, I'm guessing it's because there's only one thread so the loop blocks the xmlLoader as well.


回答1:


There was a response here by someone else that answered your question perfectly, I don't know where it went.

Anyway, since the call is asynchronous, Flex is going to fire off the load event and then keep on trucking to the next line. What you have to do is wait for Flex to receive a response from the service call THEN you can reference the xml.

This can be accopmlished by putting the "doSomething" code inside of your Event.COMPLETE handler. That way you know the data has been loaded.




回答2:


I "solved" the argument problem by making all the arguments in that function also class variables and then using those class variables to check whether the shape is correct. This is a pretty bad abuse of namespaces but I don't know how else to do it. Even if I augment the complete event I can't get the arguments into the default URLLoader class which means I'd have to change the built-in URLLoader class to take in custom arguments and that's just way too much I feel for what seems like a typical problem.

Hopefully someone comes along and tells me a better solution, I'll be watching for it, until now this is a temp fix that gets the code to run.



来源:https://stackoverflow.com/questions/6712688/async-operation-in-actionscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!