我尝试使用此方法从我的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秒,然后结束屏幕闪烁一秒,然后游戏视图闪烁一秒,然后终止屏幕出现真实和按钮工作。
当我从另一个活动开始一个活动时,就像我在菜单中按“开始游戏”一样,我没有遇到这个问题。我真的不明白这里发生了什么让它像这样滞后。
在下面添加我的帖子:
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);
}
}
}
}
答案 0 :(得分:1)
我必须看看你的线程在做什么才能确定,但我很确定这样的事情正在发生:
你调用setRunning(false),它在线程类中设置了一些标志。
你开始新的意图,但线程还没有停止,因为它没有到达检查运行标志的部分
线程(尚未暂停)开始对gui进行更改,这会混淆认为视图应该处于非活动状态的android UI
线程到达等待setRunning(true)的程度,一切都很开心。
如果这是真的,你可以等到线程在setRunning(true)上被阻塞,然后创建新意图来修复。