Abhay Sharma Abhay Sharma - 1 year ago 238
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.


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

// 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

// csll fun. for place astroid
// call fun. for Ans

// Call fun. for ques
// Call fun. for ques

// Loading Diamond image
// Start timer
// Set timer.


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');

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

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

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() {


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) {

// When a bullet hits an alien we kill them both


Answer Source

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) {

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download