Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2012, 10:47 AM   PM User | #1
jitendrak
New to the CF scene

 
Join Date: Nov 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jitendrak is an unknown quantity at this point
Post Uncaught ReferenceError: FPS is not defined how i can correct this error

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>
jitendrak is offline   Reply With Quote
Old 11-10-2012, 12:53 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-10-2012 at 12:58 PM..
Philip M is offline   Reply With Quote
Old 11-10-2012, 04:06 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.

Quote:
Code:
1000/FPS);
Nothing appears to alter FPS, so why use it solely as a divisor for a constant?
Logic Ali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:01 PM.


Advertisement
Log in to turn off these ads.