I'm trying to make a simple Pong game in Javascript. I have a Pong class, and I'd like to create a method to move the player rectangles based on how the mouse moves:
class Player
{
constructor()
{
// do stuff
}
}
class Pong
{
constructor(canvas)
{
//do stuff
this.player1 = new Player(true); // Create an instance of another class
}
handleMouseMove(event)
{
var y = event.clientY;
// this.player1 is undefined!!
console.log("this.player1: "+this.player1);
this.player1.pos.y = y;
}
function main()
{
// Initialize canvas and context
canvas = document.getElementById('mycanvas');
const pong = new Pong(canvas);
canvas.addEventListener('mousemove', pong.handleMouseMove);
}
Whenever I start moving the mouse, it tells me that player1 is undefined. How can I set the class method as the event listener and have it know about the class' members?
Playerclass?