I’m trying to find the velocity of a path follower so I can properly lead a shot at it from a static tower. When I log the body.velocity of the closest path follower I get initialize {x:0, y:0}. Do path followers have velocity? I’m not sure where I’m going wrong here. Also does anyone have any suggestions on methods or approaches to properly leading a shot at a moving target in phaser? Thanks in advance!
class Game {
preload() {
this.load.image('base_tiles', './assets/16x16_Jerom_CC-BY-SA-3.0.png')
this.load.tilemapTiledJSON('tilemap', './assets/practiceMap.tmj')
this.load.image('littleFella', 'assets/littleFella.png')
this.load.image('tower', 'assets/tower.png')
this.load.image('cannonBall', 'assets/cannonball.png')
}
create() {
//map
const map = this.make.tilemap({ key: 'tilemap' })
const tileset = map.addTilesetImage('practiceMap', 'base_tiles')
map.createLayer('floor', tileset)
map.createLayer('walls', tileset)
//sprites
this.tower = this.physics.add.sprite( 200, 100, 'tower')
this.tower.setScale(3)
//create physics groups
this.cannonBall = this.physics.add.group()
this.enemies = this.physics.add.group({runChildUpdate: true })
//shoot count
this.nextTic = 0
//creates the path followers
this.time.addEvent({
delay: 3000,
callback: () => {
this.createLittleFella()
},
repeat: 0,
})
}
update() {
if(this.time.now > this.nextTic) {
this.fireCannon();
this.nextTic = this.time.now + 500;
}
}
createLittleFella = function(count) {
let path = new Phaser.Curves.Path(-40, 32)
path.lineTo(-40, 32)
path.lineTo(45, 32)
path.lineTo(45, 465) //518
path.lineTo(160, 465) //633
path.lineTo(160, 385) //713
path.lineTo(110, 385) //753
path.lineTo(110, 270) //868
path.lineTo(240, 270) //998
path.lineTo(240, 370) //1098
path.lineTo(305, 370) //1163
path.lineTo(305, 45) //1488
path.lineTo(450, 45) //1633
path.lineTo(450, 465) //2053
path.lineTo(530, 465) //2133 <-- if total in ten seconds velocity would be 213.3 pixels per second?
let mover = this.add.follower(path, -40, 32, 'littleFella');
this.enemies.add(mover)
mover.startFollow({
positionOnPath: true,
duration: 10000,
yoyo: false,
repeat: 0,
rotateToPath: false,
verticalAdjust: true
});
// coordinates [-40, 32, 45, 32, 45, 465, 160, 465, 160, 385, 110, 385, 110, 270, 240, 270, 240, 370, 305, 370, 305, 45, 450, 45, 450, 465, 530, 465 ]
this.time.addEvent({
delay: 10500,
callback: () => {
mover.destroy()
},
loop: false,
})
}
fireCannon = function(x, y, angle) {
let inRangeEnemy = this.closestEnemy(2000)
if (inRangeEnemy) {
let cannonBall = this.physics.add.sprite( 200, 100, 'cannonBall')
let angle = Phaser.Math.Angle.Between(200, 100, inRangeEnemy.x, inRangeEnemy.y);
angle = Phaser.Math.RadToDeg(angle)
const velocityFromAngle = this.physics.velocityFromAngle(angle, 2000, cannonBall.body.velocity)
}
}
closestEnemy = function(distance) {
var enemyUnits = this.enemies.getChildren();
if (enemyUnits) {
for(var i = 0; i < enemyUnits.length; i++) {
if (enemyUnits[i].active && Phaser.Math.Distance.Between(100, 100, enemyUnits[i].x, enemyUnits[i].y) <= distance) {
return enemyUnits[i];
}
}
}
}
};