Skip to main content
Participating Frequently
November 9, 2008
Question

Accessing dynamic text inside a simple button?

  • November 9, 2008
  • 3 replies
  • 2330 views
I have created a button symbol in the library that includes a dynamic text instance called labelTxt. I've then dragged this onto the stage to form several buttons called "btn1", "btn2" etc.
I want to be able to change the contents of the text on the buttons dynamically, but I am getting quite a few errors while trying to set the text.
I have tried using (for example)
btn1.labelTxt.text=myString
but am getting errors saying that the property isn't defined.
quote:

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

I would skin a Button and use that but there are 3 quite different styles (skins) of buttons on this particular chart, and I'm not sure how to do that with Buttons. And I keep thinking there must be some easy way to access the labelTxt dynamic text in the buttons I have created.... any help would be very greatly appreciated 🙂
This topic has been closed for replies.

3 replies

Cancerinform
Inspiring
November 12, 2008
The SimpleButton class does not have the addChild() method. You will not be able to access anything inside the a button. You need to use a MovieClip.
stormieauAuthor
Participating Frequently
November 12, 2008
quote:

Originally posted by: jayclue
The SimpleButton class does not have the addChild() method. You will not be able to access anything inside the a button. You need to use a MovieClip.


I've tried using movieclips but am having trouble changing the text on the "over" and "down" frames (see explanation in my last post) and there are actually three different buttons (with different classes) so not sure how I can retrieve them as one type (see zipped files linked in last post).
Inspiring
November 9, 2008
flash.display.SimpleButton is not a dynamic class so you cannot add properties like you do. Extend the flash.display.SimpleButton - public class MySimpleButton extends SimpleButton and declare labelTxt as a property.
stormieauAuthor
Participating Frequently
November 11, 2008
Thanks for shedding some light on why I'm having problems with this. Unfortunately I'm not creating the buttons through AS, I've dragged them across to the stage, so I'm not sure how I could declare labelTxt as a property.

I'm thinking how I could do this with movieclips instead of buttons because it seems to be very difficult/impossible with simplebuttons / reading in the button labels from an XML file.

I've set up eventListeners for MOUSE_OVER, MOUSE_OUT, MOUSE_DOWN and CLICK ... and that's all working fine ... the problem comes when I try to set the text.
The text is not in the same place and is not the same colour, from the up state to the over state to the down state.
I started with a keyframe in each frame of the Btn movieclip, with a dynamic text instance called labelTxt.
I then went through the 44 buttons from the timeline and set labelTxt on them by means of a loop. This all worked fine, but when I moused over or moused down, the labelTxt on the second frame ("over") and third frame ("down") hadn't been changed, and so the labels disappeared.

So I tried naming the textfields on the second and third frames "label2Txt" and "label3Txt" and setting them to the same strings as labelTxt but then got errors because those instances don't exist when the loop runs (each button is still on frame 1 of the button movieclip timeline). So I feel like I'm going around and round in circles and thinking there simply must be a better way to do this.
I did think of putting the titles separately from the buttons and just changing the colour/position each time the button is mouseovered/clicked but there are three different colour buttons and they aren't evenly distributed (see Here for a mockup of the chart). Each button outlines, text becomes dark, button shifts to left and up, and shadow appears on mouseover.
Any suggestions for how to sort this would be very much appreciated.

Inspiring
November 11, 2008
The error indicates that the base class for the buttons is flash.display.SimpleButton so if you check the linkage of the symbol in the library this should show up. Also, you must have assigned a class name. You can write that class yourself, extending flash.display.SimpleButton and add that property labelTxt as a public (or when 'declare stage instances automatically' is on in your actionscript settings, that property will be declared for you).
stormieauAuthor
Participating Frequently
November 9, 2008
I should mention that I could use movieclips instead of buttons, but my "over" state is bigger than the "up" state, and this would cause problems if I was adding eventListeners for mouseover to a movieclip (which would be the size of the "over" state, not the "up" state. If there is an easier way of doing it, I'd love to know :)