为不同的加载器重用事件监听器 Flash - AS3
我想重用我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您在每次加载时都会覆盖相同的 imageLoader,因此基本上每次调用
imageLoaded
时,您都会使用最后一个图像。在您的情况下,我将创建一个新类来处理将图像加载到指定容器中,围绕这些行:
并像这样使用它:(
尚未测试代码,可能有一些拼写错误)
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:
and use it like this:
(Haven't tested the code, might have some typos)
(看起来您已修复
Error #2007
,正如 CapelliC 指出的那样。 )要重用
loadImage()
,它必须独立于其外部的任何内容。您当前的loadImage()
需要imageLoader
,您应该在函数内声明它。imageLoaded()
也依赖于它,但由于它进入事件侦听器,您应该通过执行.addChild(e.currentTarget.loader) 来获取
而不是imageLoader
.addChild(imageLoader)
。这种范围混乱是导致您丢失另外两个加载图像的原因(注意闭包!),因为在每次函数调用时
imageLoader
都会被自身的新实例覆盖。此外,在对
imageLoaded()
进行小幅更改后,您可以使用addEventListener()
轻松传递任何变量。这就是您应该如何完成这一切:现在您可以重用
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 currentloadImage()
needsimageLoader
, which you should declare inside the function instead.imageLoaded()
is depending on it too, but since it goes in event listener, you should get theimageLoader
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 inimageLoaded()
. That's how you should do it all:Now you can reuse both
loadImage()
andimageLoaded()
.您应该内联编写事件处理程序,以传递计数器:
You should write the event handler inline, to pass your counter: