I'm trying to create a user license agreement that will show the first time a player opens my game. I'm using a FileStream with a complete event and an IOEvent. The IOEvent is triggered the first time because the file I'm looking for doesn't exist. I then show the user agreement and write the file to the application storage directory. If I then close the game and open it a second time I expect the Complete event to fire since the file now exists but instead the IOEvent is firing again. Why isn't it finding the file and triggering the complete event? I can read the file after I create it so I know it exists at that point.
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.utils.ByteArray;
//load ula file
var file:File = File.applicationStorageDirectory;
file.resolvePath("version.txt");
var version:String = "1.0";
var stream:FileStream = new FileStream();
stream.addEventListener(Event.COMPLETE, checkVersion);
stream.addEventListener(IOErrorEvent.IO_ERROR, noVersion);
stream.openAsync(file,FileMode.UPDATE);
function checkVersion(e:Event):void{
testText.text += "found file";
stream.removeEventListener(Event.COMPLETE, checkVersion);
stream.removeEventListener(IOErrorEvent.IO_ERROR, noVersion);
var ver:String = e.target.readUTFBytes(e.target.bytesAvailable);
if(ver == version.toString()){
stream.close();
file = null;
stream = null;
testText.text += "goto splash screen";
//gotoAndStop("SplashScreen");
}
else{
stream.writeUTFBytes(String(version));
stream.close();
file = null;
stream = null;
testText.text += "wrong version number";
//ula.testText.text = "writing file version = "+version;
//ula.visible = true;
}
}
function noVersion(e:IOErrorEvent):void{
stream.removeEventListener(Event.COMPLETE, checkVersion);
stream.removeEventListener(IOErrorEvent.IO_ERROR, noVersion);
testText.text += "file doesn't exist error = "+e.text;
stream.close();
file = null;
stream = null;
var file2:File = File.applicationStorageDirectory.resolvePath("version.txt");
var stream2:FileStream = new FileStream();
stream2.open(file2, FileMode.WRITE);
stream2.writeUTFBytes(version);
stream2.close();
file2 = null;
stream2 = null;
testText.text += " created file";
}
North America
Europe, Middle East and Africa
Asia Pacific