0

在我的抽奖中,它就像一个游戏。一个僵尸来了,你必须点击他,所以它会回到开始,而不是到你家。但是我想在我的游戏中添加一个暂停,所以我将把它全部包装在一个while循环中......好吧,当我在应用程序打开时尝试这个时,它只是以黑屏打开。这是我的一些代码。

@Override
protected void onDraw(Canvas canvas) 
{
    super.onDraw(canvas); 
    try {
        Thread.sleep(speed);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    text.setTextSize(50);
    white.setColor(Color.WHITE);
    NightGreen.setColor(Color.parseColor("#003300"));
    DayGreen.setColor(Color.parseColor("#339900"));
    clear.setARGB(0, 0, 0, 0);
    grass.set(0, canvas.getHeight()/2, canvas.getWidth(), canvas.getHeight());
    AndroidDudeHitbox.set((int) AndroidX, (int) AndroidY, (int) AndroidX + AndroidDude.getWidth(),(int)  AndroidY + AndroidDude.getHeight());
    DoorHitbox.set(canvas.getWidth()/2 + 360, canvas.getHeight()/2 - house.getHeight()/2, canvas.getWidth()/2 + 300 + house.getWidth() - 10, canvas.getHeight()/2 - house.getHeight() + house.getHeight());
    ZombieHitbox.set((int) ZombieX, (int) ZombieY, (int) ZombieX + Zombie.getWidth(), (int) ZombieY + Zombie.getHeight());
    PlayPauseHitbox.set(0, canvas.getHeight() - pause.getHeight(), pause.getWidth(), 25);

    while (PausePlay)
    {


        switch ((int) possition)
        {
        case 0:
            canvas.drawColor(Color.parseColor("#66FFFF"));
            canvas.drawBitmap(sun, (int) SunX, (int) SunY, null);
            canvas.drawRect(grass, DayGreen);
            canvas.drawBitmap(house, canvas.getWidth()/2 + 300, canvas.getHeight()/2 - house.getHeight(), null);
            canvas.drawBitmap(AndroidDude, (int) AndroidX - (AndroidDude.getWidth()/2), (int) AndroidY - (AndroidDude.getHeight()/2), null);
            text.setColor(Color.BLACK);
            if (counter < 300) canvas.drawText("QUICK! Get in your hous before Nightfall!", 160, canvas.getHeight()/2 + 100, text);
            text.setColor(Color.RED);

            //Log.d("Counter", "C: Counted " + counter);

            if (counter < 110) {SunY --; counter++;}
            if (counter >= 110 && counter < 240) {SunY --; SunX ++; counter++;} 
            if (counter >= 240 && counter < 360) {SunY -= 0.5; SunX ++; counter++;}
            if (counter >= 360 && counter < 662) {SunY -= 0.333; SunX ++; counter++;}
            if (counter >= 662 && counter < 1004) {SunY += 0.333; SunX++; counter++;}
            if (counter >= 1004 && counter < 1104) {SunY += 0.5; SunX++; counter++;}
            if (counter >= 1104 && counter < 1224) {SunY ++; SunX++; counter++;}
            if (counter >= 1224 && counter < 1345) {SunY ++; counter++;}
            if (counter >= 1345)
            {
                canvas.drawColor(Color.parseColor("#00001A"));
                canvas.drawRect(grass, NightGreen);
                canvas.drawBitmap(house, canvas.getWidth()/2 + 300, canvas.getHeight()/2 - house.getHeight(), null); 
                canvas.drawBitmap(moon, canvas.getWidth() - 300, 10, null);
                canvas.drawText("You didn't make it before night fall and you were killed", 25, canvas.getHeight()/2  + 100, text);
                canvas.drawText("by the fears of night!", canvas.getWidth()/2 - 200, canvas.getHeight()/2 + 150, text);
            }


            break;

        case 1: 
            canvas.drawColor(Color.parseColor("#00001A"));
            canvas.drawBitmap(moon, (int)MoonX, (int)MoonY, null);
            canvas.drawRect(grass, NightGreen);
            canvas.drawBitmap(houseDude, canvas.getWidth()/2 + 300, canvas.getHeight()/2 - house.getHeight(), null); 
            canvas.drawBitmap(pause, 0, canvas.getHeight() - pause.getHeight() - 25, null);

            canvas.drawBitmap(Zombie, (int) ZombieX, (int) ZombieY, null);
            if (ZombieX < canvas.getWidth()/2 + 300) ZombieX += 10;
            else 
                {
                    canvas.drawColor(Color.parseColor("#00001A"));
                    canvas.drawRect(grass, NightGreen);

                    canvas.drawText("The monsters got you!", 150, canvas.getHeight()/2  + 100, text);
                    canvas.drawBitmap(house, canvas.getWidth()/2 + 300, canvas.getHeight()/2 - house.getHeight(), null); 
                    canvas.drawBitmap(moon, canvas.getWidth() - 300, 10, null);
                    ZombieX = canvas.getWidth() + 100;
                    canvas.drawBitmap(play, 0, canvas.getHeight() - play.getHeight() - 25, null);
                }

            if (counter2 < 110) {MoonY --; counter2++;}
            if (counter2 >= 110 && counter2 < 240) {MoonY --; MoonX ++; counter2++;} 
            if (counter2 >= 240 && counter2 < 360) {MoonY -= 0.5; MoonX ++; counter2++;}
            if (counter2 >= 360 && counter2 < 662) {MoonY -= 0.333; MoonX ++; counter2++;}
            if (counter2 >= 662 && counter2 < 1004) {MoonY += 0.333; MoonX++; counter2++;}
            if (counter2 >= 1004 && counter2 < 1104) {MoonY += 0.5; MoonX++; counter2++;}
            if (counter2 >= 1104 && counter2 < 1224) {MoonY ++; MoonX++; counter2++;}
            if (counter2 >= 1224 && counter2 < 1345) {MoonY ++; counter2++;}
            if (counter2 >= 1345)
            {
                canvas.drawText("You killed " + Integer.toString(ZombiesKilled) + " Monsters!", canvas.getWidth()/2 - 100, canvas.getHeight()/2  + 100, text);
                ZombieX = canvas.getWidth() + 100;
                SunX = canvas.getWidth()/2 - sun.getWidth()/2;
                SunY = canvas.getHeight();
                canvas.drawColor(Color.parseColor("#66FFFF"));
                canvas.drawBitmap(sun, canvas.getWidth()/2, 0, null);
                canvas.drawRect(grass, DayGreen);
                canvas.drawBitmap(houseDude, canvas.getWidth()/2 + 300, canvas.getHeight()/2 - house.getHeight(), null);
                canvas.drawBitmap(play, 0, canvas.getHeight() - play.getHeight() - 25, null);
    }


            break;
        }
        canvas.drawText("Score " + Integer.toString(Score), 0, 10, white);      
        canvas.drawRect(AndroidDudeHitbox, clear);
        canvas.drawRect(DoorHitbox, clear);


        if (AndroidX > canvas.getWidth() || AndroidX < 0 || AndroidY > canvas.getHeight() || AndroidY < 0) {AndroidX = 30; AndroidY = canvas.getHeight()/2;} 

        if (DoorHitbox.contains((int) AndroidX, (int) AndroidY)) possition = 1;
        }
    invalidate();
}
4

1 回答 1

3

不不不。永远不要在里面耽搁onDraw。不要将任何游戏逻辑放入onDraw. 就画吧。你有一个黑屏的原因onDraw是没有返回给系统一个机会把你所有的绘图放到物理屏幕上。

相反,创建一个完全独立的线程来控制游戏逻辑(包括计时)。它可以将事件或可运行文件发布到 aHandler以更新游戏显示。

有关这个重要问题的介绍,请参阅文章Painless Threading

于 2012-06-05T01:03:59.357 回答