Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 undefined
Here is my code:

html:
XML
<!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:
C#
//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);
}


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!
Posted
Updated 3-May-15 14:53pm
v2
Comments
Sergey Alexandrovich Kryukov 3-May-15 20:38pm    
"Could not read" sounds gibberish. Exact error information, please. You could looks really bad...
—SA
UWStudents 3-May-15 20:42pm    
Ok, I'll check the error in the chrome browser again! Thank you for telling me so soon!
UWStudents 3-May-15 20:43pm    
Woops, I did not add the entire error message, my bad. Here is the full one:Uncaught TypeError: Cannot read property 'fillRect' of undefined
Sergey Alexandrovich Kryukov 3-May-15 21:28pm    
Oh, now it's clear; thank you for the clarification. Please see my answer.
Also, next time, better don't tell those line numbers. Who will count them? Better describe the error or exception in a comment and then refer to this comment in the text describing a problem.
—SA
UWStudents 3-May-15 21:34pm    
Thank you for the information! I will make sure to do that for my next question(s)! Have a good one Sergey!

1 solution

This is because Canvas.Content is undefined. There is no such property; and you know it. It should be canvasContent you calculated before as canvas.getContext("2d").

Again, your code is pretty bad. You rely on outer context (this very variable), don't use the power of JavaScript objects, don't use encapsulation, don't use closures, don't even use passing of objects in functions. You create objects in outer scope which you never should use after certain phase of code flow, don't limit scope of variables, and so on.

If you are interested, you can look at couple of my JavaScript articles (one of them is also a Canvas game) where I discuss some of the problems I mentioned above, and their solutions:
Tetris on Canvas,
JavaScript Calculator.

—SA
 
Share this answer
 
Comments
UWStudents 3-May-15 21:31pm    
Thank you Sergey! I learned this stuff off of a tutorial, but I never knew it taught me bad programming! Thank you so much for telling me now! I am a self taught programmer at the moment, so I don't have any teacher to really help out. Thank you again Sergey! Keep coding, and I will most likely go through your articles. Have a good one Sergey.
Sergey Alexandrovich Kryukov 3-May-15 21:33pm    
You are very welcome. I'll gladly help you more if you have other questions.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900