Toxiclab.org

Controling image with mouse

12.5.2009, 12:21
Submited in: Flash | Total Views: 14999

This, step by step, detailed and thoroughly explained flash lesson will show you how to get full image control using a mouse cursor. you can use this animation for some presentation, or some flash header, banner. You also have to use action script code to make this lesson. 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 much more! Let's start!

Example:



Step 1

First, save the image below!





Step 2


Create a new flash document. Press Ctrl+J key on the keyboard (Document Properties) and set the width of your document to 250 pixels and the height to 147 pixels. Select any color as background color. Set your Flash movie's frame rate to 28 and click ok.

Step 3

Call the current layer image. 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 the image that you just saved in step 1 into a flash stage. While the image is still selected, go to the Align Panel (Ctrl+K) and do the following:

1. Make sure that the Align/Distribute to Stage button is turned on,
2. Click on the Align horizontal center button and
3. Click the Align vertical center button.



Step 5

While the image 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 "image_mc". See the picture below!



Step 7

Create a new layer above the layer image and name it action script code. After that, select the first frame of layer action script code and enter this code inside the action script panel (F9):

this.onMouseMove = function() {
 constrainedMove(image_mc, 3, 1);
};

function constrainedMove(target:MovieClip, speed:Number, dir:Number) {
 var mousePercent:Number = _xmouse/Stage.width;
 var mSpeed:Number;
 if (dir == 1) {
  mSpeed = 1-mousePercent;
 } else {
  mSpeed = mousePercent;
 }
 target.destX = Math.round(-((target._width-Stage.width)*mSpeed));
 target.onEnterFrame = function() {
  if (target._x == target.destX) {
   delete target.onEnterFrame;
  } else if (target._x>target.destX) {
   target._x -= Math.ceil((target._x-target.destX)*(speed/140));
  } else if (target._x<target.destX) {
   target._x += Math.ceil((target.destX-target._x)*(speed/140));
  }
 };
}

We're done!

Test your Movie (Ctrl+Enter) and enjoy!

Download example

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

Browse by category


Most popular tutorials