0

I am making a game in Javascript and to challenge myself, I would make the game in ES6 using OOP. I am used to program in Java.

The only problem is that there is no field? is there? I can not get my ctx find the ctx i made in the constructor.

so I use my constructor to set some variables, but i can not access them from my Update Method, I have tried a lot. Can someone please tell me how to get the variables correctly to be private in the class but accessible for everything in the class.

constructor(){
    this.x = 400;
    this.y = 250;
    this.enemySpeed = 2;
    this.spawnRate = 10;
    this.enemy = [];

    this.canvas = document.getElementById("myCanvas");
    this.ctx = this.canvas.getContext("2d");


    this.player = new Player(this.x, this.y);

    this.generateEnemies();

    setInterval(this.Update, 1000/60);

}



Update(){

    this.ctx.clearRect(0, 0, 800, 500);
    this.player.moveCheck();
    this.player.playerMove();
    for(let i = 0; i < this.spawnRate; i ++){
        this.enemy[i].enemyMove(this.enemySpeed);
    }
}
Daan Smits
  • 11
  • 4

0 Answers0