Been struggling for a while on this so I decided to craft up a jsfiddle to show an example:
gameworld = document.getElementById('GameWorld');
character = document.getElementById('c');
gameworld.addEventListener('click', function(e){
console.log(e);
character.style.left=''+e.offsetX+'px';
character.style.top=''+e.offsetY+'px';
});
Fiddle here: http://jsfiddle.net/g43nxhcw/1/
If you click anywhere on the red container, you grab the offsetX
, and offsetY
. But, if you click on the tile, you grab its offset instead, which is causing the character to not move to that spot.
What I am trying to do is grab the GameWorld's
offsetX
and offsetY
even if the tile is clicked upon, is that possible?
When looking at the console we can see that e.target
shows which element the user has clicked on. This is about as close as I can get, because I need to find the offset's of the GameWorld's
element when a parent child element of it, is clicked on.
I can get access to GameWorld's
node when the img
tile is clicked on, by accessing:
e.path[0]
, as pertinent here
Sadly, that is where I get stuck and not sure what to grab with the node tree.
Edit: I don't want to use pointer-events
either because It seems like its a easy way out of the problem, and for collision detection later, it will become an issue.
Edit2: WARNING
I was using transform:translate(X,Y)
on these VALUES and they DO NOT get passed to the DOM (for offsets) so if anyone gets stuck on this, remember to add top:
and left:
properties for the dom to catch the offsets & values.