Sign in to follow this  
Steph

How to make flying enemy fall when player jumps on it

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;
    }, 

 

Share this post


Link to post
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;
    }, 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.