Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Rep Power

    Default Really quick question

    Ok, I am following the book "Killer Game Programming" by O'Reilly. This code is from his book, I just have a few questions on what it means, because i get errors when i try and compile. The errors are that the variable period doesn't exist, which it doesn't. Here is the code:

    Java Code:
    public void run( )
        /* Repeatedly: update, render, sleep so loop takes close
           to period ms */
          long beforeTime, timeDiff, sleepTime;
          beforeTime = System.currentTimeMillis( );
          running = true;
          while(running) {
            gameUpdate( );
            gameRender( );
            paintScreen( );
            timeDiff = System.currentTimeMillis( ) - beforeTime;
            sleepTime = [COLOR="Blue"]period[/COLOR] - timeDiff;   // time left in this loop
            if (sleepTime <= 0)  // update/render took longer than period
              sleepTime = 5;    // sleep a bit anyway
            try {
              Thread.sleep(sleepTime);  // in ms
            catch(InterruptedException ex){}
            beforeTime = System.currentTimeMillis( );
        } // end of run( )
    "A popular measure of how fast an animation progresses is frames per second (FPS). For GamePanel, a frame corresponds to a single pass through the update-render-sleep loop inside run( ). Therefore, the desired 100 FPS imply that each iteration of the loop should take 1000/100 == 10 ms. This iteration time is stored in the period variable in GamePanel."

    Above is the paragraph in the book describing period. So is period simply a variable that you can put whatever time you want in it, or does it need to be something calculated?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Rep Power


    period is a variable indeed, and it's the number of milliseconds you expect one frame to take. So for example if you want 25 FPS, your period should be (1000/25)=40. You don't need to calculate it in code.

  3. #3
    Join Date
    Apr 2010
    Rep Power


    ok that's what i was thinking, thanks for helping out so fast =]

Similar Threads

  1. Quick question
    By sAntA199 in forum New To Java
    Replies: 2
    Last Post: 12-09-2009, 03:01 AM
  2. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 04:13 AM
  3. One last quick question
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 08:53 AM
  4. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  5. Quick Question
    By Spenc in forum New To Java
    Replies: 3
    Last Post: 09-22-2008, 02:26 PM

Posting Permissions

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