Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with instance names inside Button

I've created a button in Flash, and inside that I have a TextField and a MovieClip, both with instance names set. They cover all 4 frames of the button, with no keyframes.

I found I couldn't access the objects using the instance names I'd set, so I used this piece of code to see what's going on:

var obj:DisplayObject = this.m_graphics.btnChange.upState;
for ( var i:int = 0; i < obj.numChildren; i++ )
{
    trace( "We have an object at " + i + " - " + obj.getChildAt( i ) + ": " + obj.getChildAt( i ).name );
    var t:TextField = obj.getChildAt( i ) as TextField;
    if ( t == null )
        continue;

    trace( "  The textfield has text '" + t.text + "' );
}

I get this as output:

We have an object at 0 - [object Shape]: instance195
We have an object at 1 - [object TextField]: instance199
  The textfield has text 'Change'
We have an object at 2 - [object MovieClip]: instance203

So they TextField and MovieClip are there, they've just had their instance names reset to the generic "instance###".

Anyone know what the problem is? If I make the button a MovieClip, it works fine (though I have to control the frames myself).

I'm aware of the different methods I could use to work around this, but that means changing a lot of things, and I'd like to know why SimpleButton's ignore the instance names set in Flash

EDIT

Looking into it a bit further, it seems that even though there's no keyframes in the button in the Flash IDE, Flash creates 4 instances of each item (tracing through the upState for example will give me a TextField with the instance name "instance2", while in the downState, the instance name is "instance4"). If I change the text in one state, it doesn't reflect in the others.

I'm thinking that when Flash creates the objects, it's not copying over all the properties properly (namely the instance name)

EDIT

I agree that Shane's answer is a workaround - I'd said it myself that I know of the different methods to get around the problem - but it ignores the problem, it doesn't solve it (it's the only reason why I haven't accepted it). I came to SO in the first place to see if someone perhaps knows why it's happening in the first place (tbh, it seems like a bug in the SDK).

I also understand the reasoning behind the argument "you shouldn't access children in SimpleButton; if you want more control, use Sprite or MovieClip", but I don't agree with it. The Flash IDE lets you create buttons with named instances inside them, and the SimpleButton docs give you access to the different states, so for me, this is accepted behaviour. If SimpleButton can only be used for very basic, non-changeable static buttons (think localisation as well), then it's pretty useless. You can use Sprite and MovieClip, but then you have to control the different states yourself, which gets awkward. I have my own Button class to handle the boilerplate, but I shouldn't have to rewrite basic SDK functionality, which is why for me it's a bug.

I'll keep the bounty open for the time it's there. If I get nothing else, then I'll give it to Shane.

like image 822
divillysausages Avatar asked Sep 02 '11 10:09

divillysausages


1 Answers

This is because the flash.display.SimpleButton class does not inherit from DisplayObjectContainer, and the state containers are also not DisplayObjectContainers: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html

Basically, simple buttons are by their definition simple. If you want more complex control over the inner objects you will have to use a Sprite or MovieClip with buttonMode set to true.

like image 60
shanethehat Avatar answered Sep 24 '22 03:09

shanethehat