AS3: Mouse Wheel
In this tutorial we will see how to use the mouse wheel in our movies. Here we will create a new movie clip that you will be able to move a symbol up and down using your mouse wheel.
Note: You must click inside the Flash Movie before scrolling your mouse wheel.
Step 1: Create a new Flash movie with the ActionScript version set to 3.0. The background color, frame rate and the stage size do not matter for this tutorial.

Step 2: Create one new layer, so you will have two layers. Then name the one at the bottom Symbol and the one on top Actions.

Step 3: Now, select the Symbol layer and draw a rectangle using Rectangle Tool(R). Set the Stroke and Fill color to any color that you want.

Step 4: Use the Select Tool (V) and select the rectangle. Now Press F8 on your keyword to convert it to a symbol. Set the name of the symbol to Rectangle and the symbol type to Movie Clip.

Step 5: Now, we need to set an Instance Name for our symbol so we can easily refer to it using ActionScript. Use the Select Tool (V) and select the symbol, access the Properties Inspector and set MyRec as the instance name of your symbol.

Step 6: Click once on the name of the Actions layer and then right-click the only frame on it and select Actions to open Actions panel.

Step 7: Copy and paste this code to your Actions panel.
import flash.events.MouseEvent;
function MouseWheel(event:MouseEvent):void {
MyRec.y+=event.delta;
}
stage.addEventListener(MouseEvent.MOUSE_WHEEL, MouseWheel);
Note: The event.delta determines what direction the mouse wheel has moved. Practically, you only have to watch for positive and negative values of delta. If delta is positive, wheel was scrolled up. Otherwise, it was scrolled down. Usually scrolling up means page is scrolled down!
Here, 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.
That’s it, We are done. You can now test your movie (Ctrl+Enter) and move the symbol up and down using your mouse wheel!
Downloads:
Flash CS3: Mouse Wheel :: AS3
Flash CS4: Mouse Wheel :: AS3









Follow AllTutz on Twitter