Sprite passing through immovable wall?

24 Views Asked by At

So I'm trying to make a simple maze in Phaser.js, but collision has been a pain. I have gotten to the point where I can detect collisions between the sprite and my wall, but the sprite passes right through, even though I set the wall as immovable. Pretty new to phaser and js as a whole so it's probably something simple lol

    create() {
        this.cursors = this.input.keyboard.createCursorKeys()
        this.wall50x100 = this.physics.add.image(300,200,'50x100').setImmovable()
        this.sprite = this.physics.add.sprite(200,200,'sprite')
        this.sprite.setOrigin(0,0)
        //////////////////////////////////////////
        this.wall1 = this.physics.add.image(500,200,'50x100').setImmovable()
        this.wall1.setOrigin(0,0)
        //////////////////////////////////////////
        this.back = this.add.image(50,500,'back')
        this.back.setOrigin(0,0)
        this.back.setInteractive()

        this.back.on('pointerup',function(){
            this.scene.start('Scene2');
          },this)    

        this.add.text(20, 20, "Scene: 3", {font: "25px Arial", fill: "green"});
        this.physics.add.collider(this.sprite, this.wall50x100, function(sprite, wall50x100){
            console.log('Collision Detected')
        }.bind(this))
    }

I was expecting the sprite to not pass through the wall, but it did.

1

There are 1 best solutions below

0
winner_joiner On

There are atleast two posibile reasons I can think of, why the collision might not work:

  1. You are not moving the Sprite with "physics", you are setting the position of the sprite, instead of with velocity or acceleration. If you move the sprite by setting the position you override the phaser - "physics".
  2. Or the physics-bodys are to small or/and the sprite is moving to fast, so that they passthrough each other, without triggering a collision.

But it could be many other thing, these where the top two I could think of without seeing more code.

btw.: Following this Stackoverflow guide for examples might help you get better answer and also help you find you problem on you own. (p.s.: Voting and/or accepting answer might also help. having question, without accepted answer, own better solution, comments or some other form of engagement, might deter people from answering. )

Tipp: depending on you maze you could think about using a tilemap. Checkout this official examples, for some examples.