# Sprite moving using input coordinates

## Recommended Posts

I am working on a robot game and I am using phaser library.
My requirement is to pass the x y coordinates to robot(sprites) and
it should move according to that coordinates without any velocity or speed.
Every one is using speed to move the sprite. So, kindly give me some suggestions.

Now I am using following code for moving sprites:

``````// The following code is for the robot movement

// This is initial velocity
player.body.velocity.x = 0;
player.body.velocity.y = 0;

if (cursors.left.isDown)
{
player.body.velocity.x = -200;
}
else if (cursors.right.isDown)
{
player.body.velocity.x = 200;
}

if (cursors.up.isDown)
{
player.body.velocity.y = -200;
}
else if (cursors.down.isDown)
{
player.body.velocity.y = 200;
}``````

##### Share on other sites

You should check out Phaser.Physics.Arcade.moveToXY. Might be  exactly what you're looking for.

##### Share on other sites

Thank you @drhayes for you reply .I will see this and after that I will let you know if problem solved.

##### Share on other sites

Hey @drhayes  I have problem in implementation of the code, can you suggest me example code  using moveToXY???

This is the link where I posted my question and code here:

##### Share on other sites

You can use the code like this:

``game.physics.arcade.moveToXY(object,x,y,speed,maxTime);``

From the docs: Move the given display object towards the x/y coordinates at a steady velocity. If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of milliseconds.

So basically if you want the robot to go to x = 500, y = 600, and take 3 seconds to reach that, you do something like this:

``````//Speed set to 0 as it is overwritten by maxTime anyway

Keep in mind that moveToXY does not stop the movement of the object when it reaches its location. You can build in a timer that will set the x/y velocity of the player to 0 as soon as maxTime has been reached.

##### Share on other sites

Thanks @Taggrin for your answer. Actually my values of x and y are changing every time (coming through web Socket ). So, can I use variable x and y instead of numbers? and If it is possible to only update the position according value instead  of movingToXY that would be fine.What is preferable for me?

##### Share on other sites

Hey @Taggrin, I have tried moveToXY and that is working fine.But it moving with respect to pixel size not the tile size.and that should be stop at that point. Is it possiible?

suggest me any example ..

##### Share on other sites

On 9/2/2016 at 0:04 PM, Ashish said:

Hey @Taggrin, I have tried moveToXY and that is working fine.But it moving with respect to pixel size not the tile size.and that should be stop at that point. Is it possiible?

suggest me any example ..

Of course you can enter a variable instead of a number, just like any other function. That variable needs to be a number though. If you mean you also want to change the direction of the player (e.g. when the target moves) I think you can just override moveToXY with new values.

So far I have never used tiles before  to make a game so I'm not sure, maybe you can calculate the relative position to the tile based on the scale.

##### Share on other sites

Hey @Taggrin, I have tried moveToXY and that is working fine.But it moving with respect to pixel size not the tile size.and that should be stop at that point. Is it possiible?

suggest me any example ..

Actually now I am using the moveToXY and that is working fine .

But now the problem is I have set sprite starting position to (0,0) at the beginning  of sprite creation.

So, when I getting the values my sprite is moving properly .but after end of the value it is not stopping at the end tile.It is  again showing at the starting position.

How can I stop according to my x y values?.

##### Share on other sites

12 hours ago, Ashish said:

Actually now I am using the moveToXY and that is working fine .

But now the problem is I have set sprite starting position to (0,0) at the beginning  of sprite creation.

So, when I getting the values my sprite is moving properly .but after end of the value it is not stopping at the end tile.It is  again showing at the starting position.

How can I stop according to my x y values?.

moveToXY does not stop the object, it simply moves it towards the location.

If you use the maxTime argument you will know when your object will reach its destination and you can use a timer to stop the object at that exact same time, for example:

``````//Move towards target in 2 seconds

//Stop movement after 2 seconds (should be on target)
player.body.velocity.x = 0;
player.body.velocity.y = 0;
}, this);``````

Keep in mind that maxTime is not 100% accurate due to how browser timers work, so it could differ a few milliseconds.

##### Share on other sites

Ok.  I will surely try this and thank you for helping me. I appreciate this.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.