Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.
    Last edited by Philip M; 11-10-2012 at 12:58 PM.

    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.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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.

    Code:
    1000/FPS);
    Nothing appears to alter FPS, so why use it solely as a divisor for a constant?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •