Toxiclab.org

Shake the photo using the mouse cursor

9.7.2009, 12:27
Submited in: Flash | Total Views: 11131

In this thoroughly explained, detailed flash lesson, I will show you how to create cool shake animation using the AS3 and mouse cursor. You can use this animation for some flash banner. Using this lesson, you will also learn how to import any image into a flash stage, how to convert it into a Movie Clip Symbol, how to create instance name and much more! Let's start!

Example:

Move your mouse cursor over the photo to see the effect!




Step 1

First, find any photo which you like to use for this lesson.



Step 2


Create a new flash document. Press Ctrl+J key on the keyboard (Document Properties) and set the dimensions of your document as whatever you like. Select any color as background color. Set your Flash movie's frame rate to 38 and click ok.

Step 3

Call the current layer photo. Double-click on its default name (Layer 1) to change it. Press Enter once you have typed in the new name!

Step 4

Choose now File > Import > Import to stage (Ctrl+R) and import any photo into a flash stage.

Step 5

While the photo is still selected, hit F8 key (Convert to Symbol) to convert it into a Movie Clip Symbol.



Step 6

While the new made Movie Clip is still selected, go to the Properties Panel below the stage. On the left side, You will find the Instance name input field there. Call this Movie Clip photo_mc. See the picture below!



Create a new layer above the layer photo and name it action script. After that, select the first frame of layer action script and go to the Action Script panel (F9). Then, enter this code inside the actions panel:

var coordX:Number = photo_mc.x; 
var coordY:Number = photo_mc.y;
var timer:Timer = new Timer(12);

photo_mc.buttonMode = true;

photo_mc.addEventListener(MouseEvent.ROLL_OVER,startShake);
photo_mc.addEventListener(MouseEvent.ROLL_OUT,stopShake);
timer.addEventListener(TimerEvent.TIMER, shakeImage);
 
function startShake(e:MouseEvent):void{
 timer.start ()
}

function stopShake(e:MouseEvent):void{
 timer.stop();
 photo_mc.x = coordX;
 photo_mc.y = coordY;
 photo_mc.rotation = 0;
}

function shakeImage(event:Event):void { 
 photo_mc.x = coordX+ getMinusOrPlus()*(Math.random()*7); 
   photo_mc.y = coordY+ getMinusOrPlus()*(Math.random()*4); 
   photo_mc.rotation = getMinusOrPlus()* Math.random()*6; 

function getMinusOrPlus():int{
 var rand : Number = Math.random()*3;
 if (rand<1) return -1
 else return 1;
}

That's it!

Test your movie (Ctrl+Enter).

Download example

Are you a website owner?
Click here to register and submit your tutorials!

Browse by category


Most popular tutorials