Jump to content

Removing Collision from "UP" direction of sprite


bossikkk
 Share

Recommended Posts

I use matter physics. As a basis, I took this example: link

Removed wall jump and add up sensor. And in these fragments of the code I tried to implement the:

function create(){
 this.matter.world.on('collisionstart', function (event) {
        var left = playerController.sensors.left;
        var right = playerController.sensors.right;
        var up = playerController.sensors.up;

        for (var i = 0; i < event.pairs.length; i++) {
            var bodyA = event.pairs[i].bodyA;
            var bodyB = event.pairs[i].bodyB;

            if (bodyA === up || bodyA === right || bodyA === left) {
                console.log('wtf');
            }

            if (bodyB === up || bodyB === right || bodyB === left) {

                if (bodyA.gameObject) {
                    if (bodyA.gameObject.tile.layer.name === "behindGround2") {
                        bodyA.collisionFilter.category = 0;
                        touchedTiles.push(bodyA);
                        setTimeout(() => {
                            restoreCollision = true;
                        }, 400);
                    }
                } else {
                    if (bodyA.parent.gameObject.tile.layer.name === "behindGround2") {
                        bodyA.parent.collisionFilter.category = 0;
                        touchedTiles.push(bodyA.parent);
                        setTimeout(() => {
                            restoreCollision = true;
                        }, 400);
                    }
                }

            }
        }
    })
}
function update(){
 if (!playerController.blocked.bottom && matterSprite.anims.currentAnim.key === 'jump') {

            if (touchedTiles.length > 0 && restoreCollision) {
                restoreCollision = false;

                touchedTiles.forEach((el, index, arr) => {
                    el.collisionFilter.category = 1;
                    if (index === arr.length - 1) {   
                        touchedTiles = [];
                        console.log('Array is cleared');
                    }
                });
            }
       }
}

Game screen:

asdad.thumb.PNG.77d06936987362e339d7e483f1b8ce0b.PNG

Is there a working way to do this? My code works through time to time. Very often the player simply stops flying through the object.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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