0

だから私は本当に単純なサイコロ ゲームを作成しようとしています...サイコロ ローラー ゲームのようなもので、まだ答えが見つからない問題に遭遇しました。したがって、明らかに、サイコロの面(1〜6)に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) {}; 
4

1 に答える 1

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

あなたの価値観があります。画像にデータが含まれることは望ましくありません(または、少なくとも、私は気にしません)。代わりに、これらの値に基づいて画像/アニメーションをレンダリングし、後でそれらの値を直接操作できます。

画像と一緒に値を保存したい場合は、いつでもオブジェクト(GameDie)を作成し、そこに画像と現在の値を保存できます。

于 2011-02-25T18:43:30.240 に答える