我尝试intent
使用这种方法从我的 gameView 开始一个新的:
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);
}
}
}
}