Broccoli

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

5 posts in this topic

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

Share this post


Link to post
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

Share this post


Link to post
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. 

Share this post


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

 

Broccoli likes this

Share this post


Link to post
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. 

samid737 likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.