Sammdahamm Posted March 6, 2018 Share Posted March 6, 2018 Hi Guys, so Phaser 2 had a method, game.physics.arcade.moveToPointer() which moved a sprite with a given velocity towards the cursor. It seems like this has been replaced in Phaser 3 by a "moveTo()" method in ArcadePhysics, so I'm trying to reimplement it myself. I create a spriteGroup: this.projectiles = this.physics.add.group({collideWorldBounds: true}) and then later create & attempt to fire a projectile from within a "fireProjectile" method: this.firePlayerProjectile = function(projectileType){ let projectile = this.projectiles.getFirstDead(true, this.player.sprite.x-16, this.player.sprite.y-32, projectileType); this.scene.physics.moveTo(projectile, {x: this.scene.input.x + this.scene.cameras.main.scrollX, y: this.scene.input.y + this.scene.cameras.main.scrollY}, 750); } This does create the projectile sprite in the correct location, but rather than then travelling towards the target location, it just sorta of flashes and disappears after a second. Can anyone tell what I might be doing wrong? How do I get the projectile to move to a given location (and beyond) at a constant velocity? Thanks, Sam rex 1 Link to comment Share on other sites More sharing options...
oplayer Posted March 6, 2018 Share Posted March 6, 2018 Parameters Name Type Attributes Default Description gameObject Phaser.GameObjects.GameObject Any Game Object with an Arcade Physics body. x number The x coordinate to move towards. y number The y coordinate to move towards. speed number <optional> 60 The speed it will move, in pixels per second (default is 60 pixels/sec) maxTime number <optional> 0 Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms. So it either moves 750 px/sec or, more likely, bumps right into {NaN, 750}. this.scene.physics.moveTo(projectile, this.scene.input.x + this.scene.cameras.main.scrollX, this.scene.input.y + this.scene.cameras.main.scrollY, null, 750); Sammdahamm 1 Link to comment Share on other sites More sharing options...
Sammdahamm Posted March 6, 2018 Author Share Posted March 6, 2018 Ah you're totally right! I was wondering where the NaN was coming from I got thrown off a bit due to the moveToObject() method taking a {x: y:} object as the destination parameter, I didn't notice that moveTo() was a bit different haha Been scratching my head on this for while, thanks for the help dude Link to comment Share on other sites More sharing options...
adammarcwilliams Posted March 8, 2018 Share Posted March 8, 2018 I've just got this working myself but realised it's not what I need. How would you move an object so it tracked your mouse cursor 1 to 1 instead of moving towards it. I tried setting the objects position to the mouse position using this.object.body.x & .y which worked for moving it about on screen but it then ignored all physics collisions in its path. Link to comment Share on other sites More sharing options...
coder_for_life22 Posted June 23, 2018 Share Posted June 23, 2018 @adammarcwilliams did you ever figure it out? Link to comment Share on other sites More sharing options...
Recommended Posts