I am having difficulty of getting the mouse coordinates and the hero's coordinates.
When I click on the bottom right corner of the canvas I get a result of x = 641, y = 386 for the mouse click. The enemy coordinate system is 100% accurate.
It seems that the enemy coordinate system is different than the mouse coordinate system. I want them to be on a single coordinate system. Thanks for your help!
This is the initialization of canvas:
<canvas id="canvas" width = "1664" height = "1000" style = "border:1px solid gray; width: 640px; height 480px;"> </canvas>
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
var mousePos = getMousePos(Context.canvas,e);
//Use mousePos.x or mousePos.y to get the coordinates of the mouse click
var mx, my;
if(e.offsetX) {
mx = e.offsetX;
my = e.offsetY;
}
else if(e.layerX) {
mx = e.layerX;
my = e.layerY;
}
Gun.shoot()
for (var i = EnemyManager.enemies.length - 1; i >= 0; i--) {
var enemy = EnemyManager.enemies[i]
console.log("Enemy: " + enemy.x + " " + enemy.y)
console.log("Mouse: " + mousePos.x + " " + mousePos.y)
if ((enemy.x < mx) && (enemy.y < my) && ( (enemy.x + enemy.width) > mx ) && (enemy.y + enemy.height > my)) {
alert("Target HIT")
}
};
})*