|
-
Who needs pants?
Move to point?
How to i move a mc to a specific point from any ponit on the stage.
That i want to for the mc to move at the same speed to the destination ?
i know how to make it ease in but thats not what i want. What i want is to move at a steady speed.
FOR EASING: i used this
targetx = 0;
targety = 400;
_x += (targetx - _x)/speed;
_y += (targety - _y)/speed;
That eases into the position. What i want is to move at say a speed of 5.
-
I believe that I have a solution for this problem.
It sounds like what you're wanting to do is move an object along a unit vector of magnitude 5.
I don't know much flash code (hardly any really) so bear with me.
Xpos=Xpos-"speed"*(Xpos/(sqrt(("target x position"-Xpos)^2+("target y position"-Ypos)^2)))
Ypos=Ypos-"speed"*(Ypos/(sqrt(("target x position"-Xpos)^2+("target y position"-Ypos)^2)))
Disclaimer: Xpos or Ypos cannot=0 otherwise your unit vector will equal zero and your MC will go nowhere. Also your MC cannot actually reach it's destination otherwise you'll be diving by 0 and that's bad.
I hope this helps.
-Swim'nSasquatch
-
Senior Member
Calculate once when target is specified:
speed = 5;
distance_x = target._x - this._x; //find horizontal distance
distance_y = target._y - this._y; //find vertical distance
initial_distance = Math.sqrt(dx*dx+dy*dy);
step_x = distance_x/initial_distance;
step_y = distance_y/initial_distance;
While moving:
onEnterFrame = new Function()
{
var dx = target._x - this._x;
var dy = target._y - this._y;
var current_distance = Math.sqrt(dx*dx+dy*dy);
if(Math.floor(current_distance) > 0)
{
this._x += step_x*speed;
this._y += step_y*speed;
}
else
{
delete this.onEnterFrame;
}
}
-
I think the "initial_distance" variable in artyom_ch's post above should contain distance_x's instead of dx's. And with the y's as well, so...
Code:
(all on one line)
initial_distance = Math.sqrt
(distance_x*distance_x+distance_y*distance_y);
That makes it move towards the point in question at a constant speed.
However, it doesn't stop when it gets there. You are assuming that when it get's there the Math.floor(current_distance) inside the onEnterFrame if statement will be 0. But most likely it'll be some 'orrible floating point number.
Not quite figured out the best way to check if gone past point yet, but if i do before anyone else i'll let you guys know...
-
Duh! Simplest version i think would be this
Code:
onMouseDown = function() {
speed = 5;
targetx = _root._xmouse;
targety = _root._ymouse;
distance_x = targetx - this._x; //find horizontal distance
distance_y = targety - this._y; //find vertical distance
hyp = distance_x*distance_x+distance_y*distance_y;
initial_distance = Math.sqrt(hyp);
step_x = speed*distance_x/initial_distance;
step_y = speed*distance_y/initial_distance;
};
onEnterFrame = function()
{
this._x += step_x;
this._y += step_y;
if ((Math.abs(this._x - targetx) < step_x) || (Math.abs(this._y - targety) < step_y)) {
this._x = target_x; // just to make sure it ends up where we want :D
this._y = target_y;
// or, ya know, just stop. Otherwise it'll never move again!
delete this.onEnterFrame;
}
};
That should work. (I hope!)
[EDIT] Sorry, just a quick notE, i used the mouse (x,y) to test this, so that's why there's a onMouseDown. Change that to whatever is choosing the point [/EDIT]
Last edited by jonmack; 01-29-2003 at 08:42 PM.
-
Senior Member
to jonmack: quite right about initial_distance - its' spelling bug, in fact i ment those.
However, it doesn't stop when it gets there. You are assuming that when it get's there the Math.floor(current_distance) inside the onEnterFrame if statement will be 0. But most likely it'll be some 'orrible floating point number
It won't in fact since Math.floor() returns it integer.
this._x = target_x; this._y = target_y; // just to make sure it ends up where we want
Nice. Makes it pixel-perfect
// or, ya know, just stop. Otherwise it'll never move again!
I've used 'delete' just to prevent unnesessary usage of system resourses. May be used like follows:
//encapsulated function
function Move(target_x,target_y)
{
...
}
onMouseDown = function()
{
//we only assign, when reaches the destination - automatically cleans up
this.onEnterFrame = Move(_xmouse,_ymouse);
}
While your method will produce constant slow-down.
-
Senior Member
Bug in your version:
((Math.abs(this._x - targetx) < step_x) || (Math.abs(this._y - targety) < step_y)) won't work.
Should be:
((Math.abs(this._x - targetx)) < step_x) || (Math.abs(this._y - targety)) < step_y))

[[[You've lost brakets]]]
-
-
Who needs pants?
Thanks Heaps guys it works like a charm.I hope to have the current project finished in the next week or so so i can show you how much you helped .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|