So I am trying to get familiar with classes and methods but I just cant understand why certain thing work the way they are. I was trying to make a rectangle and just move it along axis-X whit the click on the window.
const canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
const goBtn = document.getElementById('go');
class Snake{
constructor(positionX, positionY){
this.positionX = positionX;
this.positionY = positionY;
}
draw(){
ctx.fillStyle = "#FF0000";
ctx.fillRect(this.positionX, this.positionY, 25, 25);
}
move(){
ctx.clearRect(this.positionX, this.positionY, canvas.width, canvas.height);
this.positionX +=25;
ctx.fillRect(this.positionX, this.positionY, 25, 25);
console.log("something");
}
}
let player = new Snake(250,250);
player.draw();
goBtn.addEventListener('click', player.move());
In this first cenario the player.draw() works fine but the player.move() runs automatically, and the event listener is not working.
window.addEventListener('click', player.move);
In the second scenario I removed the brackets from player.move. This time the event listener is working but only the consol.log('something'), the rest of the method is not.
move = () => {...
window.addEventListener('click', player.move());
After this I changed the funtion to an arrow function and but back the brackets to player.move() Here it works like on the first try it work one time atomatically witouth the evetn listener working.
Finally I removed the brackets once again...
move = () => {...
window.addEventListener('click', player.move);
And it worked just perfectly.
Can someone explain why these things are happening, or just send me some sites that explain this? Thanks in advance.
Here an executable version of my version 1 of my implementation:
const canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
const goBtn = document.getElementById('go');
class Snake{
constructor(positionX, positionY){
this.positionX = positionX;
this.positionY = positionY;
}
draw(){
ctx.fillStyle = "#FF0000";
ctx.fillRect(this.positionX, this.positionY, 25, 25);
}
move(){
ctx.clearRect(this.positionX, this.positionY, canvas.width, canvas.height);
this.positionX += 25;
ctx.fillRect(this.positionX, this.positionY, 25, 25);
console.log("something");
}
}
// changed player position to it is visible in snippet
let player = new Snake(50,50);
player.draw();
goBtn.addEventListener('click', player.move());
<button id="go">Go</button>
<canvas width="400" height="100" id="canvas"></canvas>