I am trying to determinate side where is bullet hit
It works only when player
is moving. If player moving right, in console I get ‘right’, if moving left - ‘left’. When player is standing - he tocuhes only ‘down’. Overlap is processing in playerHitCallback and all overlaps writes in console.
Why playeris is touches only ‘down’ when player is standing?
let Bullet = new Phaser.Class({
Extends: Phaser.Physics.Arcade.Image,//Phaser.GameObjects.Image,
initialize:
// Bullet Constructor
function Bullet (scene){
Phaser.Physics.Arcade.Image.call(this, scene, 0, 0, 'bullet');
this.speed = 1;
this.born = 0;
this.xSpeed = 1;
this.ySpeed = 0;
//this.setSize(12, 12, true);
},
fire: function (shooter, target){
this.setPosition(shooter.x, shooter.y); // Initial position
if (target != 'right' && target != 'left'){
console.log('atack player');
} else {
if (target == 'right'){
this.xSpeed = this.xSpeed;
}
else {
this.xSpeed = this.xSpeed * -1;
}
}
this.born = 0; // Time since new bullet spawned
},
// Updates the position of the bullet each cycle
update: function (time, delta){
this.x += this.xSpeed * delta;
this.born += delta;
if (this.born > 3000){
this.setActive(false);
this.setVisible(false);
this.destroy(true);
}
}
});
preload(){
this.load.spritesheet('person', '../assets/person.png', { frameWidth: 50, frameHeight: 70 });
this.load.image('ground', '../assets/ground.png');
//...
}
create(){
this.player = this.physics.add.sprite(100, 200, 'person');
this.player.setCollideWorldBounds(true);
this.platforms = this.physics.add.staticGroup();
this.platforms.create(0, 550, 'ground').setOrigin(0).refreshBody();
this.physics.add.collider(this.player, this.platforms);
this.enemyBullets = this.physics.add.group({ classType: Bullet, runChildUpdate: true, allowGravity: false });
//...
}
update(){
if(this.fire.isDown){
var bullet = this.playerBullets.get().setActive(true).setVisible(true);
if (bullet){
bullet.fire(this.player, this.target);
}
}
this.enemyFire(this.bad, this.player, this);
}
enemyFire(enemy, player, gameObject){
if (enemyTime > 20){
enemyTime = 0;
// Get bullet from bullets group
let bullet = this.enemyBullets.get().setActive(true).setVisible(true);
if (bullet){
bullet.fire(enemy, 'left');
// Add collider between bullet and player
gameObject.physics.add.overlap(player, bullet, this.playerHitCallback);
}
}
};
playerHitCallback(playerHit, bulletHit){
if (playerHit.body.touching.left) {
console.log('left');
} else if (playerHit.body.touching.right) {
console.log('right');
} else {
console.log(playerHit.body.touching);
}
bulletHit.setActive(false).setVisible(false).destroy(true);
};