AS3 Basics 2 – Mouse Events!

Number two in my basic Actionscript 3 functionality is MOUSE EVENTS! The following code will show you how to access the mouse’s X and Y position, scroll wheel action and direction and clicking and double clicking.

//Mouse events!
//To use the double click event, you must first enable it for each object you want to use it on. Here we use - 
stage.doubleClickEnabled = true;
//But usually, you would replase "stage", with your movieClip name.

//Here we add event listeners for the mouse events - click, double click, mouse wheel and mouse move.

stage.addEventListener(MouseEvent.CLICK, stageClick);

stage.addEventListener(MouseEvent.DOUBLE_CLICK, dblClick);

stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMouse);

//And here we add the functions for each of the listeners above.

function stageClick(e:MouseEvent):void{
	trace("clicked the stage!");
}

function dblClick(e:MouseEvent):void{
	trace("double clicked the stage!");
}

function mouseWheel(e:MouseEvent):void{
	trace("Moved the mouse wheel!");
	//Delta is used here to show how much the mouse wheel has moved, and in which direction.
	trace("Moved the mouse wheel "+e.delta +" spaces");
}

function moveMouse(e:MouseEvent):void{
	trace("Moved the mouse to "+ mouseX+","+mouseY);
}

Hope that’s helpful.

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.
[kml_flashembed
fversion=”8.0.0″ movie=”flash-content/25lines/25lines.swf” targetclass=”flashmovie” publishmethod=”static” width=”400″ height=”400″]

Get Adobe Flash player

[/kml_flashembed]
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);
addChild(bm);

for(var i=1;i<=360;i++){
     var circle:Shape = new Shape( );
     circle.graphics.beginFill( ((0xFFFFFF*i)/360) , 1 );
     circle.graphics.drawCircle( 0 ,  0 , i/(i/2) );
     var holder:MovieClip = new(MovieClip);
     holder.x = holder. y = 200;
     addChild(holder);
     holder.addChild(circle)
     circle.y=-10;
     holder.myNum= i;
     holder.addEventListener(Event.ENTER_FRAME, spinCircle);
}
function spinCircle(e:Event):void{
     e.currentTarget.rotation+=((mouseY-200)/100)*(Math.PI*(e.currentTarget.myNum/2))/150;
     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.draw(this);
     bmd.applyFilter(bmd, bmd.rect, new Point(0,0), new BlurFilter(2, 2, 1));
     stageSin+=0.02;
}

In just 25 lines of Actionscript 3…

Right, I’m new to this, so go easy with me.
The idea of this blog, is to share some of the code I write and ideas I have. So much of my learning process with Flash has been through blogs and messageboards, I thought I’d best give something back.
I’m still getting to grip with WordPress, so I’m not sure how well this work, but hopefully you should see a flash movie here –
[kml_flashembed
fversion=”8.0.0″ movie=”flash-content/25lines/25lines.swf” targetclass=”flashmovie” publishmethod=”static” width=”400″ height=”400″]

Get Adobe Flash player

[/kml_flashembed]
Cool! You can affect the transparency of the circles and their spin direction by moving your mouse around.
Well, that was my entry into the awesome 25 lines competition, a great idea run by Keith Peters. The idea was to create something cool using just 25 lines of Actionscript 3, and that’s what I made. Needless to say, the Flash community submitted some staggering pieces, but I’m still quite proud of my entry as it was one of my first AS3 projects. In my next post, I’ll post the source code.