AS3 Basics 1 – Buttons!

Reading around blogs and message boards, I’ve noticed quite a lot of people are still using Actionscript 2, especially on gaming sites. It took me quite a while to get up to speed with AS3, but I can’t recommend it enough. Having had to go back and work on some old AS2 projects recently has made me realise what a step forwards AS3 is.
So, with the aim of making the transition a little easier for anyone hoping to start using AS3, I’m going to post a few basic, frequently used code snippets. Today is Making Buttons in AS3. The following code requires a move clip on stage with the instance name of “myButton”.

//Use the hand cursor
myButton.buttonMode = true;

//Add a listener to the button, which will run the function "clickButton" when the button is clicked.
myButton.addEventListener(MouseEvent.CLICK, clickButton);

function clickButton(e:Event):void{
	trace("clicked the button!");

//Add a listener to the button, which will run the function "rollOverButton" when the button is rolled over.
myButton.addEventListener(MouseEvent.ROLL_OVER, rollOverButton);

function rollOverButton(e:Event):void{
	trace("rolled over the button!");

//Add a listener to the button, which will run the function "rollOutButton" when the button is rolled off.
myButton.addEventListener(MouseEvent.ROLL_OUT, rollOutButton);

function rollOutButton(e:Event):void{
	trace("rolled off the button!");

25 lines source code

In the last post, I put up my entry to one of the 25 lines of actionscript competitions. In case you missed it, here it is again.
fversion=”8.0.0″ movie=”flash-content/25lines/25lines.swf” targetclass=”flashmovie” publishmethod=”static” width=”400″ height=”400″]

Get Adobe Flash player

And here is the AS3 that makes it – for a stage size of 400 x 400, and a frame rate of about 50.

stage.quality = StageQuality.LOW;
var stageSin = 0.1;
var bmd:BitmapData = new BitmapData(400, 400, true, 0x000000);
var bm:Bitmap = new Bitmap(bmd);

for(var i=1;i<=360;i++){
     var circle:Shape = new Shape( ); ((0xFFFFFF*i)/360) , 1 ); 0 ,  0 , i/(i/2) );
     var holder:MovieClip = new(MovieClip);
     holder.x = holder. y = 200;
     holder.myNum= i;
     holder.addEventListener(Event.ENTER_FRAME, spinCircle);
function spinCircle(e:Event):void{
     e.currentTarget.alpha = Math.abs(1-(mouseX/200));
     e.currentTarget.scaleX = e.currentTarget.scaleY = ((Math.PI*(e.currentTarget.myNum/2))+(Math.sin(stageSin)*350))/15;

addEventListener(Event.ENTER_FRAME, drawAndSin);

function drawAndSin(e:Event):void{
     bmd.applyFilter(bmd, bmd.rect, new Point(0,0), new BlurFilter(2, 2, 1));