Search This Blog

1/1/10

Move an Object between any Two Points

In this tutorial we are going to move an object from one point(x1,y1)to another point(x2,y2) with action script. This is quite easy and involves no trigonometry. You have to analyze the coordinates and find out the ratio by which the x & y is increasing only.
As usual, draw a movieclip and attach the following script.It is made quite easy and will work for any two given points,only you have to change the condition accordingly to make it stop at it's final position,still I haven't found out any general formula that will work on any given coordinates.But I think it will help any beginner to make them think the actionscript way.Happy programming....



onClipEvent (load) {
// set up the speed
var speed:Number = 100;
// set up initial position
var x1:Number = 25;
var y1:Number = 25;
// set up final position
var x2:Number = 500;
var y2:Number = 350;
//calculate the ratio by which x,y going to change
var dx:Number = x2-x1;
var dy:Number = y2-y1;
var xratio:Number = dx/speed;
var yratio:Number = dy/speed;
this._x = x1;
this._y = y1;
}
onClipEvent (enterFrame) {
//make the ball move
this._x = this._x+xratio;
this._y = this._y+yratio;
// make it stop when it reaches the final position
if (this._x>=x2 && this._y>=y2) {
this._x = x2;
this._y = y2;
}
}

Understand the if condition properly and if u change the coordinates u have to change the condition accordingly.

No comments:

Post a Comment