AS3:将 Loader 从一个对象复制到另一个对象
我有两个类,分别称为 A 和 B。它们都包含一个 Loader 对象。在 AI 类中将内容加载到 Loader 对象中。
public class A {
var loader:Loader;
public function A():void {
loader = new Loader();
this.addChild(loader);
loader.load(...);
}
}
public class B() {
var loader:Loader;
public function B():void {
loader = new Loader();
this.addChild(loader);
}
}
我现在需要将 A 的加载器分配给 B 的加载器(加载完成后)。
在其他一些类中,我有 A 和 B 的实例。直接分配加载器值似乎不起作用(显示 B 时,不显示 A 的加载器内容)。
var b:B = new B();
var a:A = new A();
// ... I wait for a's loader to be loaded ...
// ... then ...
b.loader = a.loader;
addChild(b);
// A's Loader is not shown ...
如果我执行以下操作,它会起作用:
b.addChild(a.loader);
但这不是我想要的。当我知道我只有一个孩子但只想更改其内容时,我不想管理一堆孩子。
有没有办法直接复制/分配Loader?我尝试分配给“内容”,但它是只读的。我还有其他方法可以做到这一点吗?基本上我有一个 Loader 对象数组,所有对象都加载了图像。然后,我在舞台上有一个加载器,我想将数组中的图像(加载器)分配给它。
谢谢。
I have two classes, call them A and B. They both contain a Loader object. In class A I load content into the Loader object.
public class A {
var loader:Loader;
public function A():void {
loader = new Loader();
this.addChild(loader);
loader.load(...);
}
}
public class B() {
var loader:Loader;
public function B():void {
loader = new Loader();
this.addChild(loader);
}
}
I need to now assign A's Loader to B's Loader (after the load is complete).
In some other class I have an instance of A and B. Doing a direct assignment of the loader values doesn't seem to work (when showing B, A's loader content is not displayed).
var b:B = new B();
var a:A = new A();
// ... I wait for a's loader to be loaded ...
// ... then ...
b.loader = a.loader;
addChild(b);
// A's Loader is not shown ...
If I do the following, it works:
b.addChild(a.loader);
But that's not what I want. I don't want to manage a bunch of children when I know I only have one child but just want to change its content.
Is there a way to copy/assign a Loader directly? I tried assigning to 'content' but that's read-only. Is there another way I can do this? Basically I have an array of Loader objects that all get loaded with images. I then have a single Loader that's on the stage and I want to assign to it images (Loaders) from my array.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为了好玩做了这样的事情:
使用加载器的 loadBytes 和 loaderInfo 的 字节 我创建了一个无限循环,其中 a 加载 a、加载 b、加载 a、加载 b、加载 a、加载 b...然后循环整体开始出现。
不要这样做!
这不是您所需要的,您正在处理图像,因此加载程序内容 应该是位图。如果您有权访问像素(例如,跨域策略和新的 LoaderContext(true) 作为 load() 调用中的第二个参数),您可以访问和克隆 bitmapData。
所以你的答案应该很简单:
这不是 OOP 代码,它只是一些最小的东西,所以你可以轻松地得到图片:)
I did something like this for fun:
using loader's loadBytes and loaderInfo's bytes I create an endless loop where a loads a, loads b, loads a, load b, loads a, loads b...and loop wholes start to appear.
DON'T DO THAT!
That is not what you need, you're working with images, so the loader content should be a Bitmap. You can access and clone the bitmapData, given you have permission to access the pixels(e.g. crossdomain policy and a new LoaderContext(true) as the second parameter in the load() call).
So your answer should be as simple as:
It's not the OOP code, it's just something minimal so you can easily get the picture :)
我最终交换了孩子以获得相同的效果。它没有我想象的那么复杂。
所以基本上在 BI 中是这样的:
这是为了确保加载器位于索引 0。(它可以是任何固定索引,但在我的情况下,我需要它出现在底部)。最初没有必要在 B 中有一个加载程序,但我只是将它放在那里以确保索引 0 处有固定的内容(这样我就不会意外删除其他子项)。
然后,当将 A 的加载程序设置为 B 时,我只需交换索引 0 处的子进程。
这并不完全是我想要的解决方案,但到目前为止效果良好。
I ended up swapping the children to get the same effect. It's less complicated than I thought it would be.
So basically in B I did:
This is to make sure the loader is at index 0. (It can be any fixed index but in my case I needed it to appear at the bottom). It's not necessary to have a loader in B initially but I just put it there to make sure there's something fixed at index 0 (so I don't accidentally remove some other child).
Then when setting A's loader to B I'd just swap out the child at index 0.
It's not exactly the solution I wanted but it's working well so far.
据我了解,您想为 b 类创建一个 setLoader 方法。然后
,当 a 被加载时,请
注意 a.loader 将不再是 a 的子级(如果显示 a,它将是空的),但您仍然可以使用 a 来访问它.loader 和 b.loader。
告诉我这是否有帮助。
From what I understand you want to create a setLoader method for your b class. something like
you can then, when a is loaded, call
be aware that a.loader will not be a child of a anymore (if you display a, it will be empty), but you will still be able to access it with both a.loader AND b.loader.
tell me if this helps.