Jump to content

Group vs Group Collide problem


Cutu
 Share

Recommended Posts

Hey guys, I'm making this game where you have to go through the hole in the walls by moving the ship left or right.

What I need is to increment the score everytime you pass through one hole. The walls are a group of blocks that spawn every 1 second with a timer.

I tried to increment the score using group.getFirstAlive() and checking if the Y position was higher than the player's one but the problem is that every row of blocks spawn each second and the group.getFirstAlive() takes only the first child until it dies and when it dies the other 2 or 3 rows that spawned before won't increment the score.

So I thought that I could increment the score by creating and invisible group of blocks below the player and use the collision handler to increment the score everytime both walls overlap. This is the code I'm using currently:

this.game.physics.arcade.overlap(this.pointBlocks, this.walls, (function(){ this.score+=1;
        this.scoreText.setText(this.score.toString()); }).bind(this));

The problem with this is that when the walls overlap the score doesn't increment by 1 but by 300 or the number of blocks in each row of pointBlocks(the invisible ones).

Is there a way to just increment the score by 1??

Thanks in advance! And sorry for my English.

Spaceship.png

Link to comment
Share on other sites

Wouldn't it be simpler to overlap the player with the goal areas (gaps/holes)? Then you can just disable the goal the first time it's hit:

this.physics.arcade.overlap(this.player, this.goalBlocks, (function (player, goalBlock){
    // sprite arg is always first in Sprite vs Group
    goalBlock.body.enable = false;
    this.score + =1;
    this.scoreText.setText(this.score.toString());
}), null, this);

 

Link to comment
Share on other sites

Hey @samme thanks for your answer, I already solved my problem this way:

    addWall: function(x,y,point) {
        
            var wall = this.walls.getFirstDead();
        
        wall.reset(x,y);
        wall.body.velocity.y = this.wallSpeed;
        wall.body.immovable = true;
       
        wall.checkWorldBounds = true;

        if(point == 0) { //Added this so it will trigger with the first block in every row and not every block.
            wall.events.onOutOfBounds.add(this.incrementScore, this);
        }

        wall.outOfBoundsKill = true;
        
    },
    
    addWallRow: function() {
        
        this.wallSpeed += 5;
        
        var tilesNeeded = Math.ceil(this.game.world.width / this.wallWidth);
        
        var hole = Math.floor(Math.random() * (tilesNeeded - 3)) + 1;
        
        for(var i=0;i<tilesNeeded;i++) {
            if(i != hole && i != hole + 1) {
                this.addWall(i * this.wallWidth, 0, i);
        }
        
    }

This is working fine because it add exactly 1point every time the row of blocks go out of bounds so I had to move the player position a bit lower in the screen.

I tried overlaping the player with the hole (by adding 2 invisible blocks in that area) but the callback triggered like 50 times but maybe because I didn't disable its body, I'm gonna give it a try anyways!

Thank you!

 

Edit: @samme I already tried your suggestion but since I have 2 invisibles blocks it adds 2 points instead of 1 if the player overlap just in the middle (because it hits both blocks) and I can't add 0,5 points because sometimes the player won't hit both blocks :/

Edited by Cutu
To avoid double post
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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