完成后改变状态

时间:2010-12-14 19:41:47

标签: air flex4 state flashbuilder4

使用Flash Builder 4(4.1 flex)。 在我的函数完成运行后尝试创建状态更改,这将更改屏幕以使“完成”按钮关闭应用程序。我如何告诉空中应用程序这样做?
我发现了这个:受保护的函数setState(newState:String):对于AS3是void,但在flex 4上不起作用。它应该在文件数组完成后将状态更改为Finished。

        private function initiate_download(event:MouseEvent):void
        {
            var filearray:Array = new Array();
            filearray[0]="00.jpg";
            filearray[1]="01.jpg";
            filearray[2]="02.jpg";
            for (var i:uint; i < filearray.length; i++) {
                var remoteURL = "http://domain/" + filearray[i];
                var localURL = "C:/dir/" + filearray[i];
                downloadFile(remoteURL, localURL);
            }


            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.WRITE);
                urlStream.load(request);

                // Add event listeners
                urlStream.addEventListener(Event.COMPLETE, finishWriteFile);
                urlStream.addEventListener(ProgressEvent.PROGRESS, writeFile);
            }
    ]]>
</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="Finished"/>
</s:states>
<mx:Image x="0" y="0" source="background.jpg"/>
<s:Button x="85" y="85" includeIn="Start" label="Update" id="download" click="initiate_download(event)" color="#FFFFFF" fontSize="30"/>
<s:Button x="131" y="85" includeIn="Finished" label="Finished" id="closer" click="close_window(event)" color="#FFFFFF" fontSize="30"/>

1 个答案:

答案 0 :(得分:0)

关闭fileStream和urlStream后,您可以更改状态

this.currentState = "Finished"

现在应该可以看到关闭按钮。