如何在actionscript 3.0中通过URL加载图像

时间:2015-04-01 08:09:39

标签: actionscript-3 preloader

我已经在动作脚本2.0中测试了代码。它工作得很好,但它不支持GIF所以我希望它在动作脚本3.0中编写。  但我不知道这个。

 var current_loader: Number = 1;
 import flash.geom.*;
 var current_img: Number = 0;
 this.createEmptyMovieClip('img_01', 999);
 this.createEmptyMovieClip('img_02', 998);
 var loader: MovieClipLoader = new MovieClipLoader();
 var listener: Object = new Object();

 listener.onLoadComplete = function (target_mc: MovieClip) {
     if (target_mc._name == 'img_01') {
         img_02._visible = false;
     } else {
         img_01._visible = false;
     }
     progress_bar.visible = true;
     current_loader.opaqueBackground = 0xFF0000;
 };

 var interval: Number = setInterval(load_image, 1000);

 function load_image() {
     loader.addListener(listener);
 }

 loader.loadClip("http://google/Example3", current_loader);
 current_loader = current_loader == 1 ? 2 : 1;
 current_img = current_img == images.length - 1 ? 0 : current_img + 1;
 }

3 个答案:

答案 0 :(得分:3)

as3非常简单: 打开Flash IDE,创建新的.fla文件,选择第一帧,打开动作(f9)'复制代码。

当然在AS3中,最好使用类来放入代码。 enter image description here

import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;

var imageLoader:Loader = new Loader();
var request:URLRequest = new URLRequest("http://dummyimage.com/600x400/e000e0/fff.gif");
imageLoader.load(request);

addChild (imageLoader);

// and all possible listeners
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
imageLoader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS,  httpStatusHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
imageLoader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);

imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);


function completeHandler(event:Event):void {
    trace("completeHandler: " + event);
}

function httpStatusHandler(event:HTTPStatusEvent):void {
    trace("httpStatusHandler: " + event);
}

function initHandler(event:Event):void {
    trace("initHandler: " + event);
}

function ioErrorHandler(event:IOErrorEvent):void {
    trace("ioErrorHandler: " + event);
}

function openHandler(event:Event):void {
    trace("openHandler: " + event);
}

function progressHandler(event:ProgressEvent):void {
    trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}

请参阅http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#includeExamplesSummary

答案 1 :(得分:1)

使用直接as3有一些更简单的方法,但这种加载的强大方式可能是文件类型有很多很棒的文档。 https://greensock.com/LoaderMax-AS3

答案 2 :(得分:1)

我确实喜欢这样。它的工作非常完美。我认为这是最简单的。

 package 
    {           
     import flash.display.Loader;            
     import flash.display.Sprite;             
     import flash.net.URLRequest;           
     import flash.utils.Timer;            
     import flash.events.TimerEvent;                
     import flash.events.Event;               

public class Example extends Sprite {
    public function Example() {
        var myTimer:Timer = new Timer(1000);// 1 second
        myTimer.addEventListener(TimerEvent.TIMER,runMany);
        myTimer.start();
        function runMany(e:TimerEvent):void {
            var loader:Loader=new Loader();
            var url:String= "http://Google.Example3.com/latimage.php";
            loader.load(new URLRequest(url));
            addChild(loader);
        }
    }
  }
}