Merlin
Merlin

Reputation: 978

How can I get EaselJS's MouseEvent to work properly? It SEEMS to work, but it keeps returning back a single bitmap ID, not the correct

I'm writing a mini-game in EaselJS that requires mouse clicks on a grid of tiles. However, it appears that it's only partially working - or perhaps not assigning a unique Event to each bitmap. During creation, I assign a simple ID - a number counting up per iteration, and expect to get that back.... but no matter where I click, out of 49 objects on stage (hexagon tiles), it only reports back 48....every time. I have enabled stage.enableMouseOver(20), which I initially forgot, but it did not help.

function BuildGround()
{
    var i = 0;
    for (var x = 0; x < 7; x++)
    {
        for (var y = 0; y < 7; y++)
        {

            var h = 102;
            var s = h/Math.cos(30*Math.PI/180)/2;


            var tempTile = new createjs.Sprite(Tiles, 0);
            WorldContainer.addChild(tempTile);          
            tempTile.regX = 64;
            tempTile.regY = 64;

            tempTile.id = i;

            tempTile.x = x * s * 1.5;
            tempTile.y = y * h + (x % 2) * h / 2;

            tempTile.addEventListener("click", function(event) { alert(tempTile.id); })

            TileArray.push(tempTile);
            i++;

            console.log(i);
        }
    }

    WorldContainer.x = 155;
    WorldContainer.y = 150;
}

Upvotes: 0

Views: 82

Answers (1)

renatopp
renatopp

Reputation: 1315

My guess, you are experiencing context problem of JavaScript, try this:

tempTile.addEventListener("click", function(event) { alert(event.target.id); })

All events objects have a target element, usually the one which triggered it.

Upvotes: 1

Related Questions