2011-02-25 49 views
0

所以我想創建一個真正簡單的骰子游戲...更像是一個骰子游戲,我遇到了一個問題,我還沒有找到答案。所以顯然我有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) {}; 
+2

這是不相關的,但用'extends CrapsActivity'發佈代碼可能會讓你笑一點。 – corsiKa 2011-02-25 18:41:37

+0

我以爲嘿如果我要盯着代碼大量的時間,我可能會笑出來的「小事」可以這麼說。 – FROSTYSMOOTH 2011-02-25 19:04:38

回答

0
roll[0] = randomGen.nextInt(6); 
roll[1] = randomGen.nextInt(6); 

有自己的價值觀。你不希望圖像包含數據(或者至少,我不會打擾)。相反,您基於這些值呈現圖像/動畫,然後可以在稍後直接使用這些值。

如果你想存儲圖像的值,你總是可以創建一個對象(GameDie)並將圖像和當前值存儲在那裏。

+0

他們顯然是隨機的,也是「隨機的」以對應任何死亡的面孔,所以如果他們是隨機的,我將如何使用這些數字? – FROSTYSMOOTH 2011-02-25 20:10:41

+0

@FROSTYSMOOTH 0對應於1的滾動,1到2的滾動等。您將圖像以相同的順序存儲在數組中{face1,face2,face3,...},然後它們匹配。除非你有理由將其設置爲隨機值0以對應裸片上的6面。顯然,如果你需要卷的實際整數值,你仍然需要爲每個死亡加1,但是當你總結兩卷時你已經這樣做了。 – Kendrick 2011-02-25 20:20:05

+0

然後我必須做一些其他的錯誤,因爲當看着模擬器來測試這個時,我的第一個模具的值爲1,模具面爲3,另一個模具的值爲1, 2 ...因此兩個骰子的值都是1,但顯示不同的面值。他們按順序裝載,這就是爲什麼我認爲我完全困惑。 – FROSTYSMOOTH 2011-02-25 21:39:33