grapefruitjs / grapefruit

Outdated, I recommend you use photonstorm/phaser instead!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Tile click event not firing

krazyjakee opened this issue · comments

Looping through all the tiles in a layer is no problem.

_ref = tilemap.findLayer('Ground').tiles;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  row = _ref[_i];
  if (row != null) {
    for (_j = 0, _len1 = row.length; _j < _len1; _j++) {
      tile = row[_j];
      if (tile != null) {
        tile.on('click', function(e) {
          return console.log(e);
        });
      }
    }
  }
}

The event is assigned to the tile, and yet when I click, it isn't fired.

so sprite interaction events haven't been normalized yet (meaning the raw rendering engine method is the only current way). You have to first tell the sprite to be interactive, then set the onclick event:

tile.interactive = true;
tile.click = function(e) { console.log(e); }

Also, avoid making functions in a loop if they are all the same:

function onTileClick(e) {
    console.log(e);
}

// loops
//...
tile.interactive = true;
tile.click = onTileClick;

Proper scene interaction events will be normalized through the Pointer API when it is finished (in v0.2). At that point your code above should work.

worked, thanks very much