Abhay Sharma Abhay Sharma - 4 months ago 53
Javascript Question

game.physics.arcade.overlap() is not working in phaser

I want to kill the bullet as it overlap the asteroid but its not working. Please have a look of my code. I looked in to the code many times but everything is looking right. Also i want to perform collision over asteroid.

game.js



var bullets;
var Game = {
preload: function() {
// Load the needed image for this(play) game screen.
//load the menu screen
this.load.image('menu', './assets/images/menu.png');

// Here we load all the needed resources for the level.
// background image screen
this.load.image('playgame', './assets/images/back.png');

// globe image screen
// this.load.image('playgame', './assets/images/back.png');


this.load.image('spaceship', './assets/images/spaceship.png');

// Astroid image screen
this.load.image('astroid1', 'assets/images/asteroid1.png');
this.load.image('astroid2', 'assets/images/asteroid2.png');
this.load.image('astroid3', 'assets/images/asteroid3.png');
this.load.image('astroid4', 'assets/images/asteroid4.png');

//Load the bullet
this.load.image('bullet', 'assets/images/bullet.png');
},
create: function() {

// By setting up global variables in the create function, we initialise them on game start.
// We need them to be globally available so that the update function can alter them.

//this will show the physics of background
game.physics.startSystem(Phaser.Physics.ARCADE);

// The scrolling starfield background
gameback = game.add.tileSprite(0, 0, 800, 600, 'playgame');

textStyle_Value = {
font: "bold 20px Segoe UI",
fill: defTextColor,
align: "center"
};
textStyleAns = {
font: "bold 22px 'Comic Sans MS', 'Comic Sans'",
fill: ansTextColor,
wordWrap: true,
wordWrapWidth: 10,
align: "center"
};
textStyleQues = {
font: "bold 20px 'Comic Sans MS', 'Comic Sans'",
fill: defTextColor,
wordWrap: true,
wordWrapWidth: 10,
align: "center"
};


// Loading questionbar image
this.questionbar();

// csll fun. for place astroid
this.astroid();
// call fun. for Ans
this.generateQues();
this.generateAns();

// Call fun. for ques
this.comeQus();
// Call fun. for ques
this.comeAns();

// Loading Diamond image
this.diamond();
// Start timer
this.startTimer();
// Set timer.
this.setTimer();

this.initLoader();


bullets = game.add.group();
bullets.enableBody = true;
bullets.physicsBodyType = Phaser.Physics.ARCADE;
bullets.createMultiple(500, 'bullet', 150, false);
// bullets.setAll('anchor.x', 0.5);
// bullets.setAll('anchor.y', 0.5);
bullets.setAll('outOfBoundsKill', true);
bullets.setAll('checkWorldBounds', true);

sprite = game.add.sprite(400, 530, 'spaceship');
sprite.anchor.set(0.4);

// Playing backgroud music
fun_bckg = this.add.audio('fun_bckg', 1, true);
fun_bckg.volume = 0.5;
this.playFx('fun_bckg');

// Bullet fire music
fire_bullet = this.add.audio('fire_bullet', 1, true);
fire_bullet.volume = 0.5;
//this.playFx('fire_bullet');

},
astroid: function() {

astroid1 = this.add.button(25, 100, 'astroid1', this.astroidClicked, this);
astroid2 = this.add.button(250, 30, 'astroid2', this.astroidClicked, this);
astroid3 = this.add.button(400, 40, 'astroid3', this.astroidClicked, this);
astroid4 = this.add.button(570, 100, 'astroid4', this.astroidClicked, this);


},

fire: function() {


this.playFx('fire_bullet');



if (game.time.now > nextFire && bullets.countDead() > 0) {
nextFire = game.time.now + fireRate;

var bullet = bullets.getFirstDead();

bullet.reset(sprite.x - 0, sprite.y - 140);

game.physics.arcade.moveToPointer(bullet, 300);
}

}


}

function collisionHandler(bullets, astroid1) {
alert("hello");

// When a bullet hits an alien we kill them both
bullets.kill();
astroid1.kill();


}
.




Answer

As far as I know, you should put the collision check into an update function like this:

update: function() {
   // check for collisions
   game.physics.arcade.collide(bullets, asteroid, this.bulletHitAsteroid, null, this);
}

and kill the sprites in that function (in this case the bulletHitAsteroid function).

fire: bulletHitAsteroid(_bullets, _asteroid) {
   _bullet.kill();
   _asteroid.kill();
}

I hope this helps.

Comments