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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:44:45+00:00 2026-05-27T07:44:45+00:00

I am trying to add pause functionality to a game. Unfortunately the method I’m

  • 0

I am trying to add pause functionality to a game. Unfortunately the method I’m currently using causes the program to take no more inputs so I am unable to resume the game.

Here is what I’m currently doing:

a pause button calls:

public void pause(){
    if(data.paused == true){
        data.paused = false;
    }
    else{
        data.paused=true;
        while(true){
            if(data.paused == true){
                try{
                    Thread.currentThread().sleep(100);
                }
                catch(InterruptedException ie){
                    System.out.println(ie);
                }
            }
            else
                break;
        }
        data.paused=false;
    }
  return;

I then have a TimerTask that I have to deal with:

class aTask extends java.util.TimerTask {
    protected Data data;
    public aTask( Data d ) { data = d; }
    public void run() { 
        try{ 
            if(data.paused){
                try{
                    Thread.currentThread().sleep(100);
                }
                catch(InterruptedException ie){
                    System.out.println(ie);
                }
            }
            else
                data.tick(); 
        } 
        catch( ATCGameOverException e ){ 
            data.gameOver( e.getMessage() );
        }
    }
};

where tick causes an action in the game every second.

I would think this would allow the game to pause when the button is pressed and then unpause when the button is pressed again, but I would be wrong. What am I doing wrong?

  • 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-27T07:44:46+00:00Added an answer on May 27, 2026 at 7:44 am

    Simply try this :

    public void pause(){
      data.paused = !data.paused;
    }
    

    It looks like a threading problem. If you use Swing and your pause method is tied to an action listener, than the behaviour is normal: the pause function never returns, Swing main thread never has control again, and cannot manage further actions, including unpausing… Look at this link

    My solution simply inverse the state of the pause variable, and gives control back to Swing. It’s the game main loop that will tick the data or not according to the state of this variable. No thread is blocked, main loop continues to loop.

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

Sidebar

Related Questions

I am currently trying to add some parsing methods to a controller method in
I just started a game using cocos2d-iphone and I am trying to add a
I'm trying add a tab to my web page that looks like this: Using
I am currently trying to add 301 redirect to my routes in MVC to
I'm using VB 2008 and I'm trying to add a xmlns=mynamespace attribute to an
I am trying to add my own functionality to a jQuery plugin and to
Since GLSL doesn't have an include-file option I'm trying to add this by using
I trying to extend ConuntDownTimer to add the methods pause and resume in this
I am trying to add calendar event using the below code in my android
Trying to add an onclick handler to my tabs, and can't seem to get

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.