Home > Enterprise >  Can I an Identifier for a tile in Phaser.js
Can I an Identifier for a tile in Phaser.js

Time:01-29

So, I've been making a sort of 2d minecraft, and i need a Phaser method/function to be able to identify each tile by their tileset number,

For example, when i place a tile from a tileset, i require its tile set no.

this.block = this.worldLayer.putTileAtWorldXY(
          6,
          this.worldPoint.x,
          this.worldPoint.y
        );
        this.block.setCollision(true);

that 6 is the tile number of a tile from my tile set, and i need a way to get that number from any tile when i click it, is there anyway to do this?

thanks in advance

CodePudding user response:

I actually found a method that works at

https://www.html5gamedevs.com/topic/9627-how-to-get-tile-index-property/

use .getTileAt().index

CodePudding user response:

I'm not really familiar with phaser but when you create/put a tile with its number, just add this as a property to the tile. I assume you can reach the "tile" object when it's clicked, so you can access its tile number from the property.


something similar in vanilla javascript

function putTile(tileNo) {
  let el = document.createElement('div')
  el.no = tileNo
  el.className = "tile no"   tileNo
  el.onclick = e => console.log(el.no)
  document.body.appendChild(el)
}

document.querySelector('[add1]').onclick = e => putTile(1)

document.querySelector('[add2]').onclick = e => putTile(2)
.tile,
button {
  padding: 1rem;
  margin: 0.2rem;
}

.no1 {
  background: coral !important;
}

.no2 {
  background: navy !important;
}
<div>
  <button add1>add tile no 1</button>
  <button add2>add tile no 2</button>
</div>

clicking buttons will create "tiles"
clicking the tiles will log their №

  • Related