Flash tutorials
Home 3D (2) Actionscripting (65) Animation (100) Audio (4) Drawing (7) Full flash sites (4) Getting Started (15) Navigation (25) Special Effects (52) Text Effects (38)

Object follow the mouse on click

30.10.2006, 0:54    Total views: 35796

Learn how to create that any object, follow the mouse on click using the Action Script. You can use this action script animation for some presentation, or for some flash banner. Using this lesson, you will also learn how to convert any object into a Movie Clip Symbol, how to create instance name and much much more! Let's start!

Example:



Step 1

Create a new flash document and set Width and Height to 300px. Frame rate set to 45fps (Frames per Second).



Step 2


Double click on layer 1 to rename its name in background. For background color, set this color: #B8FECA.

Step 3

Insert a new layer and name it object. After that, create any object that will follow the mouse on click.

Step 4

Select the object (Ctrl+A), and after that press F8 on the keyboard (Convert to Symbol) to convert it into a Movie Clip.



Step 5

While your new made Movie Clip (object) is still selected, open the Properties Panel (Ctrl+F3) and under type object. See the picture below.



Step 6

Select again the object, open the Action Script Panel (F9) and paste this script:

onClipEvent (load) {
friction = "0.18";
targetx = Random(300)+20;
targety = Random(300)+20
}

onClipEvent (enterFrame) {
mouse_x = int(targetx-this._x);
mouse_y = int(targety-this._y);
if (mouse_x>0 && mouse_y>0) {
quad = Number(4);
}

if (mouse_x<0 && mouse_y>0) {
quad = Number(1);
}

if (mouse_x<0 && mouse_y<0) {
quad = Number(2);
}

if (mouse_x>0 && mouse_y<0) {
quad = Number(3);
}

abs_x = Math.abs(mouse_x);
abs_y = Math.abs(mouse_y);
tg = abs_y/abs_x;
_root.maths = Math.atan(tg)*Number(180)/Math.PI;
if (quad == 1) { angle = number(90) - number(_root.maths) }
if (quad == 2) { angle = number(90) + number(_root.maths) }
if (quad == 3) { angle = number(270) - number(_root.maths) }
if (quad == 4) { angle = number(270) + number(_root.maths) }
if (not _root.done) { setProperty (_this, _rotation, angle); _root.done = true}
speedx = difx*friction;
speedy = dify*friction;
setProperty (this, _y, _root.object._y+speedy);
setProperty (this, _x, _root.object._x+speedx);
difx = int(targetx)-this._x;
dify = int(targety)-this._y;
}

Step 7

Insert a new layer and name it circle. Take the Oval Tool (O), Fill Color must be switched off, under Stroke color set this color: #608E1A. In Properties Panel (Ctrl+F3), under Stroke height set 3 px, under Stroke style set the style like it is shown on the picture below and draw a circle.Look at the pictures below!





Step 8

Select the circle and press F8 key to convert it into a Movie Clip.



Step 9

While your new made Movie Clip (circle) is still selected, in Properties Panel (Ctrl+F3) under type circle. See the picture below.



Step 10

Double click on a new made Movie Clip (circle), or choose right click and Edit in Place.

Step 11

While your circle is still selected, press again F8 and convert it into a Movie Clip.



Step 12

Click on frame 10 and press F6. After that, open the Action Script Panel (F9) and paste this script:

gotoAndPlay(1);

Step 13

Go back on frame 1, open the Properties Panel (Ctrl+F3), under Tween choose Motion, under Rotate set CCW. Look at the picture below!





Step 14

Go back on the main scene (Scene 1), select the Circle, open the Action Script Panel (F9), and paste this script:

onClipEvent (load) {
startDrag (this, true);
_root.drag = this;
}

onClipEvent (mouseDown) {
_root.difx = this._x- _root.object._x;
_root.object.targetx = this._x;
_root.dify = this._y- _root.object._y;
_root.object.targety = this._y;
_root.done = False
}

Step 15

Insert a new layer and name it Action Script. Open the Action Script Panel (F9), and paste this script:

stop();

Test your Movie (Ctrl+Enter).

We're done!

Download source file (.fla)

Have questions about this tutorial?
Visit our friendly Community Forums!
Digg it! Add this tutorial to del.icio.us! Furl it! Add this tutorial to reddit! Spurl it! Add this tutorial to technorati!

Top tutorials

1. Advanced full flash site - Part 1
Total views: 221536

2. Water effect
Total views: 166041

3. Photo slide show
Total views: 153641

4. High-tech city animation
Total views: 151459

5. Special Picture Effect
Total views: 146246

Related links