获得骰子的价值

时间:2016-05-23 19:22:49

标签: java android

我想制作一个骰子游戏,我使用了一个github项目(https://github.com/dylanmtaylor/Simple-Dice)。

MainActivity可在此处找到:https://github.com/dylanmtaylor/Simple-Dice/blob/master/src/com/dylantaylor/simpledice/RollDice.java

我想得到骰子的价值

例如: 如果第一个骰子是1而第二个骰子是6,那么我总共需要7个骰子。

我创建了一个更改文本视图的按钮,此按钮尝试获取骰子的数量。

TextView hwTextView = (TextView)findViewById(R.id.textView8);
                hwTextView.setText(String.valueOf(diceSum));

我试图获得价值" diceSum"," diceImages"," dice&#34 ;," dice[roll[0]"," dice[roll[1]"但那并没有显示出一个数字。

我怎样才能尝试获得骰子的价值?

我的主要活动

public class dices extends AppCompatActivity {
    Button button;
    private final int rollAnimations = 50;
    private final int delayTime = 15;
    private Resources res;
    private final int[] diceImages = new int[] { R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4, R.drawable.d5, R.drawable.d6 };
    private Drawable dice[] = new Drawable[6];
    private final Random randomGen = new Random();
    @SuppressWarnings("unused")
    private int diceSum;
    private int roll[] = new int[] { 6, 6 };
    private ImageView die1;
    private ImageView die2;
    private LinearLayout diceContainer;
    private SensorManager sensorMgr;
    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;
    private static final int SHAKE_THRESHOLD = 800;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        paused = false;
        setContentView(R.layout.activity_dixit);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(Color.parseColor("#2c3e50"));
        }
        View view = this.getWindow().getDecorView();


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        button=(Button)findViewById(R.id.button_roll);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                rollDice();
                TextView hwTextView = (TextView)findViewById(R.id.textView8);
                hwTextView.setText(String.valueOf(diceSum));
            }
        });


        res = getResources();
        for (int i = 0; i < 6; i++) {
            dice[i] = res.getDrawable(diceImages[i]);
        }

        die1 = (ImageView) findViewById(R.id.die1);
        die2 = (ImageView) findViewById(R.id.die2);
        animationHandler = new Handler() {
            public void handleMessage(Message msg) {
                die1.setImageDrawable(dice[roll[0]]);
                die2.setImageDrawable(dice[roll[1]]);
            }
        };


    }


    private void rollDice() {
        if (paused) return;
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < rollAnimations; i++) {
                    doRoll();
                }
            }
        }).start();
        MediaPlayer mp = MediaPlayer.create(this, R.raw.roll);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
    }

    private void doRoll() { // only does a single roll
        roll[0] = randomGen.nextInt(6);
        roll[1] = randomGen.nextInt(6);
        diceSum = roll[0] + roll[1] + 2; // 2 is added because the values of the rolls start with 0 not 1
        synchronized (getLayoutInflater()) {
            animationHandler.sendEmptyMessage(0);
        }
        try { // delay to alloy for smooth animation
            Thread.sleep(delayTime);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void onResume() {
        super.onResume();
        paused = false;
    }

    public void onPause() {
        super.onPause();
        paused = true;
    }


}

1 个答案:

答案 0 :(得分:0)

我不明白为什么你的代码没有用。我自己根据&#34; Scarne's dice game(pig)&#34;做了一个非常小的游戏。我自己面临着检索掷骰子价值的挑战。记住,我只是使用一个骰子。这就是我所做的:

int userScore;
private int[] diceArray = {R.drawable.d1, R.drawable.d2, R.drawable.d3,
        R.drawable.d4,R.drawable.d5,R.drawable.d6};

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void onRollBtnClicked() {
    ImageView diceFace = (ImageView) findViewById(R.id.diceFaceImageView);
    userScore = randomIntegerGenerator(1,6); // this is what you need
    diceFace.setImageResource(diceArray[userScore]);

}

// function to return random integer from min to max
public int randomIntegerGenerator(int min, int max) {
     return random.nextInt((max - min) + 1) + min;
}

我将我的掷骰价值存储在 userScore 中。您可以使用类似的方法。