Jump to content

How to make flying enemy fall when player jumps on it


Steph
 Share

Recommended Posts

I have an endless runner with flies that are enemies flying towards the player. The player jumps into the air and onto the top of the fly to kill it. I would like to enable gravity on the fly on overlap so that it falls to the ground when it dies. However, my problem is that when the fly falls sometimes it overlaps with the player sprite on decent causing the overlapCallback function (killFlies) to be called more than once. How can I have an enemy sprite like this die and adhere to gravity yet not have the overlapCallback function triggered again when it's falling? Any ideas? Is there any way to disable overlap once the overlap occurs? Setting fly.body.enable = false won't work as I then can't set velocity or gravity on the fly sprite when it dies. Please see my overlapCallback function below. Any suggestions would be much appreciated (am new to Phaser).

 


    killFlies: function(player, fly){

      if (player.body.touching.down) { 
        this.bounce();
        
        //fly.body.enable = false; 
          
        fly.body.allowGravity = true; 
    
        this.stompSound.play();

         fly.animations.play('die').onComplete.addOnce(function () {
        
    fly.kill(); 
    
   }, this);

}else {
  
  this.gameOver();
}

fly.body.touching = fly.body.wasTouching;
    }, 

 

Link to comment
Share on other sites

I have actually just done a work around for this by simply adding a tween rather than using sprite frames to achieve the effect I was after. Code is below for those interested. However, if anyone has a solution to my original question it would be great to know. Thanks.

   killFlies: function(player, fly){

    if (player.body.touching.down) {
        this.bounce();

    this.stompSound.play();

    this.deadFly = this.add.sprite(fly.x, fly.y, "deadFly");
    this.deadFly.anchor.setTo(0.5,0.5);
    this.deadFlyTween = this.game.add.tween(this.deadFly).to({x:+50, y: 550}, 800, Phaser.Easing.Linear.In, true);
    this.deadFlyTween.start();

  var newScore = 1;
  this.createScoreAnimation(fly.x, fly.y, '+' +newScore, newScore);
        
    fly.kill(); 

} else {

  this.gameOver();
}

fly.body.touching = fly.body.wasTouching;
    }, 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...