-1

我打算使用 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() 执行后才返回。请帮忙

4

3 回答 3

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
}
于 2013-02-06T10:21:44.090 回答
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
    }
}
于 2013-02-06T10:15:07.350 回答
0

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

请参阅下面的帖子。

动作脚本休眠功能

AS3-睡眠功能

遗憾的是,没有任何 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"));
    }
}
于 2013-02-06T09:15:59.963 回答