PDA

View Full Version : Make movieclip move along a line path


kilauea
06-14-2008, 12:00 PM
Is it possible to use a line that is drawn in flash as a guide to move a movieclip along? If so how could I do this? This line will be created by actionscript based on where the user clicks on the screen.

GMaker0507
06-14-2008, 03:01 PM
Well you could create an array of waypoints to move to, then always move to the first waypoint.

Once you are close enough to or exactly on that waypoint, you could remove that waypoint from the array.

Since it was the first item in the array, when you remove it, the next waypoint in the array becomes the first waypoint, and therefore, you start moving the that waypoint, and so on...

Here is an ActionScript 2 Version:


var Waypoints:Array = new Array ();
var moveSpeed:Number= 3

function AddWaypoint (xPos:Number, yPos:Number) {
var newWaypoint:Object = {_x:xPos, _y:yPos};
Waypoints.push (newWaypoint);
}

function MoveToWaypoint () {
if (Waypoints.length > 0) {
var xDiff = this._x - Waypoints[0]._x;
var yDiff = this._y - Waypoints[0]._y;
var distance = Math.sqrt (xDiff * xDiff + yDiff * yDiff);
if (distance <= 1) {
this._x = Waypoints[0]._x;
this._y = Waypoints[0]._y;
Waypoints.splice (0,1);
}else
{
this._x+=(xDiff/distance)*moveSpeed
this._y+=(yDiff/distance)*moveSpeed
}
}
}


And here is an ActionScript 3 Version:

var Waypoints:Array = new Array ();
var moveSpeed:Number= 3;

function AddWaypoint (xPos:Number,yPos:Number) {
Waypoints.push (new Point(xPos,yPos));
}

function MoveToWaypoint () {
if (Waypoints.length > 0) {
var difference:Point=Waypoints[0].subtract(new Point(this.x,this.y));
var distance = difference.length;
if (distance <= 1) {
this.x = Waypoints[0].x;
this.y = Waypoints[0].y;
Waypoints.splice (0,1);
} else {
difference.normalize (1);
this.x+=difference.x*moveSpeed;
this.y+=difference.y*moveSpeed;
}
}
}

So when ever you click the mouse to draw the line, just add new waypoints to the array. Or if you want it RTS style you could add new points to the end of the array when the shift button for example is held down, and if it isnt, clear the waypoint array and then add the new waypoint

kilauea
06-17-2008, 01:49 AM
Thank you for your help.

This is an interesting concept but for some reason I can't quite wrap my head fully around how this is supposed to be working. The code that you have written, is it designed to move a movieclip directly along a path of preset waypoints? It just picks the closest next waypoint then so on?

Or the mouse click generates the waypoint and since theres only one waypoint it moves in a straight line to that point? If this is the case how can I avoid the clip from running into objects on the screen I don't want it to. If I could define a set of waypoints beforehand I could just not put any waypoints in areas that I don't want the movieclip to go.

I guess, can you explain it a bit more? Thanks again for the help.

rrh
06-17-2008, 06:11 AM
It looks like it follows a series of points. No, it doesn't look like there's any pathfinding built into it. That's a little trickier. Maybe look up the A Star algorithm. But the details of how to do pathfinding will depend upon what kind of location you're trying to navigate.

GMaker0507
06-17-2008, 06:30 AM
it moves to the waypoints in chronological order.

you said you wantedto make a movie clip move along a path, dodging objects is a different story.

im not really familiar with pathfinding,as i've never tried to tackle it. However you probably could extend what i wrote to enable pathfinding