[solved] AS3: How to make a simple button?

Subscribe to [solved] AS3: How to make a simple button? 11 posts

Sign in to reply


 
avatar for BrainStormer BrainStormer 372 posts
Flag Post

Hi.

Unfortunately I cannot find any useful tutorial on how to make a simple text button using only code.

I have made a subclass of SimpleButton:

package {
import flash.display.SimpleButton;
public class Tbtn extends SimpleButton {
public function Tbtn() {

}
}
}

Now I want to create a new TextField that is the button itself. The text is “PushMe”. The button should be clickable ofcourse (the hand should be displayed on mouse over).
I thought I make it as usual:
var textField:TextField = new TextField();
textField.text = “PushMe”;
addChild(textField);

But the SimpleButton class doesn’t seem to have such a method (addChild) at all. And I guess there’s more to be done (upState? overState maybe?).

Could you please post a simple example with a text button? I think I will be able to figure out the rest.

Thanx!

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

I’ve asked the same question here: http://www.kongregate.com/forums/4-programming/topics/64505-adding-things-dynamically. As you can see the text part was answered, and if you ever want to do music its there too, but as for the buttons i would like to know as well.

 
avatar for BrainStormer BrainStormer 372 posts
Flag Post

Actually this is the method:

SimpleButton(upState:DisplayObject = null, overState:DisplayObject = null, downState:DisplayObject = null, hitTestState:DisplayObject = null)

Creates a new SimpleButton instance.

I’ll try something right now, got a fresh idea…

 
avatar for BrainStormer BrainStormer 372 posts
Flag Post

Hehe, I made it :)

var mc:MovieClip = new MovieClip();
var text:TextField = new TextField();
text.text = “PushMe”;
text.width = 10;
text.height = 20;
var menuItem:SimpleButton = new SimpleButton();
menuItem.upState = text;
menuItem.enabled = true;
menuItem.useHandCursor = true;
menuItem.hitTestState = text;
addChild(menuItem);

There’s something not right with the hitTestState, it does it incorrectly, but that is another story :)

I consider this thread solved.

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

You’re getting something from that?

 
avatar for BrainStormer BrainStormer 372 posts
Flag Post

This code makes a button that is displayed as a text “PushMe”. It is clickable. Consider adding overState for it not to disappear on mouse over. Also TextFieldAutoSize helps.

public function MainMenu() { // this is the constructor
var mc:MovieClip = new MovieClip();
var text:TextField = new TextField();
text.text = “PushMe”;
text.autoSize = TextFieldAutoSize.LEFT;
var menuItem:SimpleButton = new SimpleButton();
menuItem.upState = text;
menuItem.overState = text;
menuItem.hitTestState = text;
addChild(menuItem);
}

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

I must be really unobservant with something in my code because i’m not getting anything:


public function Main():void
{
sBackground = new Background(Bitmap(new BackgroundClass()));
stage.addChild(sBackground);
stage.addChild(minimap);
minimap.x = 390;
minimap.y = 10;
var text:TextField = new TextField();
text.text = “PushMe”;
text.width = 10;
text.height = 20;
text.autoSize = TextFieldAutoSize.LEFT;
var menuItem:SimpleButton = new SimpleButton();
menuItem.upState = text;
menuItem.overState = text;
menuItem.enabled = true;
menuItem.hitTestState = text;
addChild(menuItem);
menuItem.x = 390;
menuItem.y = 10;
stage.addEventListener(Event.ENTER_FRAME, backgroundMove);
enemyShipTimer = new Timer(1000);
enemyShipTimer.addEventListener(“timer”, sendEnemy);
enemyShipTimer.start();
friendlyShipTimer = new Timer(5000);
friendlyShipTimer.addEventListener(“timer”, sendFriendly);
friendlyShipTimer.start();
}

and yes 390, 10 is on the screen.

 
avatar for BrainStormer BrainStormer 372 posts
Flag Post

Try to delete

text.width = 10;
text.height = 20;
and try adding that button to the stage instead of Main:
stage.addChild(menuItem);

instead of:
addChild(menuItem)

Also check that the background you place the button on is not the same color as button text (black). Aaand make sure you are using correct quotes in:
text.text = “PushMe”;
since this forum’s engine automatically converts them into incorrect ones. Use " " instead of “ ”.

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

Ok so it was because I was adding it to the main instead of the stage, thanks.

 
avatar for BrainStormer BrainStormer 372 posts
Flag Post

You are welcome.

 
avatar for ldstrumpet ldstrumpet 141 posts
Flag Post

Hey, I was just wondering if there was a way to give these buttons a background fill easily. Also, would I have to give it a class and things to make it do something when you click on it or is there an easier way. Any ideas?

Sign in to reply