2012-06-05 1 views
0

Dans mon sur le tirage son est fait comme un jeu. un zombie vient et vous devez cliquer sur lui pour qu'il revienne au début et ne le fasse pas chez vous. Mais je voulais ajouter une pause à mon jeu, donc je pense que je vais tout emballer dans une boucle de temps ... eh bien, quand j'ai essayé cela quand l'application s'ouvre, elle s'ouvre simplement avec un écran noir. voici une partie de mon code.Tandis que les boucles dans ondraw

@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(); 
} 

Répondre

3

Non, non, non. Ne retardez jamais à l'intérieur onDraw. Ne mettez aucune logique de jeu dans onDraw. Dessine juste. La raison pour laquelle vous avez un écran noir est que onDraw ne revient pas pour donner au système une chance de mettre tout votre dessin sur l'écran physique. Au lieu de cela, créez un thread complètement séparé qui contrôle la logique du jeu (y compris la synchronisation). Il peut publier des événements ou des runnables sur un Handler pour mettre à jour l'affichage du jeu. Voir l'article Painless Threading pour une introduction à cette question importante.

+0

pourriez-vous répondre à cette question et finir de répondre à ma question? Merci! http://stackoverflow.com/questions/10904287/using-a-thread-as-a-gameloop –

Questions connexes