A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Move to point?

  1. #1
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976

    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.

  2. #2
    Opu Nui
    Join Date
    Jan 2003
    Location
    I'd rather be in Hawaii
    Posts
    79
    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

  3. #3
    Senior Member artyom_ch's Avatar
    Join Date
    Jan 2003
    Location
    Russia, Murmansk
    Posts
    128
    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;
    }

    }
    Wollt ihr das Bett in Flammen sehen?!

  4. #4
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    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...
    jonmack
    flash racer blog - advanced arcade racer development blog

  5. #5
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    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.
    jonmack
    flash racer blog - advanced arcade racer development blog

  6. #6
    Senior Member artyom_ch's Avatar
    Join Date
    Jan 2003
    Location
    Russia, Murmansk
    Posts
    128
    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.
    Wollt ihr das Bett in Flammen sehen?!

  7. #7
    Senior Member artyom_ch's Avatar
    Join Date
    Jan 2003
    Location
    Russia, Murmansk
    Posts
    128
    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]]]
    Wollt ihr das Bett in Flammen sehen?!

  8. #8
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    Oh yeah - you're right about the Math.floor. Silly me. But when i tried it it never seemed to stop, so something was still not quite right.

    About the delete onEnterFrame; - yeah, i see what you meant now, you're idea is better for system resources. I thought you meant permanently delete it when it get's to the point - so it'll never move again. Good idea.

    And finally - Re: last post. Are you sure that my {}()b r[acket)]['s don't work? I'm normally good with them I'm sure my version works too. In fact i know they do, it works every time on my computer. Your extra brackets just makes it absolutely sure what happens first, that's all. Mine's still good, mine's still good!

    Right, so between us we got it all working and system resource friendly then??

    P.S. - more system resource (ever so slight) - my variable "hyp" doesn't need to be there really - i thought i might have needed it later, but i didn't in the end. Can get rid of that out of memory i guess.
    jonmack
    flash racer blog - advanced arcade racer development blog

  9. #9
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center