从随机位置获取坐标并使用它们
get the coordinates from a random position and use them
我的代码应该像这样工作:
您看到一个深色按钮(可点击,[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
如果我理解得很好,在更改之前从 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()));
}
我的代码应该像这样工作:
您看到一个深色按钮(可点击,[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
如果我理解得很好,在更改之前从 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()));
}