Call tile.draw_tile doesn’t display anything

Viewed 17

I am trying to create a small game to learn javascript.Everything works fine except my Rect class.As a matter of fact key_click works fine when it executes some console.log, but doesn’t display anything with tile.draw_tile() (nor gives any error).Could you help me?

var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

let  Rect = class {
    constructor(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
       
    }
    draw_tile() {
        c.fillStyle = "red";
        c.fillRect(this.x, this.y, this.width, this.height);
    }
}

var mouse = {
    x: undefined,
    y: undefined,
    key_move: window.addEventListener('mousemove',(event)=>{
        mouse.x = event.clientX;
        mouse.y= event.clientY;
    }),
    key_click:window.addEventListener('click',(event)=>{
        tile.draw_tile();
    })
}

let tile = new Rect(mouse.x, mouse.y, 30, 30);
main = function(){
    
    mouse.key_move;
    mouse.key_click;
    window.requestAnimationFrame(main);
}
window.requestAnimationFrame(main);


0 Answers
Related