Jump to content

Sprite moving using input coordinates


Ashish
 Share

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;
    }

 

Link to comment
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
game.physics.arcade.moveToXY(player,500,600,0,3000);

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.

Link to comment
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.

Link to comment
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?. 

Link to comment
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
game.physics.arcade.moveToXY(player,320,240,0,2000);

//Stop movement after 2 seconds (should be on target)
game.time.events.add(2000, function () {
   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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...