Jump to content

Bug? A have a function that updates score during collision, but it gets run twice instead of once (every collision)


kriket
 Share

Recommended Posts

I am using p2 physics but its simple really. I have player in a group and obstacle in a group and when these two collide a function called this.boom is run. 

    this.obstacleGroup.children[i].body.setCollisionGroup(this.obstacleCollisionGroup);        this.obstacleGroup.children[i].body.collides(this.playerCollisionGroup, this.boom, this);
    this.player.body.setCollisionGroup(this.playerCollisionGroup);    this.player.body.collides(obstacleCollisionGroup);       
Boom is: 
    boom: function() {        this.score -= 1;        console.log('Hotness');        this.scoreText.text = 'Score: ' + this.score;    },

 

 

Problem is, it gets run twice everytime.

Score becomes +2 instead of +1 everytime. Console logs twice everytime as well. 

 

 

Why is that? 

 

What can I do to get the scoring done properly on collision?

Link to comment
Share on other sites

Are you sure that not two objects in obstacleGroup aren't colliding with the playerCollisionGroup? 

 

Or that it doesn't run because the obstacle is colliding with the player but also the player colliding with the obstacle? I usually test collision between a group and the player so that it only runs one time from whatever happens first.

 

I'm not sure if I'm explaining this correctly but you should do the following test: have only one obstacle and one collision detection check (obstacle->player) and test if it runs twice.

Link to comment
Share on other sites

yeah, its only colliding with one obstacle at any given time. There's only three obstacles in the scene and I have placed them all far away from each other. Here's the code for them.

 

    // Obstacle    this.obstacleGroup = this.game.add.group();    this.obstacleGroup.enableBody = true;    this.obstacleGroup.physicsBodyType = Phaser.Physics.P2JS;      this.obstacle1 = this.obstacleGroup.create(this.game.width/7, this.game.height/6, 'obstacle');    this.obstacle2 = this.obstacleGroup.create(this.game.width/1.8, this.game.height/2.4, 'obstacle');    this.obstacle3 = this.obstacleGroup.create(this.game.width/2, this.game.height/1.15, 'obstacle');    for(var i = 0; i < this.obstacleGroup.children.length; i++){            this.obstacleGroup.children[i].body.setRectangle(20, 30, 0, 15);            this.obstacleGroup.children[i].body.static = true;            this.obstacleGroup.children[i].body.setCollisionGroup(this.obstacleCollisionGroup);            this.obstacleGroup.children[i].body.collides(this.playerCollisionGroup, this.boom, this);    }

So, yes, triple-checked. Its only colliding with one. 

 

Also, if I only have one collision test, and omit say 

this.player.body.collides(obstacleCollisionGroup);       

and only leave in 

this.obstacleGroup.children[i].body.collides(this.playerCollisionGroup, this.boom, this);
Or vice-versa,
 
well no collision happens. That's expected cos I always thought you have to specify body.collides on both groups that are going to collide. 
 
 
Also, if I change         this.score -= 1;
in boom function to          this.score -= 2;
then score actually gets decremented by -4! 
So yes, something is not right here and everything is happening twice (be it scores or console.log). 
 
 
The code is so simple and thats why I don't understand why this is happening. 
 
Game is complete now and this is the only thing/bug thats happening before I release so any help appreciated as want to get it out there now!!
 
 
PS - It may not be a bug in phaser or if it is it maybe an old one since I tested using phaser 2.3 and 2.4.2 and this issue happens with both. Damn it!
Link to comment
Share on other sites

Are you sure it's not because collision happens multiple times, even for only 2 entities? I mean, A and B collide => callback is triggered. One update later, they have moved from X pixels. But they still collide! => callback triggered again!

 

If you slow down the velocities, do you see the callbacks happening more than twice?

 

I don't know if the collide method only triggers the callback once per default (on the first encounter). I would be surprised if it was smart enough to do that.

Link to comment
Share on other sites

Are you sure it's not because collision happens multiple times, even for only 2 entities? I mean, A and B collide => callback is triggered. One update later, they have moved from X pixels. But they still collide! => callback triggered again!

 

If you slow down the velocities, do you see the callbacks happening more than twice?

 

I don't know if the collide method only triggers the callback once per default (on the first encounter). I would be surprised if it was smart enough to do that.

 

 

This is the full callback function. 

    boom: function(a) {        this.exp.position.x = a.sprite.position.x;         this.exp.position.y = a.sprite.position.y;        this.exp.animations.play('exp_anim', 32);        a.sprite.kill();        this.score -= 1;        console.log('debug');        this.scoreText.text = 'Score: ' + this.score;    },
As you can see I kill one of the sprites before updating the function. Shouldnt that prevent any double collisions?
 
I cant decrease the velocity as the character is moving perfectly now and changing his movement will affect gameplay and concept, and thats my last resort if nothing else works.
 
Hmmm, what the hell could be happening.....
Link to comment
Share on other sites

So to sum up. You see "debug" on the console log once, but the score goes from 10 to 8 instead of 9 correct?

 

If that is the case could you add a console log before the decrease of the score to show its value ( window.console.log(this.score); )?

And do this instead; this.score = this.score - 1; (because you know javascript is kinda crazy)

Link to comment
Share on other sites

thanks a lot for helping mate as I am running out of ideas. 

   boom: function(a) {        this.exp.position.x = a.sprite.position.x;         this.exp.position.y = a.sprite.position.y;        this.exp.animations.play('exp_anim', 32);//        a.sprite.kill();        a.sprite.destroy();        this.score -= 1;        this.scoreText.text = 'Score: ' + this.score;    },

 

Replacing kill function with destroy, actually solves the scoring problem!! Thank you! Although I do lose out on object pooling and game takes a performance hit, especially cos its a mobile game. But a slight performance hit is better than buggy scoring. These obstacles are placed throughout the game and pooling them would have given a significant boost. So am still interested to know why everything happens twice with kill()?

Link to comment
Share on other sites

So to sum up. You see "debug" on the console log once, but the score goes from 10 to 8 instead of 9 correct?

 

If that is the case could you add a console log before the decrease of the score to show its value ( window.console.log(this.score); )?

And do this instead; this.score = this.score - 1; (because you know javascript is kinda crazy)

 

Thats not the case. I see debug twice in console. 

Link to comment
Share on other sites

If the kill is the problem maybe try to set a.sprite.enable = false; also but my guess is that because you declare two collision checks it gets checked twice

 

To check only once you could do the following

game.physics.arcade.collide(this.player, this.obstaclePool, boom, null, this); 
Link to comment
Share on other sites

 

If the kill is the problem maybe try to set a.sprite.enable = false; also but my guess is that because you declare two collision checks it gets checked twice

 

To check only once you could do the following

game.physics.arcade.collide(this.player, this.obstaclePool, boom, null, this); 

 

 

hi, thanks for helping. I am using p2 and in p2 you have to have the collide() on both bodies. Else, it doesnt work. It different from arcade though as there's not two collision check happening. One is a collision check, the other just "tells" the player the list of bodies he is to collide with. So its not two checks. Its only one. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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