View Full Version : Labels on SimpleButtons?
juniorspecial
January 11th, 2009, 07:05 PM
How can I get a wee bit of text onto a SimpleButton so that it appears to have a label?
ayumilove
January 11th, 2009, 09:04 PM
import fl.controls.Button;
var myButton:Button = new Button();
myButton.label = "ayumilove";
addChild(myButton);
Useful Link:
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/fl/controls/Button.html#includeExamplesSummary
juniorspecial
January 11th, 2009, 09:12 PM
I'm appreciate your help, but I'm asking about the flash.display.SimpleButton class, not the fl.controls.Button class.
I'm wondering how I can put some text onto a SimpleButton instance, so that it looks like a label for the button.
ayumilove
January 11th, 2009, 09:26 PM
import flash.text.TextField;
import flash.text.TextFormat;
var ayumiText:TextField;
ayumiText = new TextField();
ayumiText.textColor = 0xFFFFFF;
ayumiText.text = "Hello Ayumilove";
ayumiText.selectable = false;
ayumiText.background = true;
ayumiText.backgroundColor = 0x000000;
ayumiText.autoSize = TextFieldAutoSize.LEFT;
addChild(ayumiText);
Michael Chen
January 11th, 2009, 09:31 PM
There isn't a label property in SimpleButton.
You could just create a dynamic textfield over it?
juniorspecial
January 11th, 2009, 09:39 PM
Thanks Michael--there isn't a label property. Also, there's no addChild() method, is there?
It seems strange to me that there is a class for making these SimpleButtons, but within it there is no way to attach a textField to it, add a label to it, or assign a value to a text property that would put a simple word on the SimpleButton?
Am I right in thinking there's no other way to put text on the SimpleButton than to position a textField over the spot where the SimpleButton happens to be?
ayumilove
January 11th, 2009, 09:39 PM
SimpleButton does not have label, but you could make your custom text over it ^^
Reference
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/SimpleButton.html#includeExamplesSummary
senocular
January 12th, 2009, 10:32 AM
You could also add text to each of its states.
In Flash authoring, you can create a new layer that spans all states and just put one text field there.
juniorspecial
January 12th, 2009, 10:37 AM
Thanks for your input, Senocular! You're very knowledgeable, and I defer to your extensive insight.
How would I add text to each of the states in ActionScript? I can't get my thick head around how to do that.
You make a good point about how to add text in Flash authoring. I hadn't thought of that. But, I wanted to just deal with ActionScript for this.
senocular
January 12th, 2009, 10:46 AM
A simple button is defined by its DisplayObject state properties (downState, overState, upState). When you define those, simply define them with a text field :).
juniorspecial
January 12th, 2009, 10:58 AM
So, I would, say, define downState as a Sprite, and add a text field to this Sprite?
Or are you saying define the downState with just a text field, and use the properties of the text field to define how the button looks?
Or would either of these methods work?
ken1171
January 13th, 2009, 06:09 AM
I have a similar question about text labels on SimpleButtons.
I want to make a dynamic pop-up menu using SimpleButtons and I have to create them dynamically because I don't know how many buttons will be needed at every situation. I created a base button with text label in the authoring environment, but I noticed the label text *cannot* be changed dynamically once the button is created and added to the library. I have the TextField an instance name but it becomes unavailable in runtime.
What would be a better solution for a dynamic menu that responds to the mouse like SimpleButtons do?
devonair
January 13th, 2009, 08:16 AM
quickie example:
package {
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class MyButton extends SimpleButton {
public static const OVER_COLOR:uint = 0x999999;
public static const UP_COLOR:uint = 0x666666;
public static const DOWN_COLOR:uint = 0x886600;
private var _label:String;
public function MyButton(label:String):void {
_label = label;
downState = createSprite(DOWN_COLOR);
overState = createSprite(OVER_COLOR);
upState = createSprite(UP_COLOR);
hitTestState = overState;
}
private function createSprite(color:uint):Sprite {
var s:Sprite = new Sprite();
s.graphics.beginFill(color);
s.graphics.drawRoundRect(0, 0, 100, 18, 10, 10);
s.graphics.endFill();
var lab:TextField = createLabel();
lab.x = Math.round(s.width * .5 - lab.width * .5);
lab.y = Math.round(s.height * .5 - lab.height * .5);
s.addChild(lab);
return s;
}
private function createLabel():TextField {
var fmt:TextFormat = new TextFormat("_sans", 11);
var tf:TextField = new TextField();
tf.selectable = false;
tf.mouseEnabled = false;
tf.autoSize = TextFieldAutoSize.LEFT;
tf.defaultTextFormat = fmt;
tf.text = _label;
return tf;
}
}
}
juniorspecial
January 15th, 2009, 12:10 PM
Here's what I get when I run that code:
5000: The class 'MyButton' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
devonair
January 16th, 2009, 03:24 AM
It's not intended to be linked to anything in the library.
DilutedImage
February 5th, 2009, 02:19 PM
This is old now, but the thread has proven useful. Thank you devonair for the code you posted. It works great, except that the text doesn't appear. I've tried changing the font, embedding the font, changing the color, etc ... Any thoughts on this?
Thanks!
-[di]
DilutedImage
February 5th, 2009, 03:43 PM
I figured it out. The code worked fine. Once inside a MovieClip that is masked via the Flash IDE though, the TextField disappears. If masked via ActionScript though, there's no problem.
superflight
December 15th, 2009, 10:29 AM
Hey.
Here is the solution to change texts in the dynamic textFields in SimpleButton instance. It is about existing instance on the stage - I was needing it for localizations purpose.
First of all - you CAN use dynamic textFields in the simplebutton states.
BUT - you CAN'T acces it by label (textfield instance name).
This happens because all frames (states) of simplebutton created in flash IDE being converted into appropriate type (e.g. Sprite) upon compilation. So even if you have "Same" textfield for all 3 visible button states - after compilation in fact there will be 3 differend objects containing 3 different textfields. So there is no sence to write any instance name for textfield.
Anyway, you can access that textfields through the child number when you know it.
The only issue here - is: all states of the buttons return DisplayObject, and DisplayObject class does not have any children, so you have to convert it into DisplayObjectContainer (at least) if you sure that there is any children (your dynamic textfield).
so. you need to do something like:
var doc:DisplayObjectContainer = my_button.upState as DisplayObjectContainer;
var tf:TextField = doc.getChildAt(1) as TextField // here you need to know where your textfield lying
tf.text = "new text" //voila - text is changeable
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.