如何防止程序进一步执行直到as3中的事件完成?

时间:2013-02-06 09:07:19

标签: php actionscript-3

我打算使用php收集数据,但目前我正在从htm文件中检索数据:代码如下:

var Data:String;
Data=getData("file:///C:/..../try.htm");
trace(Data);

function getData(url:String):String
{

    var phpFileRequest:URLRequest = new URLRequest(url);
    var phpLoader:URLLoader = new URLLoader();
    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

    phpLoader.addEventListener(Event.COMPLETE, showResult);
    phpLoader.load(phpFileRequest);
    var a:int=1;
    trace("Worked");


    function showResult (event:Event):void
    {

        import flash.text.*;
        Data= "" + event.target.data.systemResult;
        trace(Data);


    }
return Data;
}

现在函数调用执行,添加事件监听器,然后返回主代码并尝试跟踪变量Data,这将导致null,稍后当事件完成时,它将执行该函数。我想要的是只有在事件完成并且showResult()执行后才返回的程序控件。请帮忙

3 个答案:

答案 0 :(得分:1)

您无法在Flash中进行同步下载。为了等待事件而不使用处理能力,请使用输入框架侦听器和标志。一个例子:

var Data:String;
var weAreReady:Boolean=false;
getData("file:///C:/..../try.htm");
addEventListener(Event.ENTER_FRAME,enterFrame);

function getData(url:String):void
{

var phpFileRequest:URLRequest = new URLRequest(url);
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}

function showResult(event:Event):void
{
    import flash.text.*;
    Data= "" + event.target.data.systemResult;
    trace(Data);
    weAreReady=true;
}
function enterFrame(e:Event):void {
    if (weAreReady) {
        removeEventListener(Event.ENTER_FRAME, enterFrame);
        goFurther(); // this will be called when the data will be finally ready
    }
}

答案 1 :(得分:1)

我只想在Vesper的回答中添加一些东西。使用EnterFrame毫无意义,因为事件正在执行相同的工作。只需等待事件在您执行任何其他操作之前触发。

var Data:String;
var weAreReady:Boolean=false;
getData("file:///C:/..../try.htm");

function getData(url:String):void
{

var phpFileRequest:URLRequest = new URLRequest(url);
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}

function showResult(event:Event):void
{
    Data= "" + event.target.data;
    trace(Data);
    goFurther(); // this will be called when the data will be finally ready
}

如果您想同时调用不同的请求并希望等待触发所有事件,请再次避免在混合中添加EnterFrame或Timer。首先,我会避免多个同时发出的请求并将其排队,似乎是控制整个事情的更好方法。但无论如何你想要这样做,我会这样写:

var Data:String;
var _total:int; //number of events
var _done:int;  //number of events triggered
var weAreReady:Boolean=false;
_total = 4;
_done = 0;

getData("file:///C:/..../try.htm");
getData("file:///C:/..../try2.htm");
getData("file:///C:/..../try3.htm");
getData("file:///C:/..../try4.htm");

function getData(url:String):void
{

var phpFileRequest:URLRequest = new URLRequest(url);
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

phpLoader.addEventListener(Event.COMPLETE, showResult);
phpLoader.load(phpFileRequest);
}

function showResult(event:Event):void
{
    Data= "" + event.target.data;
    trace(Data);
    if(++_done == _total) goFurther(); // this will only be called when all the events have been triggered
}

答案 2 :(得分:0)

在As3.0中只使用while()循环等待函数。

请参阅以下帖子。

Action Script Sleep function

AS3-sleep-function

遗憾的是,没有任何ActionScript / Flash与睡眠功能等效。

我建议在代码后更改代码。

var Data:String;
getData("file:///C:/..../try.htm");

this.addEventListener("received", receivedHandler);
var isReceived:Boolean = false;
function receivedHandler(e:Event):void
{
    isReceived = true;
    trace("Data: ", Data);
    //do stuff
}

function getData(url:String):void
{

    var phpFileRequest:URLRequest = new URLRequest(url);
    var phpLoader:URLLoader = new URLLoader();
    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

    phpLoader.addEventListener(Event.COMPLETE, showResult);
    phpLoader.load(phpFileRequest);
    var a:int=1;
    trace("Worked");

    function showResult (event:Event):void
    {
        import flash.text.*;
        Data= "" + event.target.data.systemResult;
        dispatchEvent(new Event("received"));
    }
}