jeudi 19 mars 2015

set 2 buttons differently in an if-statement

My code should work like this: I see 2 buttons and i have to press each button 10 times to finish and after that, I'll get the time, how long I had.


my code now works like this: There are 2 buttons, but if I click one button, both buttons set their positions new, and thats wrong. (I just made the 2nd button till j == 2 because it didn't worked anyway)



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(123);
textview1.startAnimation(anim);
Toast.makeText(this, "GAME OVER", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),MainScreen.class));



finish();


}

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

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


}
i++;
j++;

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()));




}
if (j == 1 ) {


int xa1 = (int) buttongame2.getX();
int ya1 = (int) buttongame2.getY();
start_time2.setX(xa1);
start_time2.setY(ya1);
buttongame2.setX(r.nextInt(screenWidth2 - buttongame2.getHeight()));
buttongame2.setY(r.nextInt(screenHeight2 - buttongame2.getWidth()));

}
if (j == 2 ) {




int xa2 = (int) buttongame2.getX();
int ya2 = (int) buttongame2.getY();
start_time2.setX(xa2);
start_time2.setY(ya2);
buttongame2.setX(r.nextInt(screenWidth2 - buttongame2.getHeight()));
buttongame2.setY(r.nextInt(screenHeight2 - buttongame2.getWidth()));

}
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()));




}

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()));
}
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()));
}
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()));
}
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()));
}
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()));
}
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()));
}
if (i == 9 ) {
btncounter.setText("9");
int x9 = (int) buttongame1.getX();
int y9 = (int) buttongame1.getY();
start_time.setX(x9);
start_time.setY(y9);


}

else if (i == 10) {
btncounter.setText("10");


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



String thetime = String.format("%d,%d sec",

TimeUnit.MILLISECONDS.toSeconds(difference) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(difference)),
TimeUnit.MILLISECONDS.toMillis(difference)/10);

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





SharedPreferences prefs = this.getSharedPreferences("thetime1", Context.MODE_PRIVATE);

SharedPreferences prefs2 = this.getSharedPreferences("thetime2", Context.MODE_PRIVATE);

prefs3 = this.getSharedPreferences("thetime3", Context.MODE_PRIVATE);
Editor editor3 = prefs3.edit();
editor3.putLong("thetime3", difference);
editor3.commit();

if (difference < prefs2.getLong("thetime2", 100000) ) {
Editor editor = prefs.edit();
editor.putString("thetime1", thetime);
editor.commit();
Toast.makeText(this, "NEW HIGHSCORE", Toast.LENGTH_SHORT).show();

Editor editor1 = prefs2.edit();
editor1.putLong("thetime2", difference);
editor1.commit();



}


// Use the Builder class for convenient dialog construction

Builder builder;

builder = new AlertDialog.Builder(v.getContext()).setView(v);
Dialog d = builder.setView(new View(this)).create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
builder.setCancelable(false);
if (difference > prefs2.getLong("thetime2", prefs3.getLong("thetime3", 0) )) {

lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
TextView myMsg = new TextView(this);
myMsg.setText("YOU GOT " +(thetime));
myMsg.setGravity((Gravity.FILL_VERTICAL)+(Gravity.CENTER_HORIZONTAL));
myMsg.setGravity((Gravity.CENTER_HORIZONTAL));
myMsg.setTextAppearance(this, android.R.style.TextAppearance_Large);
d.getWindow().setAttributes(lp);

builder.setView(myMsg);


builder.setPositiveButton("mkay :D", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(getApplicationContext(),MainScreen.class));
finish();
// FIRE ZE MISSILES!
}


});

}
else {

lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
TextView myMsg = new TextView(this);
myMsg.setText("YOU SET A NEW RECORD WITH " +(thetime)+" !");
myMsg.setGravity((Gravity.FILL_VERTICAL));
myMsg.setGravity((Gravity.CENTER_HORIZONTAL));
myMsg.setTextAppearance(this, android.R.style.TextAppearance_Large);
builder.setView(myMsg);
d.getWindow().setAttributes(lp);

builder.setPositiveButton("mkay :D", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(getApplicationContext(),MainScreen.class));
finish();
// FIRE ZE MISSILES!
}

});
}
// Create the AlertDialog object and return it
// builder.create();

builder.show();



}


}

Aucun commentaire:

Enregistrer un commentaire