我在下面的代码中不断收到错误“ canvasContext.fillRect”

时间:2019-12-07 16:53:51

标签: javascript canvas

每次我运行应用程序并打开控制台时,此错误“ pong.js:25 Uncaught TypeError:canvasContext.fillRect不是drawEverything(pong.js:25)的函数”,总是每秒出现一次以为是因为我没有正确拼写“ fillRect”,但我不知道为什么会这样,有什么主意吗?谢谢

var canvas
var canvasContext;
var ballX = 50;



window.onload = function() {
    canvas = document.getElementById('gameCanvas');
    //get context to draw on 
    canvasContext = canvas.getContext('2d');

   setInterval(drawEverything, 1000);




}


function drawEverything() {
    ballX = ballX + 20;

    console.log(ballX);
    canvasContext.fillStyle = 'black';
    canvasContext.fillRect(0,0,canvas.width,canvas.height);

    canvasContext.fillRect = 'white';
    canvasContext.fillRect(225,210,200,200);
    canvasContext.fillStyle = 'red';
    canvasContext.fillRect(ballX, 200, 50, 25);

}

1 个答案:

答案 0 :(得分:0)

您正在使用字符串覆盖您的fillRect函数

canvasContext.fillRect = 'white';