Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:50:07+00:00 2026-05-16T15:50:07+00:00

Now I have (1: the UI-loop, there my SurfaceView is placed), (2: a second

  • 0

Now I have (1: the UI-loop, there my SurfaceView is placed), (2: a second thread, there the draw function from the UI-loop is called AND the update calculations from my Engine) and (3: the engine, there all the calculations stuff are).

Now I wonder how the best and smoothest way to do the SurfaceView independent from the actual frame rate. How shall I do so it will compensate if there is a low frame rate?

I think my current solution isn’t enough, see code below.

Thread-class

//Constructor stuff....

int static delay = 0; /* My delay in milliseconds, depends most on
which sort of game I'm trying to make */
@Override
public void run() {
 while (state==RUNNING) {
       long beforeTime = System.nanoTime();
       engine.updateSprites(); //Update calculations from my engine

 //Rita
 Canvas c = null;
 try {     

       c = surfaceHolder.lockCanvas(null);
       synchronized (surfaceHolder) {
           view.myDraw(c);    //My draw function
     }
 } finally {
     if (c != null) {
         surfaceHolder.unlockCanvasAndPost(c);
     }
 }

 sleepTime = delay-((System.nanoTime()-beforeTime)/1000000); /* 1000000 because 
 of the nanoTime gives us the current timestamp in nanoseconds */

        try {
            if(sleepTime>0){
            Thread.sleep(sleepTime);          

            }
        } catch (InterruptedException ex) { 
        }

 }

Is this code enough to ensure the independent from low frame rate? If not, where is my way to go?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:50:08+00:00Added an answer on May 16, 2026 at 3:50 pm

    http://dewitters.koonsolo.com/gameloop.html

    This is one of the best short lessons I know. It shows 3 different ways to do what you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I right now have a function the drags an element from a
Right now I have a foreach loop that grabs the first link with an
With help from you guys I now have a script that works like a
I have a loop which iterates for 97 times and there are two arrrays
i have a loop in a macro i'm writing of the following structure: there
I have a loop in Jquery: var fadeTime=500; css_id=0; $.each(my_array, function (i, d_U) {
This is driving me insane now. I have a loop that creates buttons. Each
right now I have min, max value from table1 that defines the range of
I now have a running Java program which only lacks of the final step,that
I now have a file uploader that goes on like this This is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.