...

View Full Version : Uncaught ReferenceError: FPS is not defined how i can correct this error



jitendrak
11-10-2012, 11:47 AM
Hi, I am new to the javascript and got the error Uncaught ReferenceError: FPS is not defined in my code how i can correct this .This is my code:

window.onload = function()
{
//alert("inside function");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var FPS = 50;
//alert("FPS");
var width = 450;
var height = 450;
Ball();
}

function Ball()
{
//alert("inside function");
this.x = 20;
this.y = 20;
this.radius = 10;
this.speedX = 2;
this.speedY = 2;
this.color = '#000000';
this.draw = function()
{
//alert("inside function");
context.beginPath();
context.arc(this.x, this.y, this.radius, 0, Math.PI*2, true);
context.closePath();
context.fillStyle = this.color;
context.fill();
};
}

var ball = new Ball();

setInterval(function(){

if(ball.x + ball.radius >= width || ball.x - ball.radius <= 0)
{
ball.speedX *= -1;
}
if(ball.y + ball.radius >= height || ball.y - ball.radius <= 0 )
{
ball.speedY *= -1;
}
ball.x = ball.x + ball.speedX;
ball.y = ball.y + ball.speedY;


// clear stage
context.clearRect(0, 0, width, height);
ball.draw();

},
1000/FPS);
</script>

Philip M
11-10-2012, 01:53 PM
Make FPS a global variable - declare it outside the function.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Logic Ali
11-10-2012, 05:06 PM
This code makes little sense. Ball is a constructor, so why call it without new in the onload handler?

FPS is declared in the onload handler but never referenced.



1000/FPS);

Nothing appears to alter FPS, so why use it solely as a divisor for a constant?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum