Jump to content

Trying to implement "moveToPointer"


Sammdahamm
 Share

Recommended Posts

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

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

Ah you're totally right! I was wondering where the NaN was coming from :P 
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 :D

Been scratching my head on this for while, thanks for the help dude

Link to comment
Share on other sites

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

  • 3 months later...
 Share

  • Recently Browsing   0 members

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