Skip to content
Snippets Groups Projects
Commit 3d2390cc authored by Sofiane Lasoa's avatar Sofiane Lasoa :grin:
Browse files

Fix mouse bug

parent d99c9dff
No related branches found
No related tags found
No related merge requests found
...@@ -65,7 +65,6 @@ var Player = /*#__PURE__*/function () { ...@@ -65,7 +65,6 @@ var Player = /*#__PURE__*/function () {
if (mouseX > ctx.canvas.width / 2 - this.radius && mouseX < ctx.canvas.width / 2 + this.radius && mouseY > ctx.canvas.height / 2 - this.radius && mouseY < ctx.canvas.height / 2 + this.radius) { if (mouseX > ctx.canvas.width / 2 - this.radius && mouseX < ctx.canvas.width / 2 + this.radius && mouseY > ctx.canvas.height / 2 - this.radius && mouseY < ctx.canvas.height / 2 + this.radius) {
return; return;
} }
console.log(mouseX, ctx.canvas.width / 2);
this.x += this.direction.x * this.speed; this.x += this.direction.x * this.speed;
this.y += this.direction.y * this.speed; this.y += this.direction.y * this.speed;
if (this.x < this.radius) this.x = this.radius; if (this.x < this.radius) this.x = this.radius;
...@@ -79,9 +78,9 @@ var Player = /*#__PURE__*/function () { ...@@ -79,9 +78,9 @@ var Player = /*#__PURE__*/function () {
} }
}, { }, {
key: "moveTo", key: "moveTo",
value: function moveTo(x, y) { value: function moveTo(x, y, canvas) {
var directionX = x - this.x; var directionX = x - canvas.width / 2;
var directionY = y - this.y; var directionY = y - canvas.height / 2;
var distance = Math.sqrt(Math.pow(directionX, 2) + Math.pow(directionY, 2)); var distance = Math.sqrt(Math.pow(directionX, 2) + Math.pow(directionY, 2));
this.direction.x = directionX / distance; this.direction.x = directionX / distance;
this.direction.y = directionY / distance; this.direction.y = directionY / distance;
...@@ -295,7 +294,7 @@ canvas.addEventListener("mousemove", function (event) { ...@@ -295,7 +294,7 @@ canvas.addEventListener("mousemove", function (event) {
event.preventDefault(); event.preventDefault();
mouseX = event.clientX; mouseX = event.clientX;
mouseY = event.clientY; mouseY = event.clientY;
player.moveTo(mouseX, mouseY); player.moveTo(mouseX, mouseY, canvas);
}); });
function update() { function update() {
ctx.save(); ctx.save();
...@@ -318,7 +317,6 @@ function update() { ...@@ -318,7 +317,6 @@ function update() {
player.draw(ctx, image); player.draw(ctx, image);
player.move(ctx, mouseX, mouseY); player.move(ctx, mouseX, mouseY);
ctx.restore(); ctx.restore();
// Déplacer le contexte pour centrer la caméra sur le joueur // Déplacer le contexte pour centrer la caméra sur le joueur
//ctx.translate(-cameraX, -cameraY); //ctx.translate(-cameraX, -cameraY);
requestAnimationFrame(update); requestAnimationFrame(update);
......
This diff is collapsed.
...@@ -53,7 +53,6 @@ class Player { ...@@ -53,7 +53,6 @@ class Player {
) { ) {
return; return;
} }
console.log(mouseX, ctx.canvas.width / 2);
this.x += this.direction.x * this.speed; this.x += this.direction.x * this.speed;
this.y += this.direction.y * this.speed; this.y += this.direction.y * this.speed;
...@@ -66,9 +65,9 @@ class Player { ...@@ -66,9 +65,9 @@ class Player {
this.y = ctx.height - this.radius; this.y = ctx.height - this.radius;
} }
} }
moveTo(x, y) { moveTo(x, y, canvas) {
const directionX = x - this.x; const directionX = x - canvas.width /2;
const directionY = y - this.y; const directionY = y - canvas.height /2;
const distance = Math.sqrt(directionX ** 2 + directionY ** 2); const distance = Math.sqrt(directionX ** 2 + directionY ** 2);
this.direction.x = directionX / distance; this.direction.x = directionX / distance;
......
...@@ -87,7 +87,7 @@ canvas.addEventListener("mousemove", (event) => { ...@@ -87,7 +87,7 @@ canvas.addEventListener("mousemove", (event) => {
mouseX = event.clientX; mouseX = event.clientX;
mouseY = event.clientY; mouseY = event.clientY;
player.moveTo(mouseX, mouseY); player.moveTo(mouseX, mouseY, canvas);
}); });
function update() { function update() {
...@@ -111,7 +111,6 @@ function update() { ...@@ -111,7 +111,6 @@ function update() {
player.draw(ctx, image); player.draw(ctx, image);
player.move(ctx, mouseX, mouseY); player.move(ctx, mouseX, mouseY);
ctx.restore(); ctx.restore();
// Déplacer le contexte pour centrer la caméra sur le joueur // Déplacer le contexte pour centrer la caméra sur le joueur
//ctx.translate(-cameraX, -cameraY); //ctx.translate(-cameraX, -cameraY);
requestAnimationFrame(update); requestAnimationFrame(update);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment