所以我试图创建一个真正简单的骰子游戏...更像是一个骰子滚动游戏,我遇到了一个问题,我还没有找到答案。所以很明显我有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) {};
答案 0 :(得分:0)
roll[0] = randomGen.nextInt(6);
roll[1] = randomGen.nextInt(6);
有你的价值观。您不希望图像包含数据(或者至少,我不会打扰)。相反,您可以根据这些值渲染图像/动画,然后可以在以后直接使用这些值。
如果你想用图像存储值,你总是可以创建一个对象(GameDie)并将图像和当前值存储在那里。