Flash as3 Open a File Locally (with FlashPunk)
Okay, so at the moment I am making a game in as3 with the FlashPunk framework. I have managed to set up ogmo editor to work with it, and the results have been good! I intend to send the ogmo project to some friends, so that they can make some levels for my game themselves. But I have run into a problem. I wanted my friends to be able to test the levels they make, so I set about trying to find a simple open file dialog solution, Sp that they can select the generated XML file (.oel), and then load it in to the actual game. But I just couldn't find a solution! Can a开发者_如何学Cnyone help me? Thanks in advance.
If your game is web based, you have to use the FileReference class. If it's air based, you can use the File class. Like so:
File Reference:
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;
public class FileReference_event_select extends Sprite {
private var uploadURL:URLRequest;
private var file:FileReference;
public function FileReference_event_select() {
uploadURL = new URLRequest();
uploadURL.url = "http://www.[yourDomain].com/yourUploadHandlerScript.cfm";
file = new FileReference();
file.addEventListener(Event.SELECT, selectHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
file.browse();
}
private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
trace("selectHandler: name=" + file.name + " URL=" + uploadURL.url);
file.upload(uploadURL);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}
private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
}
}
File:
import flash.filesystem.File;
import flash.events.Event;
var sourceFile:File = File.documentsDirectory;
sourceFile = sourceFile.resolvePath("Apollo Test/test1.txt");
var destination:File = File.documentsDirectory;
destination = destination.resolvePath("Apollo Test/test2.txt");
Sources:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html
精彩评论