0
既然Flash不支持多線程你怎麼在ActionScript3?如果actionscript不支持線程,如何製作一個顯示文件上傳百分比的進度條?
既然Flash不支持多線程你怎麼在ActionScript3?如果actionscript不支持線程,如何製作一個顯示文件上傳百分比的進度條?
簡短的回答是 - 通過添加一個事件監聽器來異步執行它。
長的答覆是,這是很簡單 - 如果你已經做過一次,你做了100萬次:
var site_loader:Loader;
var your_text_field:TextField;
init();
start_load();
function init():void
{
site_loader = new Loader();
this.addChild(site_loader);
your_text_field = new TextField();
this.addChild(your_text_field);
}
function start_load():void
{
site_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, update_progress_text);
site_loader.addEventListener(Event.COMPLETE, start_site);
site_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, load_error);
var url_path:String = "path/to/content.swf";
var url_request:URLRequest = new URLRequest(url_path);
site_loader.load(url_request);
}
function update_progress_text(event:ProgressEvent):void
{
var site_loader_progress:int = 100 * event.bytesLoaded/event.bytesTotal;
your_text_field.text = site_loader_progress + " %";
}
function start_site(event:Event):void
{
site_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, update_progress_text);
site_loader.removeEventListener(Event.COMPLETE, start_site);
site_loader.removeEventListener(IOErrorEvent.IO_ERROR, load_error);
}
基本處於「調度事件的ActionScript虛擬機運行時,過程事件,渲染圖形'循環;每一幀,事件都被賦予一個時間片,並且每個事件都基本上作爲一個線程(將會)在單處理器系統中運行。雖然的確是ActionScript不是多線程的,但它確實依賴於一個非常性感的事件驅動架構,在很多情況下,它是線程的更好替代方案(請查看本文的文章:http://www.stanford.edu/class/cs240/readings/threads-bad-usenix96.pdf)。這就是說,如果僅僅爲了更好地利用近乎無處不在的多處理器系統,希望AVM的未來版本能夠明確支持多線程。
ActionScript 3使用事件,您只需要每幀更新進度欄。 – 2011-06-05 20:58:21
這並不是一個明確的答案,但如果您正在尋找一個好的解決方案,請查看Greensock的LoaderMax類套件。這是[here。](http://www.greensock.com/loadermax/) – 2011-06-06 01:18:34