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 );


Share this post

Link to post
Share on other sites

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.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.