Jump to content

How to stop sprites from bouncing on eachother? (but still allow collisions)


Broccoli
 Share

Recommended Posts

I'm making a simple bug-catching game where the main character catches the bugs using the kill() function when they overlap. 

It's working, except that he can bounce on the bugs. How can I stop him from being able to bounce on them?

 

The update function looks like this: 

 

game.physics.arcade.overlap(this.player, this.fireflies, this.takeFirefly, null, this);

 

and the takeFirefly function looks like this:

 

takeFirefly: function(player, firefly) {
        firefly.kill();
        score++;
        this.txtScore.setText( score.toString()  );
    },

 

 

main.js

Link to comment
Share on other sites

The cause is found in the input logic you provide in your update function:

       if (this.cursor.up.isDown && this.player.body.touching.down){
          this.player.body.velocity.y = -600; //delete this line if you dont want the bounce
        } else if (this.cursor.right.isDown){  
          this.player.body.velocity.x = 300;
        } else if (this.cursor.left.isDown){
          this.player.body.velocity.x = -300;
        } else {
          this.player.body.velocity.x = 0;
        }     

Judging from the code, it looks like the bouncing when touched is exactly what you wanted in the first place?.You are telling Phaser: if I am holding the up key and the lower part of my body (belly) is being touched, then add velocity in -y direction/bounce up, which is true when you hit a firefly while holding the up key. If you want the player to go up only when you press up (so no bounce):

       if (this.cursor.up.isDown){
          this.player.body.velocity.y = -600;
        } 

        if (this.cursor.right.isDown){  
          this.player.body.velocity.x = 300;
        } else if (this.cursor.left.isDown){
          this.player.body.velocity.x = -300;
        } else {
          this.player.body.velocity.x = 0;
        }     

Is this what you were trying to achieve? Let me know if you can't work out what you are trying to achieve

Link to comment
Share on other sites

1 hour ago, samid737 said:

The cause is found in the input logic you provide in your update function:


       if (this.cursor.up.isDown && this.player.body.touching.down){
          this.player.body.velocity.y = -600; //delete this line if you dont want the bounce
        } else if (this.cursor.right.isDown){  
          this.player.body.velocity.x = 300;
        } else if (this.cursor.left.isDown){
          this.player.body.velocity.x = -300;
        } else {
          this.player.body.velocity.x = 0;
        }     

Judging from the code, it looks like the bouncing when touched is exactly what you wanted in the first place?.You are telling Phaser: if I am holding the up key and the lower part of my body (belly) is being touched, then add velocity in -y direction/bounce up, which is true when you hit a firefly while holding the up key. If you want the player to go up only when you press up (so no bounce):Is this what you were trying to achieve? Let me know if you can't work out what you are trying to achieve

 

Thanks for the reply. It includes "this.player.body.touching.down" because otherwise he goes infinitely high and just floats around. He needs to be able to jump , but only when touching down on ground and branches. The main problem is his ability to jump/bounce off things is applied to the fireflies.

I need to somehow specify that he can jump only when touching down on the ground and branches, but not on any bugs. 

Link to comment
Share on other sites

Oke then you should just check for collision within the if statement:

 if (this.cursor.up.isDown && this.player.body.touching.down&& (game.physics.arcade.collide(this.player,this.grasses)||game.physics.arcade.collide(this.player,this.branches))){
          this.player.body.velocity.y = -600;
        } else if (this.cursor.right.isDown){  
          this.player.body.velocity.x = 300;
        } else if (this.cursor.left.isDown){
          this.player.body.velocity.x = -300;
        } else {
          this.player.body.velocity.x = 0;
        }  

 

Link to comment
Share on other sites

6 hours ago, samid737 said:

Oke then you should just check for collision within the if statement:


 if (this.cursor.up.isDown && this.player.body.touching.down&& (game.physics.arcade.collide(this.player,this.grasses)||game.physics.arcade.collide(this.player,this.branches))){
          this.player.body.velocity.y = -600;
        } else if (this.cursor.right.isDown){  
          this.player.body.velocity.x = 300;
        } else if (this.cursor.left.isDown){
          this.player.body.velocity.x = -300;
        } else {
          this.player.body.velocity.x = 0;
        }  

 

 

 

Thanks a lot! I finally got it to work with some tweaking. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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