我试图使用以下方法从我的gameView启动一个新的intent
:
if(happy.getHP() <= 0){ //if my main-character dies
thread.setRunning(false);
Context context = getContext();
Intent intent = new Intent("nielsen.happy.activities.ENDSCREEN");
context.startActivity(intent);
}
如果我不使用
thread.setRunning(false);
进行操作,则会显示endScreen,但其按钮将无法工作,但是如果我停止了mainThread,它们将可以工作。无论如何,这段代码的问题是,当我的角色死亡时,它冻结了3-4秒,然后端屏幕闪烁了几秒钟,然后gameView闪烁了几秒钟,然后端屏幕显示出来了,按钮才起作用。
当我从另一个活动开始一个活动时,例如当我按菜单中的“开始游戏”时,我不会遇到此问题。我真的不明白在这里发生的事情使它像这样滞后。
在下面添加我的线程:
public void run() {
Canvas canvas;
Log.d(TAG, "Starting game loop");
// initialise timing elements for stat gathering
initTimingElements();
long beginTime; // the time when the cycle begun
float time1 = System.currentTimeMillis();
float time2;
//long timeDiff; // the time it took for the cycle to execute
int sleepTime; // ms to sleep (<0 if we're behind)
int framesSkipped; // number of frames being skipped
sleepTime = 0;
while (running) {
canvas = null;
// try locking the canvas for exclusive pixel editing
// in the surface
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
beginTime = System.currentTimeMillis();
framesSkipped = 0; // resetting the frames skipped
// update game state
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.render(canvas);
// calculate how long did the cycle take
timeDiff = System.currentTimeMillis() - beginTime;
// calculate sleep time
sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0) {
// if sleepTime > 0 we're OK
try {
// send the thread to sleep for a short period
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up
this.gamePanel.update(); // update without rendering
sleepTime += FRAME_PERIOD; // add frame period to check if in next frame
framesSkipped++;
}
// for statistics
framesSkippedPerStatCycle += framesSkipped;
// calling the routine to store the gathered statistics
storeStats();
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
} // end finally
}
}
在下面添加整个方法:
private void checkCollision(Canvas canvas) {
Rect h1 = happy.getBounds();
for (int i = 0; i < enemies.size(); i++) {
Rect e1 = enemies.get(i).getBounds();
if (h1.intersect(e1)){
if(enemies.get(i).getX() < controls.pointerPosition.x){
enemies.get(i).setX(-20);
}else if (enemies.get(i).getX() > controls.pointerPosition.x){
enemies.get(i).setX(20);
}else if(enemies.get(i).getY() < controls.pointerPosition.y){
enemies.get(i).setY(-20);
}else if(enemies.get(i).getY() > controls.pointerPosition.y){
enemies.get(i).setY(20);
}
if(enemies.get(i).getCooldown() <= 0){
happy.damageHP(10);
score.incScore(-10);
enemies.get(i).setCooldown();
}
}
if(happy.getHP() <= 0){
thread.setRunning(false);
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Context context = getContext();
Intent intent = new Intent("nielsen.happy.activities.ENDSCREEN");
context.startActivity(intent);
//end-screen !!!!!!!
}
if(enemies.get(i).getHP() <= 0){
enemies.get(i).death(canvas, enemies);
score.incScore(5);
break;
}
for (int j = 0; j < bullets.size(); j++) {
Rect b1 = bullets.get(j).getBounds();
if (b1.intersect(e1)) {
enemies.get(i).damageHP(5);
bullets.remove(j);
}
}
}
}
最佳答案
我必须确保确定您的线程在做什么,但是我很确定正在发生以下情况:
调用setRunning(false)可以在线程类中设置一些标志。
您启动了新的意图,但是线程尚未停止,因为它尚未到达检查运行标志的部分
线程(尚未暂停)开始对gui进行更改,这使android UI感到困惑,认为该视图应处于非活动状态
线程到达等待setRunning(true)的地步,一切再次变得快乐。
如果是这样,则可以通过等待直到线程在setRunning(true)上被阻塞,然后创建新的Intent来进行修复。