0

I am making my first android game and I have a problem with my game thread. The thing is that when I pause it and press back or home button, I get an error. "the activity (in my app game) is not responding. My code:

public class GameLoopThread extends Thread {
static final long FPS = 10;
private GameView view;
private boolean running = false;

private Object mPauseLock;
private boolean mPaused;

public GameLoopThread(GameView view) {
    this.view = view;
}

public void setRunning(boolean run) {
    running = run;
    mPauseLock = new Object();
    mPaused = false;

}

@Override
public void run() {
    long ticksPS = 1000 / FPS;
    long startTime;
    long sleepTime;
    while (running) {
        Canvas c = null;
        startTime = System.currentTimeMillis();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 0)
                sleep(sleepTime);
            else
                sleep(10);
        } catch (Exception e) {
        }

        synchronized (mPauseLock) {
            while (mPaused) {
                try {
                    mPauseLock.wait();
                } catch (InterruptedException e) {
                }
            }
        }

    }

}

/**
 * Call this on pause.
 */
public void onPause() {
    synchronized (mPauseLock) {
        mPaused = true;
    }
}

/**
 * Call this on resume.
 */
public void onResume() {
    synchronized (mPauseLock) {
        mPaused = false;
        mPauseLock.notifyAll();
     }
   }
  }

What am I doing wrong?

Korangar
  • 1
  • 2
  • Can you provide a LogCat output? – Zoltán Sep 17 '12 at 21:42
  • You have a while loop in there: while (mPaused) { ... You wait once, thats enough or it should be. – fonZ Sep 17 '12 at 21:48
  • And i dont think you have to you so many synchronized because thats only for objects that could be modified by several threads. – fonZ Sep 17 '12 at 21:54
  • Do you have any other way to pasue and resume a thread? I made this up from a code I found on the net – Korangar Sep 17 '12 at 22:11
  • The first problem I see is that private field "running" is not volatile. The compiler may assume that since no other thread will modify it, just in-line the code. – edharned Sep 18 '12 at 13:44
  • What do You mean by Volatile? how can I make it volatile? – Korangar Sep 18 '12 at 15:51
  • private volatile boolean running = false; private volatile boolean mPaused; Volatile tells the system that multiple threads will write to the variable. – edharned Sep 19 '12 at 13:43

2 Answers2

1

First, did you try to press the power button while application is running? If the application still running when you re-open the app after you tried that, then your thread code and surface class are fine. To solve your problem(the home button part), add this method to your appcompatactivity.

public class Game extends AppCompatActivity {
public int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(new YourSurfaceGame(this,"player1"));
}
//Add this override method to your appcompatactivity
@Override
protected void onStart() {
    super.onStart();
    //Re-declare your surface game class is the main solution here
    setContentView(new YourSurfaceGame(this,"player1"));
}

@Override
protected void onPause() {
    super.onPause();
}
}

It took a hours for me to solve the button home problem though I noticed something odd when I compared between pressing the home and power button. If you try this code and starting notice that all of your game progress is gone, then https://www.tutorialspoint.com/android/android_shared_preferences.htm is the answer.

I did the same thing for other game application and my conclusion was right. The game get paused after I press either the home or power button. Actually, you will find this problem if you are using thread as extended and it is different thing when you use timertask as extended inside your mainthread class. Though, I never try that before.

Marfin. F
  • 434
  • 4
  • 16
0

Most likely your applications is attempting to start up a stopped thread when it resumes. This is causing an error and causing your application to crash.

Take a look at your Logcat logs to check.

Clark
  • 1,357
  • 1
  • 7
  • 18
  • it something about keyDispatchingTimedOut 09-17 21:50:30.716: – Korangar Sep 17 '12 at 21:58
  • It pause and resume well, but if I pause and after I pressed de back or the home button it when the error appear. – Korangar Sep 17 '12 at 22:20
  • I had a similar problem before, it had to do with the View initializing the thread, then when it went to home it was destroyed but the view kept trying to start it. It might not be the problem you're having. Right now I'm just guessing :/ (I would've posted as a comment, but not 50 rep yet) – Clark Sep 17 '12 at 22:49
  • Do you have any game thread pause and resume sample code that I can study? – Korangar Sep 17 '12 at 22:58