Follow object on mouse click
This Flash tutorial will teach you how to move an object when the mouse is clicked. So, an object will follow the mouse click. I will be using a simple circle as an example, but you can use whatever ever objects you wish. I have also used a custom cursor with this tutorial.
Follow object on mouse click
Step 1
Open a new Flash document.
Select the Oval tool (O) and create a circle shape on the stage. You can alternatively import an image or create whatever object on the stage at this point.
Step 2
Select your circle shape and convert it into symbol by pressing F8. Give your symbol an appropriate name, check movie clip and click ok.
Give your movie clip the instance name “ball” as shown in the diagram below:
Step 3
On the timeline insert a new layer called “Action” right click on 1st frame and select Actions from the drop down menu.
Add the following code:
moveBall = function () {
this.dx = (this.targx-this._x)*.1;
this.dy = (this.targy-this._y)*.1;
this._x += this.dx;
this._y += this.dy;
};
setTarget = function () {
this.targx = _root._xmouse;
this.targy = _root._ymouse;
};
ball.targx = 10;
ball.targy = 10;
ball.onEnterFrame = moveBall;
ball.onMouseDown = setTarget;
**This piece of code basically moves the ball when the mouse is clicked. There are two functions: moveBall and setTarget. The moveBall function moves the ball and the setTarget moves the ball to the position of the mouse click.
Step 4
Test your movie clip Ctrl + enter and click anywhere on the stage and the object should follow your mouse. To use a custom cursor check out this tutorial.
Your object should now follow your mouse when you click.



15 comments:
i red u r tutorial ,its realy helpful thank u..
Also can i ask u 4 a favour.. I want to lern actionscript, i jst knw d play, goto such simple functions, i chkd in net 4 tutorils but i cant understand most of'em..., can u help me to get started wit it
thank u
@Preeth
I don't understand your question. Can you rephase it?
hi, i mean i really want to study action scripting, but i cant find any tutorial on net that introduces these built in functions in flash, just shows how to do this n that... thats not realy wat i want... i want to understand these things...can u help me.
@Lost
What in particular do you want learn?
If you want to learn actionscript in general I recommend you going to a book shop and buying a book on actionscript. Then you will get a better understanding of how to use the functions.
How do you make the ball move at the same speed to any location?
@Andrew
You click on the location on the stage.
When I put the code it just says error 1220 can you help me
@Liquidfired
I used a actionscript 2.0 file. This might be your problem
Exactly what i needed. Thanks for sharing.
This was a great tutorial. Exactly what I was looking for. I have a kids game that uses key presses for movements, but some kids said it was too hard... so trying the mouse movement. Unfortunately, I cannot seem to make it so that the object will stop moving when it hits an obstacle.
Originally, I had speed associated, so when they hit the wall, speed went to 0. Now I don't have speed and cannot figure a simple way to make the object stop moving.
I have tried making the target = to the current x and y, but that does not seem to do the trick. Any help would be welcomed. Thanks!
@Natasha 0
Add the following code inside the move function. Replace the 'hit_mc' for the name of the object you want to hit.
if (this.hitTest(hit_mc)) {
this._x -= this.dx;
this._y -= this.dy;
}
Thanks that did the trick! You rock!!
i tried to do and it come out with an error
@Keeweet
What is the error message?
That is very nice...
Post a Comment