Skip to main content
June 7, 2012
Answered

How do I create a button in AS3?

  • June 7, 2012
  • 1 reply
  • 441 views

Hello Everyone

How do I create a button in action script 3 and handle the click event?

Thanks

This topic has been closed for replies.
Correct answer The_Preacher1

Maybe this will help you get started.

import flash.display.Sprite;

import flash.events.Event;

import flash.display.Graphics;

var button:Sprite = new Sprite();  //Sprite to act as the button graphic.

//Set the color of the button graphic

button.graphics.beginFill(0xFFCC00);

//Set the X,Y, Width, and Height of the button graphic

button.graphics.drawRect(10, 10, 200, 60);

button.graphics.endFill(); //Apply the fill

this.addChild(button);  //Add Button Sprite to stage

button.useHandCursor = true;

button.buttonMode = true;

button.mouseChildren = false;

button.addEventListener(MouseEvent.CLICK, buttonClickHandler);

function buttonClickHandler(event:MouseEvent):void

     {

           trace("Button clicked!");

     }

1 reply

The_Preacher1
The_Preacher1Correct answer
Inspiring
June 8, 2012

Maybe this will help you get started.

import flash.display.Sprite;

import flash.events.Event;

import flash.display.Graphics;

var button:Sprite = new Sprite();  //Sprite to act as the button graphic.

//Set the color of the button graphic

button.graphics.beginFill(0xFFCC00);

//Set the X,Y, Width, and Height of the button graphic

button.graphics.drawRect(10, 10, 200, 60);

button.graphics.endFill(); //Apply the fill

this.addChild(button);  //Add Button Sprite to stage

button.useHandCursor = true;

button.buttonMode = true;

button.mouseChildren = false;

button.addEventListener(MouseEvent.CLICK, buttonClickHandler);

function buttonClickHandler(event:MouseEvent):void

     {

           trace("Button clicked!");

     }