从AS2到AS3加载外部图像

发布于 2024-07-09 10:33:23 字数 646 浏览 5 评论 0原文

我正在将一些 Actionscript 代码从 AS2 转换为 AS3,最终我成功地让其中大部分代码再次工作(它几乎是一种完全不同的语言,只是有一点语法相似性)。 最后仍然不起作用的事情之一是加载外部图像的代码。

也许这在 AS3 中已经改变了,但我真的认为使用 loadVideo 加载图像很奇怪,为什么不使用 loadImage? (另一方面,Flash 应用程序经常被称为 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);
    }
);

但这不起作用..我做错了什么,有没有更适合在 AS3 中加载图像的函数?

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.

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2024-07-16 10:33:23

人们在使用加载程序时遇到的最常见问题之一是他们监听事件的内容。 您需要确保您正在侦听大多数事件的 loader.contentLoaderInfo,而不是加载程序本身。 这是您提供的示例中的主要问题。

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

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

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.

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

//this is RIGHT
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
满栀 2024-07-16 10:33:23

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

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'));

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

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.

浅笑依然 2024-07-16 10:33:23

我自己发现了一个解决方案。

初始化或卸载图像列表(实际上是 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);
    }
}

当我需要更改图像时:

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

我也尝试添加一个列表器以使图像居中,但它似乎没有被调用? centerimage 函数做的第一件事是 trace("centerImage"); ,它根本没有出现......

I discovered a soulution, myself.

Initializing or unloading a list of images (actually instances of 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文