问题
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 http://img59.imageshack.us/img59/5002/timeliney.jpg
回答1:
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.
回答2:
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.
回答3:
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!
回答4:
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.
回答5:
Create a movieclip.
Make 3 frames, assign names for them: "out" - frame #1, "click", "over".
Add textfield to other layer like on your timeline (in first post)
Add this code to frame #1
stop();
addEventListener(MouseEvent.MOUSE_OVER, on_over, false, 0, true);
addEventListener(MouseEvent.CLICK, on_click, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, on_out, false, 0, true);
function on_out(e:MouseEvent):void
{
gotoAndStop("out");
}
function on_click(e:MouseEvent):void
{
gotoAndStop("click");
}
function on_over(e:MouseEvent):void
{
gotoAndStop("over");
}
Now, in code, you can use this clip as Super-Button (create, add click event listener, assign caption text without null exceptions, etc...)
来源:https://stackoverflow.com/questions/2517475/textfield-instance-inside-a-button-created-in-a-separate-layer-in-flash-is-null