2015-04-27 58 views
-1

我正在做一對卡片內存匹配遊戲。
它包含一個4x4矩陣。點擊兩次按鈕,它會在Android遊戲中消失

當我點擊兩次上的任何按鈕,它就會消失。
並點擊一些按鈕後,不幸的是遊戲停止並返回到菜單屏幕。

相反的過程中應該做一個卡一雙隱形的,當卡匹配,並播放一些聲音。

public class Birds 
     extends Activity { 
    private int[] id_bird = new int[16]; 
    private Integer[][] img_bird = new Integer[16][2]; 
    private Button[] myBird = new Button[16]; 
    private int mc_counter = 0; 
    private int firstid = 0; 
    private int secondid = 0; 
    private Boolean mc_isfirst = false; 
    private int correctcounter = 0; 
    //private TextView tFeedback; 
    private MediaPlayer mp; 
    private Boolean b_snd_inc, b_snd_cor, b_new_game; 
    public static boolean DEBUG = false; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     // Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show(); 
     initGame(); 
    } 

    private void initGame() { 
     SharedPreferences settings = getSharedPreferences("memoryPrefs", 0); 
     b_snd_cor = settings.getBoolean("play_sound_when_correct", true); 
     b_snd_inc = settings.getBoolean("play_sound_when_incorrect", true); 
     b_new_game = settings.getBoolean("new_game", true); 
     if(b_new_game) { 
      setContentView(R.layout.birds); 
      mc_counter = 0; 
      firstid = 0; 
      secondid = 0; 
      mc_isfirst = false; 
      correctcounter = 0; 
      //tFeedback = (TextView) findViewById(R.id.mc_feedback); 
      // fill arrays with resources 
      id_bird[0] = R.id.mc0; 
      id_bird[1] = R.id.mc1; 
      id_bird[2] = R.id.mc2; 
      id_bird[3] = R.id.mc3; 
      id_bird[4] = R.id.mc4; 
      id_bird[5] = R.id.mc5; 
      id_bird[6] = R.id.mc6; 
      id_bird[7] = R.id.mc7; 
      id_bird[8] = R.id.mc8; 
      id_bird[9] = R.id.mc9; 
      id_bird[10] = R.id.mc10; 
      id_bird[11] = R.id.mc11; 
      id_bird[12] = R.id.mc12; 
      id_bird[13] = R.id.mc13; 
      id_bird[14] = R.id.mc14; 
      id_bird[15] = R.id.mc15; 
      img_bird[0][0] = R.drawable.bird1; 
      img_bird[0][1] = R.mipmap.bird1; 
      img_bird[1][0] = R.drawable.bird2; 
      img_bird[1][1] = R.mipmap.bird2; 
      img_bird[2][0] = R.drawable.bird3; 
      img_bird[2][1] = R.mipmap.bird3; 
      img_bird[3][0] = R.drawable.bird4; 
      img_bird[3][1] = R.mipmap.bird4; 
      img_bird[4][0] = R.drawable.bird5; 
      img_bird[4][1] = R.mipmap.bird5; 
      img_bird[5][0] = R.drawable.bird6; 
      img_bird[5][1] = R.mipmap.bird6; 
      img_bird[6][0] = R.drawable.bird7; 
      img_bird[6][1] = R.mipmap.bird7; 
      img_bird[7][0] = R.drawable.bird8; 
      img_bird[7][1] = R.mipmap.bird8; 
      img_bird[8][0] = R.drawable.bird1; 
      img_bird[8][1] = R.mipmap.bird1; 
      img_bird[9][0] = R.drawable.bird2; 
      img_bird[9][1] = R.mipmap.bird2; 
      img_bird[10][0] = R.drawable.bird3; 
      img_bird[10][1] = R.mipmap.bird3; 
      img_bird[11][0] = R.drawable.bird4; 
      img_bird[11][1] = R.mipmap.bird4; 
      img_bird[12][0] = R.drawable.bird5; 
      img_bird[12][1] = R.mipmap.bird5; 
      img_bird[13][0] = R.drawable.bird6; 
      img_bird[13][1] = R.mipmap.bird6; 
      img_bird[14][0] = R.drawable.bird7; 
      img_bird[14][1] = R.mipmap.bird7; 
      img_bird[15][0] = R.drawable.bird8; 
      img_bird[15][1] = R.mipmap.bird8; 
      if(DEBUG == false) { 
       Collections.shuffle(Arrays.asList(img_bird)); 
      } 
      for(int i = 0; i < 16; i++) { 
       myBird[i] = (Button) findViewById(id_bird[i]); 
       myBird[i].setBackgroundResource(img_bird[i][0]); 
       myBird[i].setOnClickListener(new View.OnClickListener() { 
        public void onClick(View view) { 
         int i = 0; 
         for(int n = 0; n < 16; n++) { 
          if(id_bird[n] == view.getId()) { 
           i = n; 
          } 
         } 
         doClickAction(view, i); 
        } 
       }); 
      } 
     } 
    } 

    private void doClickAction(View v, int i) { 
     v.setBackgroundResource(img_bird[i][1]); 
     mc_isfirst = !mc_isfirst; 
     // disable all buttons 
     for(Button b : myBird) { 
      b.setEnabled(false); 
     } 
     if(mc_isfirst) { 
      // turning the first card 
      firstid = i; 
      // re enable all except this one 
      for(Button b : myBird) { 
       if(b.getId() != firstid) { 
        b.setEnabled(true); 
       } 
      } 
     } else { 
      // turning the second card 
      secondid = i; 
      doPlayMove(); 
     } 
    } 

    private void doPlayMove() { 
     mc_counter++; 
     if(img_bird[firstid][1] - img_bird[secondid][1] == 0) { 
      // correct 
      if(b_snd_cor) { 
       playSound(R.raw.bird); 
      } 
      waiting(50); 
      myBird[firstid].setVisibility(View.INVISIBLE); 
      myBird[secondid].setVisibility(View.INVISIBLE); 
      correctcounter++; 
     } else { 
      // incorrect 
      if(b_snd_inc) { 
       playSound(R.raw.incorrect); 
      } 
      waiting(50); 
     } 
     // re-enable and turn cards back 
     for(Button b : myBird) { 
      if(b.getVisibility() != View.INVISIBLE) { 
       b.setEnabled(true); 
       b.setBackgroundResource(R.drawable.questionbox1); 
       for(int i = 0; i < 16; i++) { 
        myBird[i].setBackgroundResource(img_bird[i][0]); 
       } 
      } 
     } 
     //tFeedback.setText(String.format("%d/%d", correctcounter, mc_counter)); 
     if(correctcounter > 7) { 
      Intent iSc = new Intent(getApplicationContext(), Congrats.class); 
      startActivity(iSc); 
     } 
    } 

    public void playSound(int sound) { 
     mp = MediaPlayer.create(this, sound); 
     mp.setVolume((float) .5, (float) .5); 
     mp.start(); 
     mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
      public void onCompletion(MediaPlayer mp) { 
       mp.release(); 
      } 
     }); 
    } 

    public static void waiting(int n) { 
     long t0, t1; 
     t0 = System.currentTimeMillis(); 
     do { 
      t1 = System.currentTimeMillis(); 
     } while((t1 - t0) < (n)); 
    } 
} 
+0

提供一些日誌,這樣我就可以看到異常 –

+1

所以呢?什麼是問題? – kebs

+0

'按鈕不幸停止'?你可以發佈你的[logcat](https://politecuriosity.files.wordpress.com/2013/05/logcat.jpg)。 – Gattsu

回答

0

你從字面上只需要在這裏添加一個額外的條件:

private void doPlayMove() { 
    mc_counter++; 
    if((secondId != firstId) && (img_bird[firstid][1] - img_bird[secondid][1] == 0)) { 
     ... 

雖然它甚至更好,如果你讓這個只有當還沒有選擇它,你可以「選擇」的元素然而。

if(mc_isfirst) { 
     // turning the first card 
     firstid = i; 
     // re enable all except this one 
     for(Button b : myBird) { 
      if(b.getId() != firstid) { 
       b.setEnabled(true); 
      } 
     } 
    } else { 
     // turning the second card 
     if(i != firstid) { 
      secondid = i; 
      doPlayMove(); 
     } 
    } 

你應該用最後一種方法。