2012-06-05 136 views
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(); 
} 

回答

3

不,不,不。永遠不要延遲onDraw。不要在onDraw中放置任何遊戲邏輯。畫出來。你有黑屏的原因是onDraw沒有返回給系統一個機會把所有的繪圖放到物理屏幕上。

相反,創建一個完全獨立的線程來控制遊戲邏輯(包括時間)。它可以將事件或可運行內容發佈到Handler以更新遊戲顯示。

有關此重要問題的介紹,請參見文章Painless Threading

+0

你能去這個問題,並完成回答我的問題?謝謝! http://stackoverflow.com/questions/10904287/using-a-thread-as-a-gameloop –