2016-07-28 86 views
0

我想杀死子弹,因为它与小行星重叠,但它不起作用。请看我的代码。我多次查看代码,但一切看起来都很正确。我也想在小行星上进行碰撞。game.physics.arcade.overlap()在移相器中不工作

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

 

 
    } 
 
    .

回答

1

据我所知,你应该把碰撞检查到更新功能如下:

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

杀该函数中的精灵(在本例中为bulletHitAsteroid函数)。

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

我希望这有助于。