Actionscript中的异步操作

Actionscript中的异步操作

本文介绍了Actionscript中的异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Actionscript中阅读了一些有关异步等待的内容,但实际上并没有做到.这是我在一个类中的代码的简短简化版本:

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];
}

这是标准的get xml,完成后触发一个事件,然后将第一个属性发布到类变量 text 中的节点上.

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.

我要调用的函数是:

// 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";
    }
}

问题是我总是得到在节点中不是圆,因为getXML()下面的处理步骤执行得比事件可以加载,触发然后存储到要检查的类变量中快.我已经阅读了所有内容,包括使用匿名函数等待,使用状态或将事件侦听器添加到函数中(可能吗?),但是我无法正确实现它们(或不理解如何正确实现),所以有人可以给我一个方法吗?我如何使用上面的代码执行此操作的示例?

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:

在getXML()中声明一个函数变量,以便在读取xml之前不会完成该函数

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

声明状态变量xmlLoaderState

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") {}

这个无限循环,我猜是因为只有一个线程,所以循环也阻塞了xmlLoader.

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

推荐答案

我通过使该函数中的所有参数也都是类变量,然后使用这些类变量来检查形状是否正确来解决"参数问题.这是对名称空间的严重滥用,但我不知道该怎么做.即使我增加了complete事件,我也无法将参数添加到默认的URLLoader类中,这意味着我必须更改内置的URLLoader类以接受自定义参数,这对于看起来像是我感觉太多了一个典型的问题.

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.

这篇关于Actionscript中的异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 23:15