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);
}
}