Discord
Login
Community
DARK THEME

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() }

I didn't test much JavaScript in MicroStudio, but as far as I tested, it shouldn't be constructer, it's constructor, it will be shown in purple letters for that

var player
var objects = []

class Player {

  constructor(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 (){
    screen.drawSprite("icon",this.x,this.y)
  }
}

init = function() {
  player = new Player(0,100, 32, 32)
}

update = function(){
  player.update()
}

draw = function() {
  screen.clear()
  player.draw()
}

And I'm not sure about console.log(), I only know basic JavaScript, but I did find some projects that deal with it; I don't know if they'll be useful to you https://microstudio.dev/i/Loginus/consolelog/

Another thing I noticed is that you should try using one of these methods to define a class:

  • Player = class { ... }
  • globalThis.Player = class { ... }

Because if you use class Player { ... } in another file, sometimes it won't load and you'll get an error message saying that Player doesn't exist, since files load randomly and then use init()

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community