AS3:复制装载机从一个对象到另一个 [英] AS3: Copying a Loader from one object to another
问题描述
我有两个班,给他们打电话A和B.它们都包含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);
}
}
我现在需要指定的加载器B的装载机(后装载完成)。
I need to now assign A's Loader to B's Loader (after the load is complete).
在其他一些I类有A和B在做装载机值的直接分配一个实例似乎不工作(显示B计算,则不会显示A的装载量)。
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.
感谢。
推荐答案
我结束了交换孩子获得同样的效果。它比我想象的要那么复杂。
I ended up swapping the children to get the same effect. It's less complicated than I thought it would be.
所以基本上B中我所做的:
So basically in B I did:
this.setChildIndex(loader,0);
这是为了确保装载机是在索引0(它可以是任何固定的指数但对我来说,我需要它出现在底部)。它没有必要在B中的装载机开始,但我只是把它放在那里,以确保有什么东西固定在索引0(所以我不小心删除了一些其他的孩子)。
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).
然后设置A的程序的时候,给B我刚换出的孩子,在指数0。
Then when setting A's loader to B I'd just swap out the child at index 0.
b.removeChildAt(0);
b.addChildAt(a.loader,0);
这不正是我想要的解决方案,但它的工作很好,到目前为止。
It's not exactly the solution I wanted but it's working well so far.
这篇关于AS3:复制装载机从一个对象到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!