这是我第一个涉及 Flash 中的外部类的项目,所以这可能是一个相当基本的问题:我正在尝试在 CSVloader 类中加载一个 csv 文件:
package Application {
import flash.events.Event;
import flash.display.Loader;
import flash.filesystem.File;
import flash.net.*;
import Application.*;
public class CSVloader {
private var waiting = true;
private var CSVdata:Array;
private var myCsvLoader:URLLoader = new URLLoader;
public function CSVloader() {
}
public function loadCSV(Args:String){
myCsvLoader.dataFormat = URLLoaderDataFormat.TEXT;
myCsvLoader.load(new URLRequest(Args));
myCsvLoader.addEventListener(Event.COMPLETE, onWait);
return CSVdata;
}
public function onCSVLoaderReady():Array {
var CSVdata:Array;
var str:String = myCsvLoader.data as String;
CSVdata = str.split("\n");
for (var i:int=0; i<CSVdata.length; i++) {
CSVdata[i] = CSVdata[i].split(';');
}
for(var q:int = 0; q<(CSVdata.length-1);q++){
CSVdata[q][1] = CSVdata[q][1].slice(0,-1);
}
trace("CSV loaded! : " + CSVdata);
return CSVdata;
}
private function onWait(e:Event):void{
CSVdata = onCSVLoaderReady();
}
}
}
如何编写 CSVloader 类,以便在调用 CSVloader("some url") 时得到一个包含所有数据的数组?
这是我的 AppController 类:
package{
import Application.CSVloader;
import Application.C;
import flash.display.MovieClip;
public class AppController extends MovieClip{
public function AppController() {
// constructor code
}
public function init(){
var myCSVLoader = new CSVloader();
trace(myCSVLoader.loadCSV("data.csv"));
}
}
}