Хорошо. Я не нашел способа «приостановить» выполнение функции в массиве.
Вместо этого я подумал об использовании счетчика файлов, поэтому не имеет значения, в каком порядке они были загружены, если все 11 загружены, а затем загружают конечное состояние.
Вот мой код для справки других:
<fx:Script>
<![CDATA[
private function close_window(event:MouseEvent):void
{
this.nativeApplication.exit();
}
private function finalScreen():void
{
this.currentState="Finished";
}
private function initiate_download(event:MouseEvent)
{
var filearray:Array = new Array();
filearray[0]="0.jpg";
filearray[1]="1.exe";
filearray[2]="02.jpg";
filearray[3]="2.exe";
filearray[4]="04.jpg";
filearray[5]="05.jpg";
filearray[6]="4.exe";
filearray[7]="07.jpg";
filearray[8]="08.jpg";
filearray[9]="09.jpg";
filearray[10]="3.exe";
for(var i:uint; i < filearray.length; i++)
{
var remoteURL = "http://domain/dir/" + filearray[i];
var localURL = "C:/dir/" + filearray[i];
downloadFile(remoteURL, localURL);
}
var filenumber = 0;
function downloadFile(url, filename)
{
// Create the stream for the data request
var urlStream = new URLStream();
// Used to initiate request for remote file
var request = new URLRequest(url);
// Create file stream
var fileStream = new FileStream();
// Create a reference to a location on disk
var file = File.desktopDirectory.resolvePath(filename);
// Called as download progresses
var writeFile = function()
{
// Write to file
if (urlStream.bytesAvailable > 51200)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
return true;
}
// Called when download completes
var finishWriteFile = function()
{
// Write to file
if(urlStream.bytesAvailable > 0)
{
var dataBuffer = new ByteArray();
urlStream.readBytes(dataBuffer, 0, urlStream.bytesAvailable);
fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
}
// Close streams
fileStream.close();
urlStream.close();
return true;
}
// Initiate download
fileStream.openAsync(file, FileMode.UPDATE);
urlStream.load(request);
// Add event listeners
urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
fileStream.addEventListener(Event.CLOSE, updateProgress);
function updateProgress(event:Event):void
{
filenumber = filenumber+1;
progbar.setProgress(filenumber*10, 110);
if(10*filenumber == 110)
{
finalScreen();
}
}
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:states>
<s:State name="Start"/>
<s:State name="Working"/>
<s:State name="Finished"/>
</s:states>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="50" y="110" includeIn="Start" label="Update" id="download" click="currentState='Working'; initiate_download(event)" color="#FFFFFF" fontSize="30" width="300" height="40"/>
<s:Button x="50" y="110" includeIn="Finished" label="Close Window" id="closer" click="close_window(event)" color="#FFFFFF" fontSize="30" width="300" height="40"/>
<s:RichText includeIn="Start" x="-22" y="85" text="Press Update to begin." width="444" textAlign="center" fontSize="14" id="starttext" color="#FFFFFF"/>
<s:RichText includeIn="Working" x="-22" y="85" text="Downloading and updating files...." width="444" textAlign="center" fontSize="14" id="workingtext" color="#848484"/>
<s:RichText includeIn="Finished" x="109" y="85" text="Update completed." width="191" textAlign="center" fontSize="14" id="finishedtext" color="#49D30C"/>
<mx:ProgressBar includeIn="Working" id="progbar" mode="manual" x="121" y="107" fontSize="18" textAlign="center"/>