【问题标题】:get the coordinates from a random position and use them从随机位置获取坐标并使用它们
【发布时间】:2015-03-12 21:04:34
【问题描述】:

我的代码应该是这样的:

您会看到一个黑色按钮(可点击,[start_time])和一个灰色按钮(不可点击,[buttongame1])。 buttongame1 预测,start_time 下一步将移动到哪里。 当您单击 start_time 时,start_time 将移动到 buttongame1 的 x 和 y。 同时,buttongame1 将它的位置随机设置为新的。 现在,当您单击 start_time 时,start_time 将移动到 buttongame1 的新 x 和 y。 同时,buttongame1 随机设置它的位置,以此类推。

我已经尝试过了,但问题是,我无法从 buttongame1 获取或使用 x & y

public void onClick(View v) {

    int x0;
    int y0;
    int x1;
    int y1;
    int x2;
    int y2;
    int x3;
    int y3;
    int x4;
    int y4;
    int x5;
    int y5;
    int x6;
    int y6;
    textview1 = (TextView) findViewById(R.id.over);


    Random r = new Random();
    RelativeLayout decorView = (RelativeLayout) buttongame1.getParent();

    int screenWidth = decorView.getWidth();
    int screenHeight = decorView.getHeight();

    i++;

    if(v == gameover){
        i--;
        btncounter.setText("GAME OVER");
        start_time.setVisibility(View.GONE);
        textview1.setVisibility(View.VISIBLE);
        Animation anim = new AlphaAnimation(0.0f, 1.0f);
        anim.setDuration(50); //You can manage the time of the blink with this parameter
        anim.setStartOffset(20);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        textview1.startAnimation(anim);
        Toast.makeText(this, "GAME OVER", Toast.LENGTH_SHORT).show();

        finish();
    }

    if (i == 0 ) {
        btncounter.setText("0");
        buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
        buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));
        int x0 = (int) buttongame1.getX();
        int y0 = (int) buttongame1.getY();
    }

    if (i == 1 ) {
        btncounter.setText("1");
        startTime = System.currentTimeMillis();
        buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
        buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));
        int x1 = (int) buttongame1.getX();
        int y1 = (int) buttongame1.getY();
        start_time.setX((x1));
        start_time.setY(y1);

// ... and so on

【问题讨论】:

    标签: java android button random set


    【解决方案1】:

    如果我理解得很好,请在更改之前从 buttongame1 中获取 X 和 Y。

         if (i == 1 ) {
                btncounter.setText("1");
                 startTime = System.currentTimeMillis();
                int x1 = (int) buttongame1.getX();
                int y1 = (int) buttongame1.getY();
                 buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
                 buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));
    
                start_time.setX((x1));
                start_time.setY(y1);
       }
    

    此代码也是:

       if (i == 0 ) {
                btncounter.setText("0");
                int x0 = (int) buttongame1.getX();
                int y0 = (int) buttongame1.getY();
                 buttongame1.setX(r.nextInt(screenHeight - buttongame1.getWidth()));
                 buttongame1.setY(r.nextInt(screenHeight - buttongame1.getHeight()));
    
       }
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2023-03-26
      • 2012-05-27
      • 2011-11-28
      • 2014-11-15
      • 2018-03-23
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      相关资源
      最近更新 更多