PDA

View Full Version : Nesting Sprite Objects



douglaae
June 1st, 2007, 03:23 PM
How do you nest Sprite objects inside parent Sprite objects?

From a previous post that Senocular answered, I found that the way to nest MovieClip objects inside one another is:

var parentClip:MovieClip = new MovieClip();
parentClip.childClip = new MovieClip();
parentClip.addChild(parentClip.childClip);
addChild(parentClip);

This is only possible because the MovieClip class is a DYNAMIC class. Sprite on the other hand is not declared as dynamic and therefore is a "sealed" class meaning that you can't assign new variables to Sprite objects on the fly as in the above example. Sealed classes are much better for performance reasons as I understand because they "don't need to maintain an in-memory hash table." So, if I have, say, a menu that requires a hierarchical structure (nested Sprites), how would I create this?

Andy

irrationalistic
June 1st, 2007, 04:06 PM
I haven't actually checked to make sure this works, but perhaps try:

ActionScript Code:

var parentSprite:Sprite = new Sprite();
var childSprite:Sprite = new Sprite();

parentSprite.addChild(childSprite);
addChild(parentSprite);





That should work. I don't think you need to specifically declare your object as "parentClip.childClip"...

Krilnon
June 1st, 2007, 04:13 PM
Yep, that's how it works. It doesn't matter how you reference the children, you just need to call the addChild method (or another child-adding method) of the DisplayObjectContainer that you want to be the parent. That's what irrationalistic's code does.

douglaae
June 1st, 2007, 05:03 PM
If I execute trace(parentClip.childClip);, I get a compiler error. However, I was able to verify that the child clip is in fact a child of the parent with the following code. Thanks guys! I'll just have to update lots of references now from AS2 code.

var parentSprite:Sprite = new Sprite();
parentSprite.graphics.beginFill(0xc0c0c0);
parentSprite.graphics.drawRect(0, 100, 500, 300);
parentSprite.graphics.endFill();
var childSprite:Sprite = new Sprite();
childSprite.graphics.beginFill(0xff0000, .3);
childSprite.graphics.drawRect(0, 75, 50, 20);
childSprite.graphics.endFill();

parentSprite.addChild(childSprite);
addChild(parentSprite);
//If you change the x value here, the child clip moves with its parent
parentSprite.x = 400;
trace("parent size: ", parentSprite.width, " x ", parentSprite.height);

Andy

Krilnon
June 1st, 2007, 05:22 PM
If I execute trace(parentClip.childClip);, I get a compiler error.

That makes sense. Adding childClip as a child of parentClip won't make a property on parentClip with a name that is the identifier that you used to add the child.

For example:
for each(sprite in arrayOfSprites){
parentSprite.addChild(sprite);
}

It wouldn't make very much sense for parentSprite.sprite to reference anything after this loop runs, because the identifier just holds a reference to a Sprite, and parentSprite only receives the reference, not the identifier's name.

You can use Sprite#getChildByName if you want to get a reference to a Sprite by its name property, but the name is going to be something like instance1 unless you explicitly give it a name elsewhere.