PDA

View Full Version : another addChild mindmelter



kkos
October 9th, 2007, 04:36 PM
Please hava a look at this code:



public class Obj extends Sprite
{
public var _clip:Sprite;
public function Obj(clip:Sprite=null)
{
this._clip = clip;
}
public function attachTo(scope:DisplayObjectContainer)
{
if (this._clip)
{
scope.addChild(this); // nothing is displayed
// or scope.addChild(this._clip);
}
}
}


Now i use



var obj = new Obj(new mc());
obj.attachTo(this.container_mc);

whereas the mc() is some library instance.
Naively i would have expected that if the _clip is a member of the Obj Class i would see it on stage when using scope.addChild(this).

But this is not the case.
I have to add the clip directly by using scope.addChild(this._clip) to see it being displayed. Why is that ??

Keith

senocular
October 9th, 2007, 04:42 PM
just using scope.addChild(this); you're adding your empty Obj instance obj to this.container_mc. There are no visuals in obj. Does the mc instance (_clip) have the visuals? If so, for them to be seen, they will have to be added somewhere, either in the obj instance (whcih is then added to container_mc) or to container_mc directly

kkos
October 9th, 2007, 04:58 PM
hmmm...O.K.
This works:

this._clip = this.addChild(clip);

Thank you.

senocular
October 9th, 2007, 05:12 PM
That wont work if clip is null ;)