Jump to content

problem detecting collisions in game


DuranDuran
 Share

Recommended Posts

Hello,

 

I am making a game using this tutorial:

https://gamedevacademy.org/how-to-make-an-infinitely-scrolling-game-with-phaser/

 

I made some changes to it using the Phaser Basic Project template.  I am having a problem with the collision detection. In the update function I have these lines detecting if a collision has happened:

    update: function () {
        console.log("in the update function!!!!!!!!!");
        //  Honestly, just about anything could go here. It's YOUR game after all. 
        // Eat your heart out!

        // player land on ground instead of falling through
        this.game.physics.arcade.collide(this.player, this.ground, this.playerHit, null, this);
        // player bitten by a flea
        this.game.physics.arcade.collide(this.player, this.fleas, this.playerBit, null, this);
        // player can overlap with dirt mounds
        this.game.physics.arcade.overlap(this.player, this.mounds, this.collect, this.checkDig, this);

This collision mechanism works as long as the player does not collide with a flea or digs.  (I guess this means the collision mechanism does not work.)  For example when the player collides with a flea, this.playerBit  is called:

 playerBit: function (player, flea) {
        //remove the flea that bit our player so it is no longer in the way
        flea.destroy();

        //update our stats
        this.scratches++;
        this.refreshStats();

        //change sprite image
        this.player.loadTexture('playerScratch');
        this.player.animations.play('scratch', 10, true);

        //play whine audio
        this.whineSound.play();

        //wait a couple of seconds for the scratch animation to play before continuing
        this.stopped = true;
        this.player.body.velocity.x = 0;
        this.game.time.events.add(Phaser.Timer.SECOND * 2, this.playerScratch, this);
    },
    refreshStats: function () {
        this.pointsText.text = this.points;
        this.fleasText.text = this.maxScratches - this.scratches;
    },
  playerScratch: function () {
        this.stopped = false;

        // check the number of scratches, if 5 or greater
        // the player dies
        if (this.scratches >= 5) {
            console.log("scratches greater than 4");
            this.player.alive = false;

            // reset world,
            this.fleas.destroy();
            this.mounds.destroy();

            this.player.loadTexture('dog');
            this.player.animations.play('walk', 10, true);
            this.player.body.setSize(this.player.standDimensions.width, this.player.standDimensions.height);

            //.. then run home
            this.player.anchor.setTo(.5, 1);
            this.player.scale.x = -1;
            this.player.body.velocity.x = -1000;

            // run off the screen
            this.game.camera.unfollow();

            //..then go to Game Over state
            this.game.time.events.add(15000, this.gameOver, this);

        } else {
            console.log("in the playerScratch function!!!!!!!!!");
            this.player.loadTexture('dog');
            this.player.animations.play('walk', 3, true);
            this.player.body.setSize(this.player.standDimensions.width, this.player.standDimensions.height);
        }
        console.log("leaving the playerScratch function");
    },

After the player scratches, the player sinks below the ground.  Why is this?

Here is the update function :

 update: function () {
        console.log("in the update function!!!!!!!!!");
        //  Honestly, just about anything could go here. It's YOUR game after all. 
        // Eat your heart out!

        // player land on ground instead of falling through
        this.game.physics.arcade.collide(this.player, this.ground, this.playerHit, null, this);
        // player bitten by a flea
        this.game.physics.arcade.collide(this.player, this.fleas, this.playerBit, null, this);
        // player can overlap with dirt mounds
        this.game.physics.arcade.overlap(this.player, this.mounds, this.collect, this.checkDig, this);


        //only respond to keys and keep the speed if the player is alive
        if (this.player.alive && !this.stopped) {

            this.player.body.velocity.x = 250;
            //We do a little math to determine whether the game world has wrapped around.
            //If so, we want to destroy everything and regenerate, so the game will remain random
            if (!this.wrapping && this.player.x < this.game.width) {
                //Not used yet, but may be useful to know how many times we've wrapped
                this.wraps++;

                // once we wrap we want to destroy everything and regenerate the world
                this.wrapping = true;
                this.fleas.destroy();
                this.generateFleas();
                this.mounds.destroy();
                this.generateMounds();

                // then put things back in the correct order
                this.game.world.bringToTop(this.grass);
                this.game.world.bringToTop(this.mounds);
                this.game.world.bringToTop(this.ground);
            } else if (this.player.x >= this.game.width) {
                this.wrapping = false;
            }


            //take the appropriate action for swiping up or pressing up arrow on keyboard
            //we don't wait until the swipe is finished (this.swipe.isUp),
            //  because of latency problems (it takes too long to jump before hitting a flea)
            if (this.swipe.isDown && (this.swipe.positionDown.y > this.swipe.position.y)) {
                this.playerJump();
            }
            else if (this.cursors.up.isDown) {
                this.playerJump();
            }

            //The game world is infinite in the x-direction, so we wrap around.
            //We subtract padding so the player will remain in the middle of the screen when
            //wrapping, rather than going to the end of the screen first.
            this.game.world.wrap(this.player, -(this.game.width / 2), false, true, false);

        }

    },

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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