grapefruitjs / grapefruit

Outdated, I recommend you use photonstorm/phaser instead!
MIT License
106 stars 14 forks source link

Tile click event not firing #86

Closed krazyjakee closed 10 years ago

krazyjakee commented 10 years ago

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.

englercj commented 10 years ago

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.

krazyjakee commented 10 years ago

worked, thanks very much