1

我尝试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);
            }
        }
    }
}
4

1 回答 1

1

我必须看看你的线程在做什么才能确定,但​​我很确定这样的事情正在发生:

  • 您调用 setRunning(false) 在线程类中设置一些标志。

  • 您启动了新意图,但线程尚未停止,因为它尚未到达检查运行标志的部分

  • 线程(尚未暂停)开始对 gui 进行更改,这混淆了认为视图应该处于非活动状态的 android UI

  • 线程到了等待 setRunning(true) 的地步,一切又恢复了快乐。

如果这是真的,您可以通过等待线程在 setRunning(true) 上被阻塞,然后创建新意图来修复。

于 2012-09-05T00:18:11.827 回答