Internet Cross Logo
Internet Cross your one stop web tutorial website
Your Ad Here

Moving a object to a specified point

by Anand
This tutorial will show you how to move a object to a specified point. This has a lot of uses in gaming and other things.



Let's start.
1.Create a movieclip object of your choice. Here I created a amoeba object I gave it a instance name of "amoeba".
2.Create a mouse cursor . I created a movieclip with a red square , I gave it a instance name of "point".
3.Now comes the actionscript put the below code in the first frame.

//x,y are x and y axia position
//xl,yl are last position of movieclip
//xi,yi are x and y incremental values

x=amoeba._x;y=amoeba._y;
function pos()
{
xl = amoeba._x;
yl = amoeba._y;
xi = xl - x;
yi = yl - y;
xi = xi/150
yi = yi/150
if(yi < 0){yi = -yi;}
if(xi < 0){xi = -xi;}
}
function animate()
{
pos();
if(amoeba._x >= x){amoeba._x -= xi;}
if(amoeba._y >= y){ amoeba._y -= yi;}
if(amoeba._x <= x){amoeba._x = xi;}
if(amoeba._y <= y){ amoeba._y = yi;}
}
setInterval(animate,10);
point.onMouseDown=function()
{
x = this._x;
y = this._y;
}
point.onEnterFrame = function()
{
startDrag("point",true);
}

4.That's it test the movie. Click anywhere on the screen and you will see the amoeba object moves to that point.

[Download File][Page 1]
Visit our forums to discuss / post your problems.