sg8

Why my physics arcade overlap function is not working

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 ?!");
    }

 

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
1 hour ago, drhayes said:

Don't modify the player's and the rock's positions directly -- change their velocities. If they don't have velocities then they won't overlap/collide correctly.

Either this or make them kinematic and move them by modifying their position directly.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.