vendredi 27 mars 2015

place a button randomly in layout

my code works like this:buttongame1 is the place where start_time moves on the next i . But buttongame1 should choose it's place randomly, and it does this, but not on i == 0 , so everytime when I open the activity, buttongame1 is not randomly placed



public void onClick(View v) {

textview1 = (TextView) findViewById(R.id.over);

Random r = new Random();

RelativeLayout decorView = (RelativeLayout) buttongame1.getParent();

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



if(v == gameover){


i--;

btncounter.setText("GAME OVER");
start_time.setVisibility(View.GONE);
buttongame1.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(123);
textview1.startAnimation(anim);
Toast.makeText(this, "GAME OVER", Toast.LENGTH_SHORT).show();
over1.start();



finish();


}

if (i == 0 ) {
btncounter.setText("0");

//int x0 = (int) buttongame1.getX();
//int y0 = (int) buttongame1.getY();


}
i++;

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

mp1.start();
}
if (i == 2 ) {
btncounter.setText("2");
int x2 = (int) buttongame1.getX();
int y2 = (int) buttongame1.getY();
start_time.setX(x2);
start_time.setY(y2);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp2.start();
}
if (i == 3 ) {
btncounter.setText("3");
int x3 = (int) buttongame1.getX();
int y3 = (int) buttongame1.getY();
start_time.setX(x3);
start_time.setY(y3);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp3.start();
}
if (i == 4 ) {
btncounter.setText("4");
int x4 = (int) buttongame1.getX();
int y4 = (int) buttongame1.getY();
start_time.setX(x4);
start_time.setY(y4);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp4.start();
}
if (i == 5 ) {
btncounter.setText("5");
int x5 = (int) buttongame1.getX();
int y5 = (int) buttongame1.getY();
start_time.setX((x5));
start_time.setY(y5);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp5.start();
}
if (i == 6 ) {
btncounter.setText("6");
int x6 = (int) buttongame1.getX();
int y6 = (int) buttongame1.getY();
start_time.setX(x6);
start_time.setY(y6);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp6.start();
}
if (i == 7 ) {
btncounter.setText("7");
int x7 = (int) buttongame1.getX();
int y7 = (int) buttongame1.getY();
start_time.setX(x7);
start_time.setY(y7);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp7.start();
}
if (i == 8 ) {
btncounter.setText("8");
int x8 = (int) buttongame1.getX();
int y8 = (int) buttongame1.getY();
start_time.setX(x8);
start_time.setY(y8);
buttongame1.setX(r.nextInt(screenWidth - buttongame1.getHeight()));
buttongame1.setY(r.nextInt(screenHeight - buttongame1.getWidth()));
mp8.start();
}
if (i == 9 ) {
btncounter.setText("9");
int x9 = (int) buttongame1.getX();
int y9 = (int) buttongame1.getY();
start_time.setX(x9);
start_time.setY(y9);
mp9.start();


}

else if (i == 10) {
mp10.start();
btncounter.setText("10");
start_time.setVisibility(View.GONE);
buttongame1.setVisibility(View.GONE);

long difference = (System.currentTimeMillis() - startTime);



String thetime = String.format("%d,%03d sec",difference/1000,difference%1000);

Toast.makeText(this, (thetime), Toast.LENGTH_SHORT).show();




Aucun commentaire:

Enregistrer un commentaire