ActionScript 3
Ładowanie plików zewnętrznych
Szukaj…
Uwagi
W niektórych przypadkach aplikacja nie może manipulować zawartością zasobów ładowanych z zewnętrznego zasobu (np. Przekształcać obrazy). Nie pamiętam na pewno, co to jest, ale jestem pewien, że jest to związane z ładowaniem treści między domenami.
Ładowanie obrazów zewnętrznych / plików SWF za pomocą modułu ładującego
Utwórz obiekt modułu ładującego:
var loader:Loader = new Loader(); //importDodaj słuchaczy do modułu ładującego. Standardowe są kompletne i błędy io / security
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); //when the loader is done loading, call this function loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadIOError); //if the file isn't found loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadSecurityError); //if the file isn't allowed to be loadedZaładuj żądany plik:
loader.load(new URLRequest("image.png"));Utwórz swoje moduły obsługi zdarzeń:
function loadComplete(e:Event):void { //load complete //the loader is actually a display object itself, so you can just add it to the display list addChild(loader) //or addChild(loader.content) to add the root content of what was loaded; } function loadIOError(e:IOErrorEvent):void { //the file failed to load, } function loadSecurityError(e:SecurityError):void { //the file wasn't allowed to load }
Ładowanie za pomocą klasy Loader jest asynchroniczne. Oznacza to, że po wywołaniu modułu
loader.loadaplikacja będzie nadal działać podczas ładowania pliku. Treść modułu ładującego nie jest dostępna, dopóki moduł ładujący nieEvent.COMPLETEzdarzeniaEvent.COMPLETE.
potrzebny import:
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
Ładowanie pliku tekstowego za pomocą FileStream (tylko środowisko wykonawcze AIR)
Prosty przykład synchronicznego odczytu pliku tekstowego UTF.
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
//First, get a reference to the file you want to load
var myFile:File = File.documentsDirectory.resolvePath("lifestory.txt");
//Create a FileStream object
fileStream = new FileStream();
//open the file
fileStream.open(myFile, FileMode.READ);
//read the data and assign it to a local variable
var fileText:String = fileStream.readUTF();
//close the current filestream
fileStream.close();