Classes not working in JavaScript
I've been looking around for a while to see if anyone else has had this issue, but I'm running out of ideas and I can't find anyone else having the issues I am.
Simply put, I'm trying to use Classes in my JavaScript code. I'm utterly lost, since when I try to make an instance of my player, the properties become undefined. This makes situations like implementing movement on a per-object basis impossible, since something like "this.x" becomes undefined. Can't call console.log() in the constructor either to debug there. I have no idea why that's happening. I know the object is created and the functions are being called (as the draw function is being called, the sprite is appearing, and console.log() works when I put it in the class' update() function).
What I do know is that when I set the properties manually in init(), they become initialized and set (i.e. player.x = 15 makes the player's x coordinate become 15, whereas before it was undefined. It can then take input from the handleMovement() function). Makes me think the constructor just isn't being called.
Below is my code, any help is appreciated:
var player var objects = []
class Player{
constructer(x,y,w,l){ this.x = x this.y = y this.w = w this.l = l }
handleMovement () { if (keyboard.RIGHT){ if (keyboard.SHIFT){ this.x+=1 } this.x +=2 } if (keyboard.LEFT){ if (keyboard.SHIFT){ this.x-=1 } this.x -=2 }
if (keyboard.UP){
if (keyboard.SHIFT){
this.y+=1
}
this.y +=2
}
if (keyboard.DOWN){
if (keyboard.SHIFT){
this.y-=1
}
this.y -=2
}
}
update (){ this.handleMovement() }
draw (){ // using test values to just display for now screen.drawSprite( "kenneysprites/monochromerpg/tiles_green_16x162", -150, 15, 16, 16 ) } }
init = function() { player = new Player(0,100, 32, 32) }
update = function(){ player.update() }
draw = function() { screen.clear() player.draw() }
