Tuesday, April 13, 2010

Flash Action Script Tutotial

Object Follow Mouse


Learn how to make an object follow the mouse and speed up or slow down depending on how far away the object is.  Create this effect in Flash MX with the help of Action Script.  So learn basic scripting in flash go ahead or Read Tutorial



















1. Open a new Flash document and make 2 layers. Name them actions and ball. (Top pic.)

On the ball layer draw a circle with no line color (In the example above I used a circle that was 26 x 26.) Select the ball, hit F8 to convert it to a movie clip. Go into editing mode for the movie clip, select the ball and set the x and y coordinates to -13 in the properties bar.(Middle pic)

This will position the ball in the center of the movie clip. Then go back to the main timeline, select the ball Movie Clip and give it an instance name of ball_mc (Bottom pic)

2. Now I going to add a few lines of action script and it will be done.

Add this code on the main timeline on the actions layer

ball_mc.onEnterFrame = function() {

var xMouse = _root._xmouse;

var yMouse = _root._ymouse;

if(Math.abs(xMouse - this._x) < 1) {

this._x = xMouse;

this._y = yMouse;

} else {

this._x -= (this._x-xMouse) / 6;

this._y -= (this._y-yMouse) / 6;

}

}



We are doing a couple of things in our onEnterFrame action for ball_mc. First we find the coordinates of the mouse in the movie. Next we check to see if the ball is within one pixel of the mouse. If it is we set the ball's position to the mouse position otherwise we move the ball towards the mouse. To move it closer we are taking the distance from the ball to the mouse and dividing by 6 so that it will move slower as it gets closer to the mouse.

3. There is many ways you can edit this code to have more functionality, so have some fun with it. Now go play.
                                                                                                                                                   -Satabdi.

2 comments:

  1. I play with this tutorial in off time.

    ReplyDelete
  2. nice as a basic scripting or starting

    ReplyDelete