为不同的加载器重用事件监听器 Flash - AS3

发布于 2024-12-11 18:55:44 字数 1580 浏览 0 评论 0原文

我想重用我的 loadImage 函数,该函数传递一个 url 和一个字符串值,并将图像加载到影片剪辑中。

我有多个图像想要加载到不同的影片剪辑中。我以为我可以用我的 addEventListener(Event.COMPLETE, 传递一个变量,但它只在我第一次加载图像时起作用,下面是我的代码,下面是我的错误:

AS3

    //CALL loadImage  FUNCTION PASS URL TO LOCAL IMAGE AND UNIQUE NUMBER FOR EVENTLISTENER


loadImage('\\assets\\' + rootPath + '\\images\\bg.jpg',"1");
loadImage('\\assets\\' + rootPath + '\\images\\english.png',"2");
loadImage('\\assets\\' + rootPath + '\\images\\spanish.png',"3");

var imageLoader:Loader;

function loadImage(url:String,inc:String):void {
    trace(inc);
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
    function(e:Event) : void { imageLoaded(inc) });

}

function imageLoaded(counter:String):void {
    trace(counter);
if (counter == "1"){

    trace("Background");
background.addChild(imageLoader);
}else if (counter == "2"){

    trace("english");

englishMC.addChild(imageLoader);
}else if (counter == "3"){

    trace("Spanish");

spanishMC.addChild(imageLoader);
}
}

错误:

TypeError: Error #2007: Parameter listener must be non-null.
    at flash.events::EventDispatcher/addEventListener()
    at Function/<anonymous>()
    at EndCap_fla::MainTimeline/processXML()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

我知道我可以为每个加载的图像设置单独的函数,但如果可能的话,我希望能够重用 loadImage 函数

I want to reuse my loadImage function that is passed a url and a string value and it loads an image into a movie clip.

I have multiple images that I want to load into different movieClips. I thought I could just pass a variable with my addEventListener(Event.COMPLETE, but it only works the first time I load an image below is my code and below that is my error:

AS3

    //CALL loadImage  FUNCTION PASS URL TO LOCAL IMAGE AND UNIQUE NUMBER FOR EVENTLISTENER


loadImage('\\assets\\' + rootPath + '\\images\\bg.jpg',"1");
loadImage('\\assets\\' + rootPath + '\\images\\english.png',"2");
loadImage('\\assets\\' + rootPath + '\\images\\spanish.png',"3");

var imageLoader:Loader;

function loadImage(url:String,inc:String):void {
    trace(inc);
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
    function(e:Event) : void { imageLoaded(inc) });

}

function imageLoaded(counter:String):void {
    trace(counter);
if (counter == "1"){

    trace("Background");
background.addChild(imageLoader);
}else if (counter == "2"){

    trace("english");

englishMC.addChild(imageLoader);
}else if (counter == "3"){

    trace("Spanish");

spanishMC.addChild(imageLoader);
}
}

error:

TypeError: Error #2007: Parameter listener must be non-null.
    at flash.events::EventDispatcher/addEventListener()
    at Function/<anonymous>()
    at EndCap_fla::MainTimeline/processXML()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

I know I could just have seperate functions for each image loaded but if possible I would like to be able to reuse the loadImage function

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

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

发布评论

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

评论(3

冷︶言冷语的世界 2024-12-18 18:55:45

问题是您在每次加载时都会覆盖相同的 imageLoader,因此基本上每次调用 imageLoaded 时,您都会使用最后一个图像。

在您的情况下,我将创建一个新类来处理将图像加载到指定容器中,围绕这些行:

class ResourceLoader {
    private var target: Container;
    private var loader: Loader;

    function ResourceLoader(target:Container, image: String) {
        loader = ...
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    }

    function onComplete(e: Event):void {
        target.addChild(loader);
    }
}

并像这样使用它:(

new ResourceLoader(background, '\\assets\\' + rootPath + '\\images\\bg.jpg');

尚未测试代码,可能有一些拼写错误)

The problem is you are overriding the same imageLoader on each load, so basically everytime the imageLoaded is called, you will use the last image.

In your case, I would create a new class to handle the loading of an image into a specified container, something around these lines:

class ResourceLoader {
    private var target: Container;
    private var loader: Loader;

    function ResourceLoader(target:Container, image: String) {
        loader = ...
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    }

    function onComplete(e: Event):void {
        target.addChild(loader);
    }
}

and use it like this:

new ResourceLoader(background, '\\assets\\' + rootPath + '\\images\\bg.jpg');

(Haven't tested the code, might have some typos)

尛丟丟 2024-12-18 18:55:45

(看起来您已修复 Error #2007,正如 CapelliC 指出的那样。 )

要重用 loadImage(),它必须独立于其外部的任何内容。您当前的 loadImage() 需要 imageLoader,您应该在函数内声明它。

imageLoaded() 也依赖于它,但由于它进入事件侦听器,您应该通过执行 .addChild(e.currentTarget.loader) 来获取 imageLoader 而不是 .addChild(imageLoader)

这种范围混乱是导致您丢失另外两个加载图像的原因(注意闭包!),因为在每次函数调用时 imageLoader 都会被自身的新实例覆盖。

此外,在对 imageLoaded() 进行小幅更改后,您可以使用 addEventListener() 轻松传递任何变量。这就是您应该如何完成这一切:

loadImage("\\assets\\" + rootPath + "\\images\\bg.jpg", "1");
loadImage("\\assets\\" + rootPath + "\\images\\english.png", "2");
loadImage("\\assets\\" + rootPath + "\\images\\spanish.png", "3");

function loadImage(url:String, inc:String):void {
  trace(inc);
  var imageLoader:Loader = new Loader();
  imageLoader.load(new URLRequest(url));
  imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded(inc));
}

function imageLoaded(counter:String):Function {
  return function(e:Event):void {
    trace(counter);
    if (counter == "1") {
      trace("Background");
      background.addChild(e.currentTarget.loader);
    } else if (counter == "2") {
      trace("english");
      englishMC.addChild(e.currentTarget.loader);
    } else if (counter == "3") {
      trace("Spanish");
      spanishMC.addChild(e.currentTarget.loader);
    }
    //e.currentTarget.removeEventListener(Event.COMPLETE, arguments.callee);
  }
}

现在您可以重用 loadImage()imageLoaded()

(It looks like you fixed that Error #2007 as CapelliC pointed it out.)

To reuse loadImage(), it has to be independent from anything outside it. Your current loadImage() needs imageLoader, which you should declare inside the function instead.

imageLoaded() is depending on it too, but since it goes in event listener, you should get the imageLoader by doing .addChild(e.currentTarget.loader) instead of .addChild(imageLoader).

That scope confusion is what's making you loose the two other loaded images (beware the closures!), because imageLoader is being overrided by a new instance of itself on every function call.

Also, you can easily pass any variable with your addEventListener() after a small change in imageLoaded(). That's how you should do it all:

loadImage("\\assets\\" + rootPath + "\\images\\bg.jpg", "1");
loadImage("\\assets\\" + rootPath + "\\images\\english.png", "2");
loadImage("\\assets\\" + rootPath + "\\images\\spanish.png", "3");

function loadImage(url:String, inc:String):void {
  trace(inc);
  var imageLoader:Loader = new Loader();
  imageLoader.load(new URLRequest(url));
  imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded(inc));
}

function imageLoaded(counter:String):Function {
  return function(e:Event):void {
    trace(counter);
    if (counter == "1") {
      trace("Background");
      background.addChild(e.currentTarget.loader);
    } else if (counter == "2") {
      trace("english");
      englishMC.addChild(e.currentTarget.loader);
    } else if (counter == "3") {
      trace("Spanish");
      spanishMC.addChild(e.currentTarget.loader);
    }
    //e.currentTarget.removeEventListener(Event.COMPLETE, arguments.callee);
  }
}

Now you can reuse both loadImage() and imageLoaded().

铜锣湾横着走 2024-12-18 18:55:44

您应该内联编写事件处理程序,以传递计数器:

function loadImage(url:String,inc:String):void {
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
    function(e:Event) : void { imageLoaded(inc) });
}

You should write the event handler inline, to pass your counter:

function loadImage(url:String,inc:String):void {
imageLoader = new Loader();
imageLoader.load(new URLRequest(url));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
    function(e:Event) : void { imageLoaded(inc) });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文