Jump to content

Collision player on movable platform


chon27
 Share

Recommended Posts

Hello!

I'm problems with this case;

I have a map loaded from tiled with blocks that they are collisionable, some like:

// Import tiled map
this.map = this.make.tilemap({ key: 'fase' });
this.tileset = this.map.addTilesetImage('tileset');
this.layer = this.map.createDynamicLayer(0, this.tileset, 0, 0);

// Set collision in two type of blocks
this.layer.setCollisionBetween(0, 2);

// Set collision between player and layer
this.physics.add.collider(this.solomon, this.layer);

So this part is correctly functional as can see in the gif, player walk over blocks.

Quote

this.solomon is created with a new class that extends from Phaser.Physics.Arcade.Sprite.

Then I created a new movable block like this:

this.movil = this.physics.add.sprite(180, 104, 'block');
this.movil.setVelocityX(30);
this.movil.setImmovable(true);
this.movil.body.allowGravity = false;
this.movil.setBounce(1);

// Collision with the layer
this.physics.add.collider(this.movil, this.layer);

// And collision with the player
this.physics.add.collider(this.solomon, this.movil);

My problem is why the player doesn't collision with the layer when he is on the movable block. However if I walk against it while I'm on movable block, the collision does work!! (As seen at the end of gif, before he falls)

Some help?

 

collision_issue.gif

Link to comment
Share on other sites

Thanks for you reply. I was on vacation and I couldn't try it until now.

This does not work but you give me an idea of problem that I solved like this:

this.physics.add.collider(this.solomon, this.movil, function() {
        this.onPlatform = true;
});

And the other side, at update function:

if(this.onPlatform && this.solomon.body.velocity.x === 0) {
    // In order to know direction on movement (1 to right or -1 to left)
    let dir = this.movil.body.newVelocity.x > 0? 1: -1;
    // Then sets velocity X to solomon multiplicate by the direction to make collision by both sides 
    this.solomon.body.setVelocityX(0.001*dir);
}

// Reset onPlatform to set false when leaves it
this.onPlatform = false;

Now I have only to optimize or atomize a bit.

The result in the gif.

Thank you very much!

 

solomon_done.gif

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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