views:

271

answers:

1

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

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

+1  A: 

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.

Amarghosh
Great, just great. Thank you :)
slacky
What if I need to use that perticular child lets say I need to change its x values?
Fahim Akhter
@Fahim `getChildByName("thename").x = 20;`
Amarghosh
@AmarghoshI am doing this : MovieClip(competencyContainer.getChildByName(String("test")).x)+= 80;I get these two errors:1105: Target of assignment must be a reference value.5000: The class 'com.felinefrenzy.user' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Fahim Akhter
1105: Parentheses are misplaced. Also, `String("test")` is redundant - "test" is a string object. Call `MovieClip(competencyContainer.getChildByName("test")).x += 80;` 5000: This is unrelated to the given statement. Make the `com.felinefrenzy.User` class and extend it from `MovieClip` (`public class User extends MovieClip`)
Amarghosh
@Amarghosh also tried getchildByName("0").x competencyContainer.getChildByName("0").x += 80;Error #1009: Cannot access a property or method of a null object reference.
Fahim Akhter
this does not give a error at compile time, but that error comes later
Fahim Akhter
`trace(competencyContainer); trace(competencyContainer.getChildByName("0");` - which one of these give a null? Btw, you should ask this as a separate question I think.
Amarghosh
This worked : competencyContainer.getChildByName("0").x = competencyContainer.getChildByName("0").x - 80;
Fahim Akhter