从AS2到AS3加载外部图像

时间:2022-10-28 04:52:35

I'm converting some Actionscript code from AS2 tp AS3, and I've eventually managed to get most of it to work again (it's allmost a totally different language, sharing just a little syntax similarity). One of the last things that still doesn't work, is the code for loading an external image.

我正在转换来自AS2 tp AS3的一些Actionscript代码,我最终设法让它的大部分工作再次起作用(它是一种完全不同的语言,仅仅有一点语法相似性)。用于加载外部图像的代码的最后一件事仍然不起作用。

Perhaps this has changed in AS3 but I really thought it was strange that to load an image you use loadVideo, why not loadImage? (on the other hand a flash application is constantly called a flash video even when it's not used for animation at all). This doesn't work anymore, and what I've found is a pretty complex code that is said to replace this oneliner imageholder.loadVideo(url); is this:

也许这在AS3中有所改变,但我真的认为加载图像你使用loadVideo很奇怪,为什么不加载loadImage呢? (另一方面,闪存应用程序通常被称为flash视频,即使它根本不用于动画)。这不再起作用了,我发现的是一个非常复杂的代码,据说可以取代这个oneliner imageholder.loadVideo(url);这是:

var urlreq:URLRequest = new URLRequest(url);
var theloader:Loader = new URLLoader();
theloader.load(urlreq);
theloader.addEventListener(Event.COMPLETE, function(event:Event):void {
        imageholder.addChild(theloader);
    }
);

But this doesn't work.. What I am doing wrong, and is there a more suited function to load images in AS3?

但这不起作用..我做错了什么,是否有更适合在AS3中加载图像的功能?

3 个解决方案

#1


1  

I think you're making things a little too complicated - here's what you need to load a single image:

我认为你让事情变得有点复杂 - 这就是加载单个图像所需要的:

import flash.display.Loader;
import flash.event.Event;
import flash.net.URLRequest;

var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event){
 // e.target.content is the newly-loaded image, feel free to addChild it where ever it needs to go...
});
imageLoader.load(new URLRequest('yourImage.jpg'));

... can you extrapolate from there to loading a list of images? Run that code in for-loop, each time assign a different variable (place in an array, perhaps, or property of an object) to your newly loaded e.target.content so you can access it outside of that 'onComplete'-type function.

...你能从那里推断加载图像列表吗?在for循环中运行该代码,每次为新加载的e.target.content分配一个不同的变量(可能是数组中的位置或对象的属性),以便您可以在'onComplete'类型之外访问它功能。

#2


3  

One of most common problems people have with loaders is what they listen to for events. You need to make sure you're listening to the loader.contentLoaderInfo for most events, not the loader itself. That was the main problem in the example you provided.

人们对装载机最常见的问题之一就是他们听取的事件。您需要确保您正在侦听大多数事件的loader.contentLoaderInfo,而不是加载器本身。这是您提供的示例中的主要问题。

//this is WRONG
loader.addEventListener(Event.COMPLETE, onLoad);

//this is RIGHT
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);

#3


0  

I discovered a soulution, myself.

我自己发现了一种污垢。

Initializing or unloading a list of images (actually instances of MovieClip):

初始化或卸载图像列表(实际上是MovieClip的实例):

for (i = 0; i<imgHolders.length; i++) {
    var loader:Loader = imgHolders[i].getChildByName("imgloader"+i);
    if (loader) {
        loader.unload();
    } else {
        loader = new Loader();
        loader.name = "imgloader" + i;
        // Does't seem to work, commented out.
        //loader.addEventListener(Event.COMPLETE, centerimage);
        imgHolders[i].addChild(loader);
    }
}

when I need to change the image:

当我需要更改图像时:

var loader:Loader = imgHolders[index].getChildByName("imgloader"+index);
loader.load(new URLRequest(newurl);

I tried to add a listner too to center the image, but it doesn't seem to get called? The first thing the centerimage function does is trace("centerImage"); wich doesn't appear at all...

我试图添加一个列表器来使图像居中,但它似乎没有被调用? centerimage函数做的第一件事是trace(“centerImage”);什么都没出现......

#1


1  

I think you're making things a little too complicated - here's what you need to load a single image:

我认为你让事情变得有点复杂 - 这就是加载单个图像所需要的:

import flash.display.Loader;
import flash.event.Event;
import flash.net.URLRequest;

var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event){
 // e.target.content is the newly-loaded image, feel free to addChild it where ever it needs to go...
});
imageLoader.load(new URLRequest('yourImage.jpg'));

... can you extrapolate from there to loading a list of images? Run that code in for-loop, each time assign a different variable (place in an array, perhaps, or property of an object) to your newly loaded e.target.content so you can access it outside of that 'onComplete'-type function.

...你能从那里推断加载图像列表吗?在for循环中运行该代码,每次为新加载的e.target.content分配一个不同的变量(可能是数组中的位置或对象的属性),以便您可以在'onComplete'类型之外访问它功能。

#2


3  

One of most common problems people have with loaders is what they listen to for events. You need to make sure you're listening to the loader.contentLoaderInfo for most events, not the loader itself. That was the main problem in the example you provided.

人们对装载机最常见的问题之一就是他们听取的事件。您需要确保您正在侦听大多数事件的loader.contentLoaderInfo,而不是加载器本身。这是您提供的示例中的主要问题。

//this is WRONG
loader.addEventListener(Event.COMPLETE, onLoad);

//this is RIGHT
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);

#3


0  

I discovered a soulution, myself.

我自己发现了一种污垢。

Initializing or unloading a list of images (actually instances of MovieClip):

初始化或卸载图像列表(实际上是MovieClip的实例):

for (i = 0; i<imgHolders.length; i++) {
    var loader:Loader = imgHolders[i].getChildByName("imgloader"+i);
    if (loader) {
        loader.unload();
    } else {
        loader = new Loader();
        loader.name = "imgloader" + i;
        // Does't seem to work, commented out.
        //loader.addEventListener(Event.COMPLETE, centerimage);
        imgHolders[i].addChild(loader);
    }
}

when I need to change the image:

当我需要更改图像时:

var loader:Loader = imgHolders[index].getChildByName("imgloader"+index);
loader.load(new URLRequest(newurl);

I tried to add a listner too to center the image, but it doesn't seem to get called? The first thing the centerimage function does is trace("centerImage"); wich doesn't appear at all...

我试图添加一个列表器来使图像居中,但它似乎没有被调用? centerimage函数做的第一件事是trace(“centerImage”);什么都没出现......