T
T
Triborg-3332019-08-27 16:14:04
JavaScript
Triborg-333, 2019-08-27 16:14:04

How to fix the error of undefined 'x'?

I want to write a game, through prototype, but in the console there is an error
5d652c88ed261056690613.png
Code:

;(function(){

let canvas, ctx, width, height;

let Game = function(){
    
    //Подключаюсь к canvas.
  canvas = document.getElementById('knight');

  ctx = canvas.getContext('2d');

  let gameSize = {

  };

  this.loadGame();

  this.knight = new knight();

}

const bg = new Image();

bg.src = "img/bg.png";

Game.prototype.loadGame = function(){
  this.knight();
  this.draw();

  requestAnimationFrame(()=>{
    this.loadGame();
  });
}

function knight(){
  this.size = {x : 150, y : 150};
  this.position = {x : 150, y : 150};
}


Game.prototype.knight = function(){

}

Game.prototype.draw = function(){
  ctx.fillRect(150, 150, this.size.x, this.size.x)
}

window.onload = function(){
  new Game("ctx");
}

})();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Afanasy Zakharov, 2019-08-27
@afanasiyz

You have something scary here :(
At a minimum, please adjust the indents.
1. You call the loadGame function, it is taken from Game.prototype.
2. loadGame calls the this.knight function - it is also selected from Game.prototype (empty function)
3. The draw function is called, this.size.x is used - and this.size - does not exist, this field is not filled in. The
question was how to fix it - I have no idea what you want, maybe in the Game.prototype.knight declaration you need to drag the implementation of knight (normal function).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question