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 6963949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:51:40+00:00 2026-05-27T15:51:40+00:00

I am having trouble in creating and displaying multiple game objects on the screen

  • 0

I am having trouble in creating and displaying multiple game objects on the screen for every 3 seconds. there is no problem when there’s only one object but if I want to create multiple, the problem occurs. To explain in detail, There’s a main game loop (the one identical with the existing ones on the internet) and in that game loop,in every 3 seconds I want a new object to be created, added to the ArrayList and then update game panel and show all the objects on the screen in every 3 seconds. The code block above works but it is too fast so the screen is filled with images, I want it to be periodic. What must I do? If using a background thread in order to prevent the block of UI thread, how can I do it?

Thanks in advance.

Here’s my code block:
MAIN THREAD PART:

    while (running) {
        canvas = null;          
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                beginTime = System.currentTimeMillis();

                this.gamePanel.update();

                this.gamePanel.render(canvas);          

            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        } // end finally
    }

and Update method in my MainGamePanel class:
public void update() {

    int random = 5 + (int) (Math.random() * (200 - 5));
    droid = new Carrier(BitmapFactory.decodeResource(getResources(),
            R.drawable.image), random, 1);
    Carriers.add(Carrier);

    for (int i = 0; i < Carriers.size(); i++) {
        Carrier CarrierTemp = Carriers.get(i);
        CarrierTemp .update();
    }
}
  • 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-27T15:51:40+00:00Added an answer on May 27, 2026 at 3:51 pm

    Here’s a solution that is built on your current code:

    Put this in your thread somewhere:

    int savedtime = 0;
    long lastTime;
    

    in your update() method:

    //Calculate time since last update:
    long now = System.currentTimeMillis();
    savedtime += now - lastTime;
    lastTime = now;
    if(savedTime > 3000){//if more than three seconds have passed:
        savedTime = 0;
        int random = 5 + (int) (Math.random() * (200 - 5));
        droid = new Carrier(BitmapFactory.decodeResource(getResources(),R.drawable.image), random, 1);
        Carriers.add(Carrier);
    }
    for (int i = 0; i < Carriers.size(); i++) {
        Carrier CarrierTemp = Carriers.get(i);
        CarrierTemp .update();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble creating multiple xml requests using php's curl_multi_exec. The problem is that
I'm having trouble creating multiple pages in a PrintDocument and displaying them within a
I am creating a Stratego Game and am having trouble creating an algorithm that
I am having some serious trouble creating a WPF TreeView with an Object databinding.
I'm having trouble finding information online for creating a object in javascript and pointing
I am having trouble creating a table with MySQL. I have narrowed the problem
I'm having trouble creating a solid game engine for my OpenGL application. It's a
I'm having trouble creating a sub that can create objects of a variable type
I am new to JDOM, and am having trouble creating a document. The problem
So I'm creating a layout for my site. I'm having trouble with displaying my

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.