所以我正在尝试创建一个真正简单的骰子游戏......更像是一个掷骰子游戏,我遇到了一个我还没有找到答案的问题。所以很明显我有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) {};