views:

1994

answers:

5

Can anyone explain the difference between the "name" property of a display object and the value found by getChildByName("XXX") function? They're the same 90% of the time, until they aren't, and things fall apart.

For example, in the code below, I find an object by instance name only by directly examining the child's name property; getChildByName() fails.

var gfx:MovieClip = new a_Character(); //(a library object exported for Actionscript)

var do1:DisplayObject = null;
var do2:DisplayObject = null;

for( var i:int = 0 ; i < gfx.amSword.numChildren ; i++ )
{
    var child:DisplayObject = gfx.amSword.getChildAt(i);
    if( child.name == "amWeaponExchange" )  //An instance name set in the IDE
    {
        do2 = child;
    }
}

trace("do2:", do2 );

var do1:DisplayObject = gfx.amSword.getChildByName("amWeaponExchange");

Generates the following output:

do2: [object MovieClip]
ReferenceError: Error #1069: Property amWeaponExchange not found on builtin.as$0.MethodClosure and there is no default value.

Any ideas what Flash is thinking?

A: 

I haven't really managed to understand what it is you're doing. But one thing I've found is that accessing MovieClip's children in the very first frame is a bit unreliable. For instance you can't gotoAndStop() and then access whatever children is on that frame, you have to wait a frame before they will be available.

grapefrukt
A: 

In one place you are looping through gfx.amSword and in another e.gfx.amSword - are you missing the e. ?

Also, it's not the cause of your problem, but class names should start a with capital letter and not include underscores. "a_Character" should just be "Character".

Iain
A: 

Oops, you're right about the e, Iain, but that's not the problem, I removed the e from the code to focus on the problem, but didn't catch that one.

I think I should post a clearer example of the failure. The funny class name is just my personal naming convention for classes auto-generated by the Flash IDE with "export for Actionscript", but it's confusing the issue.

Matt W
A: 

I misunderstood with my first answer.

This may have something to do with the Flash IDE Publish setting: "Automatically declare stage instances" in the ActionScript 3.0 Settings dialog.??

defmeta
+1  A: 

It seems you fixed it yourself!

With:

var do1:DisplayObject = gfx.amSword.getChildByName["amWeaponExchange"];

You get the error:

ReferenceError: Error #1069: Property amWeaponExchange not found on builtin.as$0.MethodClosure and there is no default value.

Because the compiler is looking for the property "amWeaponExchange" on the actual getChildByName method.

When you change it to:

var do1:DisplayObject = gfx.amSword.getChildByName("amWeaponExchange");

As you did in your edit, it successfully finds the child and compiles.

defmeta