所以我想創建一個真正簡單的骰子游戲...更像是一個骰子游戲,我遇到了一個問題,我還沒有找到答案。所以顯然我有6張圖片(1-6),但我在某處丟失的是如何在「骰子滾動」時爲這些圖片分配值。所以當我開始實施一些遊戲邏輯時,我可以比較模具面的數量。這是我迄今爲止所做的,和往常一樣,任何援助都將受到高度讚賞。爲圖像分配一個值
public class CrapsGameActivity extends CrapsActivity {
private final int rollAnimations = 50;
private final int delayTime = 15;
private Resources res;
private final int[] diceImages = new int[]{R.drawable.die1, R.drawable.die2,
R.drawable.die3, R.drawable.die4, R.drawable.die5,R.drawable.die6};
private Drawable dice[] = new Drawable[6];
private final Random randomGen = new Random();
private int diceSum;
private int roll[] = new int[] {6,6};
private ImageView die1;
private TextView die1_Total;
private int die1_number;
private ImageView die2;
private TextView die2_Total;
private TextView diceTotal;
private LinearLayout diceContainer;
private Handler animationHandler;
private long lastUpdate = -1;
private float x, y, z;
private float last_x, last_y, last_z;
private boolean paused = false;
private static final int UPDATE_DELAY = 50;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
paused = false;
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
setTitle(getString(R.string.app_name));
res = getResources();
for (int i = 0; i<6; i++){
dice[i] = res.getDrawable(diceImages[i]);
}
diceContainer = (LinearLayout) findViewById(R.id.diceContainer);
diceContainer.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
try{
rollDice();
die1_Total.setText("" + roll[0]);
die2_Total.setText("" + roll[1]);
diceTotal.setText(""+ diceSum);
}catch(Exception e) {};
}//end of onClick
});//end of onClick listener
die1 = (ImageView) findViewById(R.id.die1);
die1_Total = (TextView) findViewById(R.id.TV_die1);
die2 = (ImageView) findViewById(R.id.die2);
die2_Total = (TextView) findViewById(R.id.TV_die2);
diceTotal = (TextView) findViewById(R.id.TV_diceTotal);
animationHandler = new Handler(){
public void handleMessage(Message msg){
die1.setImageDrawable(dice[roll[0]]);
die2.setImageDrawable(dice[roll[1]]);
}//end of handle message
};//end of Handler
}
private void rollDice() {
if(paused) return;
new Thread(new Runnable(){
@Override
public void run(){
for(int i = 0; i < rollAnimations; i++){
doRoll();
}//end of for statement
}//end of run()
}).start();//end of thread
}//end of rollDice()
private void doRoll(){//only does a single roll
roll[0] = randomGen.nextInt(6);
roll[1] = randomGen.nextInt(6);
diceSum = roll[0] + roll[1] + 2;
synchronized(getLayoutInflater()){
animationHandler.sendEmptyMessage(0);
}
try{//delay for smooth animations
Thread.sleep(delayTime);
}catch(final InterruptedException e){
e.printStackTrace();
}
}//end of doRoll()
public void onResume(){
super.onResume();
paused = false;
}//end of onResume()
public void onPause(){
super.onPause();
paused = true;
}//end of onPause()
}//end of activity
我可能沒有正確地做,但代碼(下)。當我使用它來查看模擬器上的數字時,它們是隨機的,並且不對應於骰子的面值。所以,如果我想總計骰子的面值(現在再次運行它),0代表6張臉,2代表3張臉,這應該給我一個9的總骰子面,但我得到2
public void onClick(View v) {
try{
rollDice();
die1_Total.setText("" + roll[0]);
die2_Total.setText("" + roll[1]);
diceTotal.setText(""+ diceSum);
}catch(Exception e) {};
這是不相關的,但用'extends CrapsActivity'發佈代碼可能會讓你笑一點。 – corsiKa 2011-02-25 18:41:37
我以爲嘿如果我要盯着代碼大量的時間,我可能會笑出來的「小事」可以這麼說。 – FROSTYSMOOTH 2011-02-25 19:04:38