Ashish Posted August 31, 2016 Share Posted August 31, 2016 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 More sharing options...
drhayes Posted August 31, 2016 Share Posted August 31, 2016 You should check out Phaser.Physics.Arcade.moveToXY. Might be exactly what you're looking for. Ashish 1 Link to comment Share on other sites More sharing options...
Ashish Posted August 31, 2016 Author Share Posted August 31, 2016 Thank you @drhayes for you reply .I will see this and after that I will let you know if problem solved. Link to comment Share on other sites More sharing options...
Ashish Posted September 1, 2016 Author Share Posted September 1, 2016 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: http://gamedev.stackexchange.com/questions/129287/how-can-i-pass-the-xy-coordinates-coming-thought-websocket-to-movetoxy-to-move-t Link to comment Share on other sites More sharing options...
Taggrin Posted September 2, 2016 Share Posted September 2, 2016 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 More sharing options...
Ashish Posted September 2, 2016 Author Share Posted September 2, 2016 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? Link to comment Share on other sites More sharing options...
Ashish Posted September 2, 2016 Author Share Posted September 2, 2016 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 .. Link to comment Share on other sites More sharing options...
Taggrin Posted September 5, 2016 Share Posted September 5, 2016 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. Ashish 1 Link to comment Share on other sites More sharing options...
Ashish Posted September 5, 2016 Author Share Posted September 5, 2016 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 More sharing options...
Taggrin Posted September 6, 2016 Share Posted September 6, 2016 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. Ashish 1 Link to comment Share on other sites More sharing options...
Ashish Posted September 6, 2016 Author Share Posted September 6, 2016 Ok. I will surely try this and thank you for helping me. I appreciate this. Link to comment Share on other sites More sharing options...
Recommended Posts