本文介绍了Html5画布游戏(pong)问题:未捕获的TypeError:无法读取未定义的属性'fillRect'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在编写一个短乒乓游戏(基本上是ping pong),我在谷歌Chrome中遇到错误它说,Uncaught TypeError:无法读取未定义的属性'fillRect' 这是我的代码: html:I was in the middle of programming a short pong game(basically ping pong), and I got an error in google chrome it said, Uncaught TypeError: Cannot read property 'fillRect' of undefinedHere is my code:html:<!DOCTYPE html><html><head> <title>Pong</title></head><body><canvas id="gameCanvas" width="800" height="600"></canvas><script type="text/javascript" src="Pongjs1.js"></script></body></html> javascript: javascript://Pong Game//================var canvas, canvasContext;var ballX = 50;var ballspeedX = 10;//================ window.onload = function() { canvas = document.getElementById('gameCanvas'); canvasContext = canvas.getContext("2d"); var fps = 30; setInterval(function() { moveEverything(); drawEverything(); }, 1000/fps); }//================//Movement,physics,etc//================ function moveEverything() { ballX = ballX + ballspeedX; if(ballX >= canvas.width) { ballspeedX = -ballspeedX; } if(ballX <= 0) { ballspeedX= -ballspeedX; } }//================ function drawEverything() { colourRect(0,0,canvas.width,canvas.height,"black"); colourRect(0,210,10,100,"white"); colourRect(ballX,50,10,10,"red");//horizantal,vertical,width,height(IN ORDER) }//================function colourRect(leftX,TopY,width,height, drawColor) { canvasContext.fillStyle = drawColor; canvas.Context.fillRect(leftX,topY,width,height);} 它说错误是在我的js文件中的第28和46行。所以函数colourRect 和函数moveEverything 我不知道我哪里出错了:/请查看我的代码以查看错误的位置制作。谢谢您的时间!It says the error is on line 28 and 46 in my js file. So the function colourRect and function moveEverything I have no clue where I went wrong :/ Please check out my code to see where the error is made. Thank you for your time!推荐答案 这篇关于Html5画布游戏(pong)问题:未捕获的TypeError:无法读取未定义的属性'fillRect'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 17:58