첫 안드로이드 게임을 만들고 있는데 내 게임 스레드에 문제가 있습니다. 문제는 내가 멈추고 뒤로 또는 홈 버튼을 누르면 오류가 발생한다는 것입니다. "(내 애플 리케이션 게임에서) 활동이 응답하지 않는 내 코드 :.?.홈 버튼을 눌렀을 때 안드로이드 스레드가 일시 중지/다시 응답하지 않음
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();
}
}
}
내가 잘못을하고있는 중이 야 무엇을
LogCat 출력을 제공 할 수 있습니까? –
while 루프가 있습니다 : while (mPaused) {... 한 번만 기다려주세요. – fonZ
그리고 여러 스레드에 의해 수정 될 수있는 객체에만 해당하므로 많은 동기화가 필요하다고 생각하지 않습니다. – fonZ