jeudi 12 mars 2015

get the coordinates from a random position and use them

My code should work something like this:


you see one dark button (clickable, [start_time]) and one grey button (not clickable, [buttongame1]). the buttongame1 predict, where start_time will move next. when you click start_time, start_time moves to x and y of buttongame1. At the same time, buttongame1 set it's position randomly new. now when you click start_time, start_time moves to the new x and y of buttongame1. At the same time, buttongame1 set it's position randomly new, and so on.


I've tried this, but the problem is, I can't get or use the x & y from the buttongame1



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




Aucun commentaire:

Enregistrer un commentaire