views:

283

answers:

4

I've created an button object in flash. The button contains 2 layers. One is the background image and on top of it is a textField. The textfield is dynamic. I use the button inside a movieclip and I export it in a SWC. The I'm trying to use it in flex.

I'm trying to do this:

var myComponent:MyComponent = new MyComponent();
myComponent.button01.theTextField.text = "Caption";

I get and instance of the button(myComponent.button01 is not null in Flex debugger), but the instance of the textField(myComponent.button01.theTextField) is null and I'm not able to change the text(but the default text appears onscreen). The code is compiled correctly in flex. Does anyone has any idea what is wrong?

I exported the in swc the button control as well. So the button is not the default SimpleButton from Flash, but an derived class generated by flash(with the same name as the symbol defined in flash). It contains theTextField memeber, which is null.

Here is the button timeline(Layer 2 contains the textfield, and the textfield instance is named theTextField): alt text

A: 

It sounds like your reference path to theTextField is problematic. If you're debugging in Flex, examine the variables tab and see if you can locate myComponent, then myComponent.button01, and then myComponent.button01.theTextField.

Robusto
I did, myComponent.button01 is not null, myComponent.button01.theTextField is null. I noticed the problem but I don't know what the problem is. The default textField value is shown, so it is somewhere.
php html
+1  A: 

I believe you can not reference children of a button (SimpleButton object).

If you write a quick test in Flash, trying to modify the .text property of theTextField, I think you will get a runtime error saying something like:

1119: Access of possibly undefined property theTextField through a reference with static type flash.display:SimpleButton.

Sorry, I don't have information on how to resolve your problem. Hopefully, this will be a good starting point to help you figure out an answer to that, though.

Ross Henderson
I exported the in swc the button control as well. So the button is not the default SimpleButton from Flash, but an derived class generated by flash(with the same name as the symbol defined in flash). It contains theTextField memeber, but is null.
php html
@rhtx: Good thought. It might involve using mx_internal namespace in order to be able to address that.
Robusto
@php html: I think the derived class would still behave the same as SimpleButton, in this regard, actually. @Robusto: This doesn't seem like a namespace issue, to me (not that I'm much of an expert at all), but I'd be really interested in hearing some further thoughts on that line.
Ross Henderson
+1  A: 

Actually, there is no problem, everyhting works as it should. :-)

After you instantiate a class (new MyComponent()) the child objects are not instantiated themselves, that procedure is deffered. Only after you add the component to the display list will all the subcontrols be instantiated. So, you need to access the subcontrols only after you actually added the object to the display list.

In flex controls you have a creationComplete event that is used for just that purpose.

You can read up on details of object creation here.

jpop
+1  A: 

The problem is SimpleButton appears to be a final class. In other words, you can't addChild to it, or add any properties dynamically. I did a simple test with a Button (flash.display.SimpleButton) and a TextField. There was no way to get a handle on the TextField that I could find when it was placed on the Button in the IDE. When I made the button a MovieClip that worked great. So you have to put the Button instance on a layer underneath the TextField inside a MovieClip/Sprite Symbol. I ended up adding the Symbols through code instead, since I had two slightly different visual representations.

var button:MovieClip;
if (val1 < 10) {
    button = new LowButton();
    button.addEventListener(MouseEvent.CLICK, onLowClick);
    button.textField1.text = "You";
    button.textField2.text = "Stinker";
} else if (val1 > 10 && val1 < 100) {
    button = new MidButton();
    button.textField.text = "Middie";
... 

You get the idea!