Richard
Richard

Reputation: 11

Uncaught ReferenceError: Cannot access 'Sprite' before initialization

I'm currently programming a web game and I'm stuck here, could someone help me? I've tried different ways but I've been stuck here for a few hours so I thought I'd write here, I'm a junior programmer and I'm using this page for the first time, if I've described something wrong I apologize.

Uncaught ReferenceError: Cannot access 'Sprite' before initialization

const backgorund = new Sprite({
  position: {
    x: 0,
    y: 0
  },
  imageSrc: './img/background.png'
})

class Sprite {
    constructor({position, velocity, imageSrc}) {
        this.position = position
        this.velocity = velocity
        this.width = 50
        this.height = 150
        this.image = new Image()
        this.image.src = imageSrc
    }
    draw() {
      c.drawImage(this.image, this.position.x, this.position.y)

    }
    update() {
        this.draw()
    }
  }

Upvotes: 0

Views: 1437

Answers (1)

punund
punund

Reputation: 4421

You need to initialize a class before use. Move your class declaration on top:

class Sprite {
  // ...
}

const backgorund = new Sprite({
   // ...
})

Upvotes: 1

Related Questions