Phaser 3 stop object
by Trobing
HTML
<script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser.js"></script>
JavaScript
var config = {
type: Phaser.AUTO,
width: 600,
height: 600,
physics: {
default: 'arcade',
arcade: {
debug: true
}
},
scene: {
preload: preload,
create: create,
update: update
}
}
var game = new Phaser.Game(config)
var player,
destination = null;
function preload() {
const circle = this.add.graphics()
circle.setVisible(false)
circle.fillStyle(0x999999)
circle.fillCircle(20, 20, 20)
circle.generateTexture('circle', 40, 40)
}
function create() {
player = this.physics.add.sprite(100, 100, 'circle')
player.setCircle(player.body.width/2)
this.input.on('pointerdown', _handlePointerDown, this)
}
function update() {
if (destination !== null && player.body.hitTest(destination.x, destination.y)) {
player.body.stop()
destination = null
}
}
function _handlePointerDown(pointer) {
this.physics.moveToObject(player, pointer, 500)
destination = {x: pointer.x, y: pointer.y}
}