Jump to content

Collide with a door and read custom properties of an object


Recommended Posts

I have this map created in Tiled, with an object layer for the doors. And this door object has a custom property "link", which should tell where the door goes.

In phaser, I created an object layer for this doors and put them in to a new static group (this.physics.add.staticGroup). And if the player hits this door object, a method get called: this.physics.add.overlap( this.player, doors, this.enterDoor , null, this ). And there, in the enterDoor method, I want to read the "link" property, so I can switch the scene based on that value, which tells where to go next. But I am not able to get it there, it not seems to be possible to even add this value to the childs of the group, because already there, these values get lost. I am looking for a solution to this problem.



These are the parts of the relevant code:

Part 1, colliding with the door works without problems. Only thing there is, I have a missing sprite picture in game, but I do not want to use any sprite for the door. It should be a invisible area and if the player hits, it should go to the next scene.

    this.map = this.add.tilemap( 'map1' );
    var tileset = this.map.addTilesetImage( 'main', 'tiles' );

    this.firstLayer = this.map.createStaticLayer( 'layer1', tileset );
    this.secondLayer = this.map.createStaticLayer( 'layer2', tileset );
    this.thirdLayer = this.map.createStaticLayer( 'layer3', tileset );

    this.createPlayer( 400, 400 );

    this.secondLayer.setCollisionBetween( 1, 2000 );
    this.physics.add.collider( this.player, this.secondLayer);

    this.objectLayer = this.map.getObjectLayer( 'objectLayer' );

    var doors;

    this.objectLayer.objects.forEach( ( object, index ) =>
        if (object.type === 'door')
          object.key = object.name;
          object.setXY = { x: object.x + 16, y: object.y + 16 };
          doors = this.physics.add.staticGroup( object );

          //doors[ index ].set( 'link', object.link );

    this.physics.add.overlap( this.player, doors, this.enterDoor , null, this );


Part 2, this is the enterDoor Method, called if the player hits the door. There I want to read the door.link property and based on that, go to the next scene.

enterDoor( player, door )
    console.log( door.link ); //undefined

    door.disableBody( true, true) ;
    this.scene.switch( 'Outside', Outside );

    // it should be like this:
    // this.scene.switch( door.link, door.link );


Link to comment
Share on other sites

  • 4 months later...
  • 9 months later...

I had the same issue and posted the location of the custom properties here if anyone is still interested



Note that I found it in the `data` property with the current Phaser version, 3.23. I'm not sure if it was always set in earlier iterations of Phaser 3.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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