Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help creating a button and adding text inside it

Explorer ,
Aug 13, 2013 Aug 13, 2013

Hi guys...

I am trying to create a button (or a MC, same thing) using only code, but it has to have what I need, inside...

var ins_btn:  ?????

var ins:String = "How to play / Como jugar";

ins_btn.x = 202,75;                  

ins_btn.y = 316;

ins_btn.width = 533,30;

ins_btn.height = 59,35;

ins_btn.text = ins;

addChild(ins_btn);

I have tried that way, making ins_btn a MC, but it did not work (it did not show up in my screen) and I am doing that, so, when someone clicks on it, it goes to frame two:

ins_btn.addEventListener(MouseEvent.CLICK, instructionF);

function instructionF(event:MouseEvent):void {

    ins_btn.removeEventListener(MouseEvent.CLICK, instructionF);

    gotoAndStop(2);

    }

So... What do I need to do to make this button or movieclip show up in the stage, with the words How to play / Como jugar inside it?

Thanks a lot!

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Aug 13, 2013 Aug 13, 2013

For a button to work it needs something to click on as a hit area. If you need a basic button without doing any design you can use either one of the premade buttons (window->common libraries->buttons) or just use the button component (window->components->user interface->button).

For instance, the latter, open the components panel (window->components), roll open User Interface, drag the Button component into your library.

In code you can use it as simply as this:

import fl.controls.Button;

import f

...
Translate
LEGEND ,
Aug 13, 2013 Aug 13, 2013

For a button to work it needs something to click on as a hit area. If you need a basic button without doing any design you can use either one of the premade buttons (window->common libraries->buttons) or just use the button component (window->components->user interface->button).

For instance, the latter, open the components panel (window->components), roll open User Interface, drag the Button component into your library.

In code you can use it as simply as this:

import fl.controls.Button;

import flash.events.MouseEvent;

var button:Button = new Button();

button.label = "How to play / Como jugar";

button.setSize(150,20);

addChild(button);

button.addEventListener(MouseEvent.CLICK, instructionF);

function instructionF(event:MouseEvent):void

{

          button.removeEventListener(MouseEvent.CLICK, instructionF);

    gotoAndStop(2);

}

If you want to draw your own button, use the graphics class of any display object and draw a background so your button has something to capture the click. You also need a TextField inside the button. For example a blue rounded rectangle Sprite button:

import flash.display.Sprite;

import flash.text.TextField;

import flash.text.TextFieldAutoSize;

import flash.text.TextFormat;

import flash.text.AntiAliasType;

var ins_btn:Sprite = new Sprite();

addChild(ins_btn);

// background

ins_btn.graphics.beginFill(0x6666CC,1);

ins_btn.graphics.drawRoundRect(10,10,150,20,10,10);

ins_btn.graphics.endFill();

// text

var tf:TextField = new TextField();

ins_btn.addChild(tf);

tf.width = 10;

tf.autoSize = TextFieldAutoSize.LEFT;

tf.antiAliasType = AntiAliasType.ADVANCED;

tf.selectable = false;

tf.text = "How to play / Como jugar";

tf.setTextFormat(new TextFormat("Arial",11,0xFFFFFF));

tf.x = 22;

tf.y = 10;

// hit

ins_btn.addEventListener(MouseEvent.CLICK, instructionF);

function instructionF(event:MouseEvent):void

{

    ins_btn.removeEventListener(MouseEvent.CLICK, instructionF);

    gotoAndStop(2);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013

One thing you will need to add to the movieclip is a textfield so that the text assignment you attempt has something to accomodate it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 13, 2013 Aug 13, 2013

var fmat2:TextFormat = new TextFormat();

fmat2.font = "Verdana";

fmat2.size = 35;

fmat2.color = "0xFFFF33";

fmat2.align = "center";

fmat2.bold = true;

var instructions:TextField = new TextField();

instructions.defaultTextFormat = fmat2;

instructions.text = "How to play / Como jugar";

Ok, I have this TextField (with its format)

how do I add it to the MovieClip to be shown in the stage? (if I add this instruction to the stage (addChild instructions) it works, but as a text and therefore I can´t click on it

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013

The TextField needs the .selectable property set to false. See my Sprite button example above.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 13, 2013 Aug 13, 2013

checking it now

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 13, 2013 Aug 13, 2013

Got it working Sinious

Thanks a lot both of you!!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013
LATEST

You're welcome and good luck!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 13, 2013 Aug 13, 2013

To add the textfield to the movieclip you can use...

ins_btn.addChild(instructions);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 13, 2013 Aug 13, 2013

var fmat2:TextFormat = new TextFormat();

fmat2.font = "Verdana";

fmat2.size = 35;

fmat2.color = "0xFFFF33";

fmat2.align = "center";

fmat2.bold = true;

var instructions:TextField = new TextField();

instructions.defaultTextFormat = fmat2;

instructions.text = "How to play / Como jugar";

var ins_btn:MovieClip = new MovieClip();

ins_btn.x = 202,75;                  

ins_btn.y = 316;

ins_btn.width = 533,30;

ins_btn.height = 59,35;

ins_btn.addChild(instructions);

addChild(ins_btn);

Still not shown on stage... do I have to set any other property to the MC to be shown?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines