Flash / Flex / ActionScript/Network/Loader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:19, 26 мая 2010
Содержание
- 1 Add event listener to content loader info
- 2 Adding a Loader to the display list
- 3 Add mouse click listener to a loaded swf
- 4 Displaying load progress
- 5 Handling an event announcing a loaded object"s availability
- 6 Load an image using Loader
- 7 Load-error handling
- 8 Loading and Interacting with External Movies
- 9 Loading External Images at Runtime
- 10 Main.Swf Listener Receives Notification for an Event Targeted at a Module.swf Display Object
- 11 Polling for the existence of a loaded object
Add event listener to content loader info
package {
import flash.display.*;
import flash.text.*;
import flash.net.URLRequest;
import flash.events.*;
public class Main extends Sprite {
public function Main( ) {
var loader:Loader = new Loader( );
addChild( loader );
loader.contentLoaderInfo.addEventListener( Event.OPEN, handleOpen );
loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, handleProgress );
loader.contentLoaderInfo.addEventListener( Event.ruPLETE, handleComplete );
loader.load( new URLRequest( "image.jpg" ) );
}
private function handleOpen( event:Event ):void {
trace( "open" );
}
private function handleProgress( event:ProgressEvent ):void {
var percent:Number = event.bytesLoaded / event.bytesTotal * 100;
trace( "progress, percent = " + percent );
}
private function handleComplete( event:Event ):void {
trace( "complete" );
}
}
}
Adding a Loader to the display list
package {
import flash.display.*;
import flash.net.URLRequest;
public class Main extends Sprite {
private var loader:Loader;
public function Main () {
loader = new Loader();
addChild(loader);
var urlRequest:URLRequest = new URLRequest("sunset.jpg");
loader.load(urlRequest);
}
}
}
Add mouse click listener to a loaded swf
package {
import flash.display.*;
import flash.net.*;
import flash.events.*;
public class Main extends Sprite {
private var loader:Loader;
public function Main( ) {
loader = new Loader( );
loader.load(new URLRequest("http://yoursite.ru/Module.swf"));
addChild(loader);
loader.addEventListener(MouseEvent.CLICK, clickListener);
}
private function clickListener (e:MouseEvent):void {
trace("Module.swf was clicked");
}
}
}
Displaying load progress
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.*
import flash.text.*;
public class Main extends Sprite {
private var loader:Loader; // The asset loader
private var progressOutput:TextField; // The text field in which
// to display load progress
public function Main () {
createLoader();
createProgressIndicator();
load(new URLRequest("sunset.jpg"));
}
private function createLoader ( ):void {
loader = new Loader( );
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
progressListener);
loader.contentLoaderInfo.addEventListener(Event.ruPLETE,
completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT,
initListener);
}
private function createProgressIndicator ( ):void {
progressOutput = new TextField( );
progressOutput.autoSize = TextFieldAutoSize.LEFT;
progressOutput.border = true;
progressOutput.background = true;
progressOutput.selectable = false;
progressOutput.text = "LOADING...";
}
private function load (urlRequest:URLRequest):void {
loader.load(urlRequest);
if (!contains(progressOutput)) {
addChild(progressOutput);
}
}
// Listener invoked whenever data arrives
private function progressListener (e:ProgressEvent):void {
// Update progress indicator.
progressOutput.text = "LOADING: "
+ Math.floor(e.bytesLoaded / 1024)
+ "/" + Math.floor(e.bytesTotal / 1024) + " KB";
}
private function initListener (e:Event):void {
addChild(loader.content); // Add loaded asset to display list
}
private function completeListener (e:Event):void {
removeChild(progressOutput);
}
}
}
Handling an event announcing a loaded object"s availability
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.utils.*;
public class Main extends Sprite {
private var loader:Loader;
public function Main( ) {
loader = new Loader( );
loader.contentLoaderInfo.addEventListener(Event.INIT,
initListener);
loader.load(new URLRequest("Module.swf"));
}
private function initListener (e:Event):void {
loader.content.addEventListener("ASSETS_READY",
assetsReadyListener);
}
private function assetsReadyListener (e:Event):void {
trace(Object(loader.content).t.text);
}
}
}
Load an image using Loader
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
public class LoadImage extends Sprite {
public function LoadImage() {
var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest("images/trophy.png"));
addChild(imageLoader);
}
}
}
Load-error handling
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.*
import flash.text.*;
public class Main extends Sprite {
private var loader:Loader;
private var progressOutput:TextField;
public function Main( ) {
createLoader( );
createProgressIndicator( );
load(new URLRequest("sunset.jpg"));
}
private function createLoader ( ):void {
loader = new Loader( );
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,
progressListener);
loader.contentLoaderInfo.addEventListener(Event.ruPLETE,
completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT,
initListener);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
ioErrorListener);
}
private function createProgressIndicator ( ):void {
progressOutput = new TextField( );
progressOutput.autoSize = TextFieldAutoSize.LEFT;
progressOutput.border = true;
progressOutput.background = true;
progressOutput.selectable = false;
progressOutput.text = "LOADING...";
}
private function load (urlRequest:URLRequest):void {
loader.load(urlRequest);
if (!contains(progressOutput)) {
addChild(progressOutput);
}
}
private function progressListener (e:ProgressEvent):void {
progressOutput.text = "LOADING: "
+ Math.floor(e.bytesLoaded / 1024)
+ "/" + Math.floor(e.bytesTotal / 1024) + " KB";
}
private function initListener (e:Event):void {
addChild(loader.content);
}
private function completeListener (e:Event):void {
removeChild(progressOutput);
}
private function ioErrorListener (e:IOErrorEvent):void {
progressOutput.text = "LOAD ERROR";
}
}
}
Loading and Interacting with External Movies
package {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;
public class LoaderExample extends Sprite {
private var _loader:Loader;
public function LoaderExample( ) {
_loader = new Loader( );
addChild( _loader );
_loader.contentLoaderInfo.addEventListener( Event.INIT, handleInit );
_loader.load( new URLRequest( "ExternalMovie.swf" ) );
}
private function handleInit( event:Event ):void {
var movie:* = _loader.content;
trace( movie.getColor( ) );
movie.setColor( 0xFF0000 );
}
}
}
Loading External Images at Runtime
package {
import flash.display.*;
import flash.net.URLRequest;
public class LoaderExample extends Sprite {
public function LoaderExample( ) {
var loader:Loader = new Loader( );
addChild( loader );
loader.load( new URLRequest( "image.jpg" ) );
}
}
}
Main.Swf Listener Receives Notification for an Event Targeted at a Module.swf Display Object
package {
import flash.display.*;
import flash.net.*;
import flash.events.*;
public class Main extends Sprite {
private var loader:Loader;
public function Main( ) {
loader = new Loader( );
loader.load(new URLRequest("http://yoursite.ru/Module.swf"));
addChild(loader);
}
}
}
Polling for the existence of a loaded object
package {
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.utils.*;
public class Main extends Sprite {
private var loader:Loader;
public function Main( ) {
loader = new Loader( );
loader.contentLoaderInfo.addEventListener(Event.INIT,
initListener);
loader.load(new URLRequest("Module.swf"));
}
private function initListener (e:Event):void {
var timer:Timer = new Timer(100, 0);
timer.addEventListener(TimerEvent.TIMER, timerListener);
timer.start( );
}
private function timerListener (e:TimerEvent):void {
if (loader.content.hasOwnProperty("t")) {
trace(Object(loader.content).t.text);
e.target.stop( );
}
}
}
}