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.
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.