Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda