2014-01-30 62 views
0

這應該是一個骰子應用程序,每個骰子應該在3種顏色之間交替,因此每個面值可以是藍色,黃色或紅色,到目前爲止,我只能我正在製作第一個骰子。現在我有一個數組存儲所有圖像用於每種顏色和陣列中的18個值,因爲這裏有骰子的6邊和每邊3種顏色。所以我有我的方法在陣列中取0-2,應該都是1,紅藍和黃,然後2-5應該是2,紅藍黃。問題是每當我運行它時,它顯示的總數永遠不是正確的總和,因爲面值始終關閉。例如它會顯示一個紅色的3和一個正常的黑色和白色1,總數將是8.任何幫助將不勝感激!方法不是從數組中拉出正確的變量

public class MainActivity extends Activity { 

int[] images = new int[] { 

     // dicex1 
     R.drawable.dicex1red, 
     R.drawable.dicex1blue, 
     R.drawable.dicex1yellow, 

     // dicex2 
     R.drawable.dicex2red, 
     R.drawable.dicex2blue, 
     R.drawable.dicex2yellow, 

     // dicex3 
     R.drawable.dicex3red, 
     R.drawable.dicex3blue, 
     R.drawable.dicex3yellow, 

     // dicex4 
     R.drawable.dicex4red, 
     R.drawable.dicex4blue, 
     R.drawable.dicex4yellow, 

     // dicex5 
     R.drawable.dicex5red, 
     R.drawable.dicex5blue, 
     R.drawable.dicex5yellow, 

     // dicex6 
     R.drawable.dicex6red, 
     R.drawable.dicex6blue, 
     R.drawable.dicex6yellow, }; 

Random three = new Random(); 

MediaPlayer mp; 
ImageView dice1, dice2; 
TextView total; 
Button roll; 
int di1, di2, num; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    roll = (Button) findViewById(R.id.bRoll); 
    total = (TextView) findViewById(R.id.tvTotal); 
    dice1 = (ImageView) findViewById(R.id.IVdice1); 
    dice2 = (ImageView) findViewById(R.id.IVdice2); 
    mp = new MediaPlayer(); 
    final Random random = new Random(); 

    roll.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      mp.release(); 
      di1 = random.nextInt(6); 
      di1++; 
      di2 = random.nextInt(6); 
      di2++; 

      if (di1 == 1) { 
       numb(2); 

       numColor(); 
      } else if (di1 == 2) { 
       numb(5); 
       num = num + 3; 
       numColor(); 
      } else if (di1 == 3) { 
       numb(8); 
       num = num + 6; 
       numColor(); 
      } else if (di1 == 4) { 
       numb(11); 
       num = num + 9; 
       numColor(); 
      } else if (di1 == 5) { 
       numb(14); 
       num = num + 12; 
       numColor(); 
      } else if (di1 == 6) { 
       numb(17); 
       num = num + 15 ; 
       numColor(); 
      } 

      if (di2 == 1) { 
       dice2.setImageResource(R.drawable.dicex1); 
      } else if (di2 == 2) { 
       dice2.setImageResource(R.drawable.dicex2); 
      } else if (di2 == 3) { 
       dice2.setImageResource(R.drawable.dicex3); 
      } else if (di2 == 4) { 
       dice2.setImageResource(R.drawable.dicex4); 
      } else if (di2 == 5) { 
       dice2.setImageResource(R.drawable.dicex5); 
      } else if (di2 == 6) { 
       dice2.setImageResource(R.drawable.dicex6); 
      } 
      int answer = di1 + di2; 
      total.setText("The total is " + answer); 

      mp = MediaPlayer.create(getApplicationContext(), 
R.raw.dice); 
      mp.start(); 

     } 

    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void numColor() { 



    if (num == 1) { 
     dice1.setImageResource(images[0]); 
    } else if (num == 2) { 
     dice1.setImageResource(images[1]); 
    } else if (num == 3) { 
     dice1.setImageResource(images[2]); 
    } else if (num == 4) { 
     dice1.setImageResource(images[3]); 
    } else if (num == 6) { 
     dice1.setImageResource(images[4]); 
    } else if (num == 7) { 
     dice1.setImageResource(images[5]); 
    } else if (num == 8) { 
     dice1.setImageResource(images[6]); 
    } else if (num == 9) { 
     dice1.setImageResource(images[7]); 
    } else if (num == 10) { 
     dice1.setImageResource(images[8]); 
    } else if (num == 11) { 
     dice1.setImageResource(images[11]); 
    } else if (num == 12) { 
     dice1.setImageResource(images[12]); 
    } else if (num == 13) { 
     dice1.setImageResource(images[13]); 
    } else if (num == 14) { 
     dice1.setImageResource(images[14]); 
    } else if (num == 15) { 
     dice1.setImageResource(images[15]); 
    } else if (num == 16) { 
     dice1.setImageResource(images[16]); 
    } else if (num == 17) { 
     dice1.setImageResource(images[17]); 
    } else if (num == 18) { 
     dice1.setImageResource(images[18]); 
    } 
} 

public void numb(int x){ 
    num = three.nextInt(x); 
} 
} 
+0

我很難理解。如我錯了請糾正我。你想弄清楚給出的顏色和價值的適當的drawable。是嗎? –

+0

是的,我希望它總是隨機選擇骰子上數字的3種顏色中的1種,但它不會從數組中選擇正確的可繪製物或某物,因爲2個骰子的總數永遠不會出現正確。 – user2555459

回答

0

這裏就是我會做

private static final int RED = 0; 
private static final int BLUE = 1; 
private static final int YELLOW = 2; 

private int getResourceForDie(int color, int value) { 
    return images[3*(value - 1) + color]; 
} 

所以獲取資源的價值3黃色死,你會打電話getResourceForDie(YELLOW, 3)其中value是1和6(含)之間。

+0

這看起來像它可以工作,但我很新的Android,有點困惑我將如何在我的應用程序中實現這一點。 – user2555459

+0

如果你想讓dice1成爲一個值爲3的黃色模子,你可以調用'dice1.setImageResource(getResourceForDie(YELLOW,3));' –

+0

好吧,讓我的數組保存所有的圖像,然後添加這個方法。我將如何去使用Random()從這個方法調用一個隨機數和顏色? – user2555459

相關問題