2016-07-28 18 views
0

Ich möchte die Kugel töten, da sie den Asteroiden überlappt, aber es funktioniert nicht. Bitte schauen Sie sich meinen Code an. Ich habe viele Male nach dem Code geschaut, aber alles sieht gut aus. Auch ich möchte Kollision über Asteroiden durchführen.game.physics.arcade.overlap() funktioniert nicht in phaser

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

 

 
    } 
 
    .

Antwort

1

Soweit ich weiß, sollten Sie die Kollisionsprüfung in eine Update-Funktion so:

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

und töten die Sprites in dieser Funktion (in diesem Fall die bulletHitAsteroid-Funktion).

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

Ich hoffe, das hilft.