开发者

AS3 - Getting child by name and then by depth

开发者 https://www.devze.com 2022-12-11 19:31 出处:网络
I have 3 sprites: img1spr, img2spr, and img3spr. Inside every sprite I have some bitmaps. Every sprite has a name:

I have 3 sprites: img1spr, img2spr, and img3spr. Inside every sprite I have some bitmaps. Every sprite has a name:

img1s开发者_StackOverflow社区pr.name=name1;
img2spr.name=name2;
img3spr.name=name3;

Now the names are changing every time a function executes and what I want to know if there is a way to do something like:

getChildByName("name1").getChildAt(0)

That means that I want to find the sprite named name1, and then to find the first child inside it, or otherwise, to count the number of elements inside name1 with numChildren.

Is there a way to do this?


You are almost there - getChildByName returns a DisplayObject and you have to cast it to DisplayObjectContainer to call getChildByName and numChildren

trace(DisplayObjectContainer(getChildByName("name1")).numChildren);
DisplayObjectContainer(getChildByName("name1")).getChildAt(0);

If you are changing the names of sprites in between, you must make sure that you do not assign the same name to two objects in the child list of a parent (flash allows this). This is because getChildByName returns the first child with the given name - which may or may not be the one you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消