Buttons in AS3 for beginners

Buttons in AS3 for beginners

Postby Fighterlegend » Sat Aug 22, 2009 11:42 am

First, you need to import the mouse event with this line of code:

Code: Select all
import flash.events.MouseEvent;


Then, you need to create a button on the field, or just use the draw function in Flash to create a button to the field..

(this code is for the drawing)
Code: Select all
import flash.display.*;// import the button drawing stuff

function drawButton(event:Event){//run the function
    var button1:MovieClip = new MovieClip();// create a new movieclip
    button1.graphics.lineStyle(1);// sets line style
    button1.graphics.beginFill(0xff0000);// fills the button with selected color
    button1.graphics.drawCircle(200,200,50);// draws a circle at 200x, 200y, and with 50 scale
    // there are other shapes (drawRectangle, drawTriangle) and can be used with the top code. :P
    addChild(button1);// adds a child so all of the above could happen
    button1.graphics.endFill();// ends fill
   button1.addEventListener(MouseEvent.CLICK, buttonActive);// creates event listener
   removeEventListener(Event.ENTER_FRAME, drawButton);// removes the Enter_Frame function
}

addEventListener(Event.ENTER_FRAME, drawButton);//Create the event



Now onto the button part..
Make an MC name button1 on the field, or use the above script.

Apply this to the frame:
Code: Select all
addEventListener(Event.ENTER_FRAME, drawButton);// Creates the mouseevent

function buttonActive(e:MouseEvent){// runs the mouse event
   //do stuff
}


Here's the full script:
Code: Select all
stop();
import flash.events.MouseEvent;
import flash.display.*;// import the button drawing stuff

function drawButton(event:Event){//run the function
   graphics.clear();
    var button1:MovieClip = new MovieClip();// create a new movieclip
    button1.graphics.lineStyle(1);// sets line style
    button1.graphics.beginFill(0xff0000);// fills the button with selected color
    button1.graphics.drawCircle(200,200,50);// draws a circle at 200x, 200y, and with 50 scale
    // there are other shapes (drawRectangle, drawTriangle) and can be used with the top code. :P
    addChild(button1);// adds a child so all of the above could happen
    button1.graphics.endFill();// ends fill
   button1.addEventListener(MouseEvent.CLICK, buttonActive);
   removeEventListener(Event.ENTER_FRAME, drawButton);
}

addEventListener(Event.ENTER_FRAME, drawButton);//Create the event

function buttonActive(e:MouseEvent){
   //do stuff
}


For more info on Drawing, go here.
For more info on Buttons, go here.
User avatar
Fighterlegend
The Leader!!!
 
Posts: 182
Joined: Tue Aug 11, 2009 10:21 pm
Location: Brooklyn, New York

Re: Buttons in AS3 for beginners

Postby Vegeta » Thu Mar 04, 2010 12:21 pm

that's good pretty :D
Vegeta
VIP
 
Posts: 4
Joined: Sat Dec 19, 2009 12:44 pm

Re: Buttons in AS3 for beginners

Postby Fighterlegend » Thu Mar 04, 2010 10:49 pm

Vegeta wrote:that's good pretty :D

Vegiman! I'm sorry I haven't talken to you recently. I don't know any IRC servers except Nixtrix's one.
All my data got erased from my IRC client. So I want to know where I can contact you.
User avatar
Fighterlegend
The Leader!!!
 
Posts: 182
Joined: Tue Aug 11, 2009 10:21 pm
Location: Brooklyn, New York


Return to ActionScript

Who is online

Users browsing this forum: No registered users and 1 guest

cron