Discord
Login
Community
DARK THEME

Cannot Read Properties of Undefined Variable

I don't even know I've seen some weird errors but this one is just persistent.

let player;

init = function() {
  player = {
    x: 0,
    y: 0,
    zoom: 2
  }
}

update = function() {
  updateEm();
}

draw = function() {
  screen.clear();
  resetHighlighted();
  drawEm(player);
  drawHexagon(80+player.x,80+player.y,120);
}

I'm getting an error on line 19 ( drawHexagon(80+player.x,80+player.y,120); ).

Cannot read properties of undefined (reading 'x'), in file "main" at line 19

Whenever I update my code, the error changes to a line in any file seemingly randomly; some lines don't even mention an x variable. I really just can't understand why I'm getting this error, so if anyone has any insight, that'd be greatly appreciated! If you need more of my code I can also try to provide that.

I'm suspecting you're initalizing your player wrong. What happens if you pull the player initializer out of your init function, make it Player = function () { [code] } and call it in your init with player = new Player(); ?

Post a reply

Progress

Status

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