var setup = {
type: Phaser.AUTO,
width: 536,
height: 183,
scene: {
customize,
renew
},
physics:{
default:'arcade',
arcade:{
gravity:{y: 260},
}},
banner: false
};
let playerObj;
let initialPointer;
let pathItemsGroup;
let lastPoint;
function customize () {
this.add.text(10, 10, 'Shift mouse to target\nPause to view complete path.', {color:'#ffffff'})
let ground = this.add.rectangle(0 , 183, 535, 20, 0x00ff00).setOrigin(0, .5);
pathItemsGroup = this.add.group();
playerObj = this.add.rectangle(30 , 158, 30, 30, 0x1AA840);
playerObj.setDepth(2)
initialPointer = this.add.arc(-10, 0, 5, 0, 2 * Math.PI, true, 0xffffff);
this.physics.add.existing(initialPointer);
this.input.on('pointermove', function (point) {
activatePath(this, playerObj, point)
},this);
}
function renew(time, delta){
let {x, y} = initialPointer
if(!lastPoint || Phaser.Math.Distance.Between(x, y, lastPoint.x, lastPoint.y ) > 20){
let trail = this.add.arc(x, y, 3, 0, 2 * Math.PI, true, 0xcdcdcd);
pathItemsGroup.add(trail);
lastPoint = {x, y};
}
}
function activatePath(scene, playerObj, point){
let pace = 300;
let angle = Phaser.Math.Angle.BetweenPoints(playerObj, point);
pathItemsGroup.clear(true);
initialPointer.setPosition( playerObj.x, playerObj.y );
scene.physics.velocityFromRotation(angle, pace, initialPointer.body.velocity);
}
new Phaser.Game(setup);
* {padding:0;margin:0;}
<script src="//cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea9a828b998f98aad9c4dfdfc4d8">[email protected]</a>/dist/phaser.js"></script>