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

  • Home
  • SEARCH
  • 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 8423845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:50:15+00:00 2026-06-10T03:50:15+00:00

so for the game I’m creating I realized I mist have done something wrong

  • 0

so for the game I’m creating I realized I mist have done something wrong with my gameLoop because the first time I play through my game it runs great, but the second time or anything after that, it slows down by about half. Even if I minimize the game (Because that stops the gameLoop, and then bringing it up again starts it back up) here is the gameLoop code:

public void gameLoop(){

        new Thread(){
            public void run() {
                while(gameRunning){
                    try {
                        Thread.sleep(1000/60);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (tutorial){
                        updateBullet();
                        updatePlayer();
                        repaint();
                    } else {
                        updateEnemies();
                        updateBullet();
                        createEnemies();
                        updateParticles();
                        updatePlayer();
                        repaint();
                    }

                }
                repaint();
            }
        }.start();
}

I start it for the first time in the init() just with

gameLoop();

And then I also have:

public void stop(){
    bg.stop();
    gameRunning = false;
}
public void start(){
    bg.start();
    gameRunning = true;
    gameLoop();

}

And finally the playerUpdate also stops the loop with (The Thread inside of player is to allow for some effects to finish after the player dies):

 public void updatePlayer(){
    if (player.isMovingLeft){
        player.x-=3;
    }
    if (player.isMovingRight){
        player.x+=3;
    }
    for (int j=0; j < enemies.size(); j++){
        if (player.isAlive){
            if (enemies.get(j).x+19 > player.x && enemies.get(j).x < player.x+40 && enemies.get(j).y > player.y  && enemies.get(j).y < player.y+40) {
                enemies.remove(j);
                j--;
                explode.setFramePosition(0);
                explode.start();
                for (int k = 0; k <21; k++){
                    addParticle(player.x+20,player.y+20,2);
                }
                new Thread(){
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            gameRunning = false;
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }.start();

                player.isAlive = false;
                break;

            }
        }
    }
}

And then it is restarted with in the keyPressed event:

if (!gameRunning){
        if (arg0.getKeyCode() == KeyEvent.VK_ENTER){
            enemies.clear();
            bullets.clear();
            particles.clear();
            score = 0;
            player.x = 200;
            player.isMovingLeft = false;
            player.isMovingRight = false;
            player.isAlive = (true);

            gameRunning = true;
            gameLoop();
        }
    }

So why is it that whenever the loop is stopped and started again, it runs at half the speed? Thanks!

  • 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-06-10T03:50:16+00:00Added an answer on June 10, 2026 at 3:50 am

    It looks like you’re starting a new thread for each gameloop; this means each time the gameloop runs, you’ve got another thread for the Java VM to handle. This is extremely inefficient, as eventually you’ll have 1000 threads running, causing HUGE lag. Is there any way you could rewrite your code without threading there?

    Also, what is this supposed to do?

                    try {
                        Thread.sleep(1000/60);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    

    Why would you do 1000/60? A: Why not just use 16? B: It seems like you meant something else here, not sure what though

    Also, what is the bg variable your reference in your start() and stop() methods?

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

Sidebar

Related Questions

I have a game where each player (A total of 5 players play the
My game will play 10 rounds, so each time you advance to a new
My game units have an angle variable, and a targetAngle variable. These variables range
Permutation Game (30 Points) Alice and Bob play the following game: 1) They choose
So I have this game database, where I have several users with fields, id,
//Handle game logic mcPlayer.update(); //create question mcMathQu.update(); the first update function of external as
Jump Game: Given an array, start from the first element and reach the last
I have a simple game that is in progress. As of right now all
I have a game server than can take requests from a user. A user
I have a game which displays an array of colored blocks. The user can

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.