Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Phaser 3 - Move gameObject to mouse click ("click to move" movement") using tween

So, I'm making a game with Phaser 3 that will need a "click to move" movement, but I can't use physics so I want to change it and use a tween to make the player move to the clicked position ("click to move"). This was the code that I was using that used physics:

var target = new Phaser.Math.Vector2();

preload(){
    this.load.image("image", "assets/image.png");
}

create(){
    player = this.physics.add.sprite(400, 350, "image");

    this.input.on('pointerup', function (pointer) {

        target.x = pointer.x;
        target.y = pointer.y;
        
        // Move at 200 px/s:
        this.physics.moveToObject(player, target, 200);

    }, this);
}

update(){
    var distance = Phaser.Math.Distance.Between(player.x, player.y, target.x, target.y);

    if (player.body.speed > 0)
    {
        //  4 is our distance tolerance. The faster it moves, the more tolerance is required.
        if (distance < 4)
        {
            player.body.reset(target.x, target.y);
            player.anims.stop();
        }
    }
}

I tried doing it (using tween), and it works but not as I expected, the results are a lot different from the first code that used physics, it moves much faster and the object (player) keeps teletransporting to the tween's original x and y. Here is what I tried:

var tween;
var target = new Phaser.Math.Vector2();

preload(){
    this.load.image("image", "assets/image.png");
}

create(){
    player = this.add.sprite(400, 300, "image");

    tween = this.tweens.add({
        targets: player,
        x: 400,
        y: 300,
        ease: 'Power1',
        duration: -1,
        paused: true
    });

    this.input.on('pointerdown', function (pointer) {

        target.x = pointer.x
        target.y = pointer.y

        tween.play();
    });
}

update(){
    if (tween.isPlaying())
    {
        tween.updateTo('x', target.x, true);
        tween.updateTo('y', target.y, true);
    }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!