How to make a display object move to a designated position?

Hi,

I have display objects that I want to move to a specific positions, but I know only to move physic objects by applying forces to them.

I googled alot before asking this but the only thing relevant to this was transition.to() function, does this do what I want to do or there is a better method of doing this?

Thanks.

transition.to() would be a perfectly acceptable way to move an object, assuming you don’t have a physics object attached to it.

For instance if you want to move an object named “thePlayer” from: 

x, y = 0, 0

to 

x, y = 100, 100

over 2 seconds, you could do

transition.to(thePlayer, {time = 2000, x = 100, y = 100})

This would do exactly that, transition thePlayer to 100, 100 over 2 second.

The problem comes when you will want something to move constantly at a consistent speed, across varying distances. If the transition period is always the same, but the distance increases/decreases the speed will change.

You could also use an enterFrame function to constantly move the object to a “target” position at a speed based on the current distance to the target:

thePlayer.x = thePlayer.x + (0.3 \* (targetX - thePlayer.x)) thePlayer.y = thePlayer.y + (0.3 \* (targetY - thePlayer.y))

This will make the object move faster when it is further away, and slower when it is close to the target.

If you ARE using physics bodies, and want to move to a specified location, then use:

thePlayer:setTarget( targetX, targetY )

+1 Alan.  

Create a simple 2D vector class.   To travel from point A to point B, you need to move your object along the vector B - A, or target - location.

Calculate that vector, normalize it (make it so the length is 1 unit), and scale it to the speed you want (multiply x and y by a factor).   This is your object’s velocity.   Add it to your location until you reach your target.

transition.to() would be a perfectly acceptable way to move an object, assuming you don’t have a physics object attached to it.

For instance if you want to move an object named “thePlayer” from: 

x, y = 0, 0

to 

x, y = 100, 100

over 2 seconds, you could do

transition.to(thePlayer, {time = 2000, x = 100, y = 100})

This would do exactly that, transition thePlayer to 100, 100 over 2 second.

The problem comes when you will want something to move constantly at a consistent speed, across varying distances. If the transition period is always the same, but the distance increases/decreases the speed will change.

You could also use an enterFrame function to constantly move the object to a “target” position at a speed based on the current distance to the target:

thePlayer.x = thePlayer.x + (0.3 \* (targetX - thePlayer.x)) thePlayer.y = thePlayer.y + (0.3 \* (targetY - thePlayer.y))

This will make the object move faster when it is further away, and slower when it is close to the target.

If you ARE using physics bodies, and want to move to a specified location, then use:

thePlayer:setTarget( targetX, targetY )

+1 Alan.  

Create a simple 2D vector class.   To travel from point A to point B, you need to move your object along the vector B - A, or target - location.

Calculate that vector, normalize it (make it so the length is 1 unit), and scale it to the speed you want (multiply x and y by a factor).   This is your object’s velocity.   Add it to your location until you reach your target.