开发者

Flash CS5 Handling "file does not exist" Error in a FileMode.READ situation

I'm working on a flash game in Flash CS5 Professional that will eventually be running on an iPhone (hence the iOS tag). I am currently designing the save game portion of the code and I'm trying to set up what happens when someone runs the game for the very first time and the save game file doesn't exist yet. Here is my code:

public class SaveGameFile extends MovieClip {
    private var file:File;
    private var savedGame:XML;

    public function SaveGameFile() {
        addEventListener(Event.ADDED_TO_STAGE, addedFileSystem);
    }

    private function addedFileSystem(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, addedFileSystem);
        file = File.applicationStorageDirectory;
        file = file.resolvePath("saveGame1.xml");
        xmlLoad();
    }

    private function xmlLoad():void {
        var fileStream:FileStream = new FileStream();
        fileStream.addEventListener(IOErrorEvent.IO_ERROR, ioReadErrorHandler);
        fileStream.open(file, FileMode.READ);
        fileStream.addEventListener(Event.COMPLETE, xmlLoadCompleteHandler);
    }

    private function xmlLoadCompleteHandler(event:Event):void {
        var fileStream:FileStream = event.target as FileStream;
        fileStream.removeEventListener(Event.COMPLETE, xmlLoadCompleteHandler);
        var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
        savedGame = XML(str);
        fileStream.close();
    }

    private function ioReadErrorHandler(event:Event):void {
        var fileStream:FileStream = event.target as FileStream;
        fileStream.removeEventListener(IOErrorEvent.IO_ERROR, ioReadErrorHandler);
        createFile();
        xmlSave();
开发者_如何学编程    }

In the ioReadErrorHandler I have two more functions that basically create the XML file and put it into the savedGame variable. Then xmlSave creates the XML file. At least this is what is supposed to happen. Right now when it gets to

fileStream.open(file, FileMode.READ);

I get the Error #3003: File or directory does not exist, which means my ioReadErrorHandler isn't doing the trick here. If I create the file and save it, then this code works perfectly and I can trace(savedGame) and it shows up just fine. But if I try to get it to create it (which as I mentioned it must when the game is first run) this is where I end up. Am I missing something here or is the IOErrorEvent.IO_ERROR not what I need to make this work?

Also once this is done (and working properly of course), will it go back to FileMode.READ and try again? Or does the error essentially break you out of the function and it needs to be run again?


As mentioned in the comment to my own post, I found the correct way to handle this synchronous operation. Here is the corrected code if anyone searching this wants to know:

public class SaveGameFile extends MovieClip {
private var file:File;
private var savedGame:XML;

public function SaveGameFile() {
    addEventListener(Event.ADDED_TO_STAGE, addedFileSystem);
}

private function addedFileSystem(event:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, addedFileSystem);
    file = File.applicationStorageDirectory;
    file = file.resolvePath("saveGame1.xml");
    xmlLoad();
}

private function xmlLoad():void {
    var fileStream:FileStream = new FileStream();
    try {
        fileStream.open(file, FileMode.READ); 
    } catch(e:IOError) {
        createFile();
        xmlSave();
    }
    var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
    savedGame = XML(str);
    fileStream.close();
}

Again "createFile():" runs a function that creates the XML file and saves it to the variable "savedGame". Then "xmlSave();" runs a function to actually save the file so xmlLoad has something to load when this is run the very first time on a new device.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜