Thursday, 30 July 2009

The Mousewheel in Actionscript 3

The mouse wheel event in AS2 is now slightly different in AS3. The two versions are almost exactly the same there are only a few differences. Below is a comparison of the two versions.

AS2

var mouseListener:Object = new Object();
mouseListener.onMouseWheel = function(delta) {
trace("The delta value is: " +delta);
}

Mouse.addListener(mouseListener);


AS3

import flash.events.MouseEvent;

public function mouseWheel(event:MouseEvent){
trace("The delta value is: " + event.delta);
}

addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheel);


The ‘delta’ properties in both of the above versions refer to the amount of up and down mouse movements. A positive delta means the mouse wheel has moved up, and a negative delta means the mouse wheel has moved down. I received a delta of + and - 3 on my mouse, but your values may be different depending on your mouse configuration.

Below is a simple example of using the mouse wheel. The rectangle shape will move upwards when the mouse wheel is up, and down when the mouse wheel is down.


The Mousewheel in Actionscript 3

Step 1

Open a Flash AS3 file.
Select the rectangle tool and create a basic rectangle shape like below:




Step 2

Convert your rectangle shape into a movie clip by pressing F8. Then give it the instance name: rec_mc.




Step 3

On the timeline create a new layer called “Actions”. Then select the first frame and hit F9 to open up the actions panel, and enter the following code:



Code:

1. Imports the mouse event.
2. This is a function which creates the mousewheel event. The rectangle shape is then checked to ensure it does not go over the screen. The event.delta determines what direction the mouse wheel has moved. If the delta is less than 0 the rectangle will go downwards, and if the delta is greater than 0 the rectangle will go upwards.
3. This add the event listener with the mouse wheel event.


Step 4

Test your mouse wheel example Ctrl + Enter. Now move your mouse wheel up and down and you should see the shape move as well.



You should now be able to use the mouse wheel in Actionscript 3.0.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP