Jump to content

Why my physics arcade overlap function is not working


sg8
 Share

Recommended Posts

So i'm just trying to make a simple collision between falling objects and a players sprite. Its nothing complicated.

I'm trying to use the game.physics.arcade.overlap() function.

Here is my code:

The Player class ->

export class Player{
    game: Phaser.Game;
    player: Phaser.Sprite;

    constructor(game:Phaser.Game){
        this.game = game;
        this.player = this.game.add.sprite(400, 520, "Player");
        this.game.physics.arcade.enable(this.player);
    }

    create(){
    }

    update(){

        if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
        {
            if(this.player.x >= -10){
                this.player.x -= 7;
            }
        }
        else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
        {
            if(this.player.x <= 830){
                this.player.x += 7;
            }
        }
    }
}

The falling objects class ->

 

    export class Rock {

    game:Phaser.Game;
    rocks: Phaser.Group;

    constructor(game:Phaser.Game){
        this.game = game;
    }

    create(){

        this.rocks = this.game.add.physicsGroup(Phaser.Physics.ARCADE);
        this.game.physics.arcade.enable(this.rocks);

        var x = 10;

        for(var i = 0; i < 9; i++){
            var rock = this.rocks.create(x, this.game.rnd.between(-30,-5), "Rock");
            rock.body.velocity.y = this.game.rnd.between(240,300);
            x += 105;
        }
    }

    update(player){
        this.rocks.forEach(this.checkPos, this);
    }

    checkPos(rock) {
        if (rock.y > 800)
        {
            rock.y = -100;
        }
    }
}

The Main Game File where i'm using the overlap function ->

create(){

        this.difficulties = [];
        this.difficulties.push(new Difficulty(0, 5));
        this.difficulties.push(new Difficulty(1, 7));
        this.difficulties.push(new Difficulty(2, 9));
        this.currentDifficulty = this.difficulties[0];
        this.shouldChangeDifficulty = true;

        this.levelOne = new LevelOne(this.game);
        this.levelOne.create(this.currentDifficulty);
        this.currentLevel = this.levelOne;

        this.player = new Player(this.game);
        this.player.create();

        this.rocks = new Rock(this.game);
        this.rocks.create();
    }

    update(){
        this.player.update();
        this.rocks.update(this.player);

        this.game.physics.arcade.overlap(this.player, this.rocks, this.collisionHandler, null, this);
    }

    collisionHandler (player, rock) {
        console.log("Does it work ?!");
    }

 

Link to comment
Share on other sites

3 hours ago, Igor Georgiev said:

does this method this.game.physics.arcade.overlap has to be in the update? Isn't it internally going through the update? It does not work if it is outside the update, so I guess this is the way to go.

If you want the overlap between the two bodies to be checked every game update then yes it should be in the update function. Otherwise you can call it just when you need it, like when some other event happens. If for example you have a player that can move into things, you could just do the overlap/collision checks when the move input is down.

Link to comment
Share on other sites

2 hours ago, Arcanorum said:

If you want the overlap between the two bodies to be checked every game update then yes it should be in the update function. Otherwise you can call it just when you need it, like when some other event happens. If for example you have a player that can move into things, you could just do the overlap/collision checks when the move input is down.

I understand now. Thank you for clarifying this for me :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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