Wednesday, 15 September 2010

fixing my current project android - button on click -



fixing my current project android - button on click -

public void onclick(view v) { int id = v.getid(); if (id == r.id.button2) { counter++; changingtextview.settext(""+counter); youlost.settext(""); final random rand = new random(); int diceroll = rand.nextint(6) + 1; if (diceroll == 1) { action1(); } else if (diceroll == 2) { action2(); } else if (diceroll == 3) { action3(); } else if (diceroll == 4) { action4(); } else if (diceroll == 5) { action5(); } else if (diceroll == 6) { action6(); } } else if (id == r.id.button4 || id == r.id.button6 || id == r.id.button3 || id == r.id.button5 || id == r.id.button1) { youlost.settext("you lost"); counter=0; changingtextview.settext(""+counter); } } private void action6() { // todo auto-generated method stub button2.setx(button2.getx()); button2.sety(button2.gety()); } private void action5() { // todo auto-generated method stub button2.setx(button1.getx()); button2.sety(button1.gety()); button1.setx(button2.getx()); button1.sety(button2.gety()); } private void action4() { // todo auto-generated method stub button2.setx(button3.getx()); button2.sety(button3.gety()); button3.setx(button2.getx()); button3.sety(button2.gety()); } private void action3() { // todo auto-generated method stub button2.setx(button4.getx()); button2.sety(button4.gety()); button4.setx(button2.getx()); button4.sety(button2.gety()); } private void action2() { // todo auto-generated method stub button2.setx(button5.getx()); button2.sety(button5.gety()); button5.setx(button2.getx()); button5.sety(button2.gety()); } private void action1() { // todo auto-generated method stub button2.setx(button6.getx()); button2.sety(button6.gety()); button6.setx(button2.getx()); button6.sety(button2.gety()); } }

hello :) current long script, want app recognize button pressing on , if button2 alter button2 place other button place (randomly), did 2 buttons set same x,y , buttons dissapear, can plz prepare me? give thanks much :)

there problem code:

button2.setx(button1.getx()); button2.sety(button1.gety()); button1.setx(button2.getx()); button1.sety(button2.gety());

in above lines have set x,y of button1 button2 , both have same x,y. therefore, next 2 lines don't alter anything.

if looking swap positions of these 2 buttons seek this:

float x=button2.getx(); float y= button2.gety(); button2.setx(button1.getx()); button2.sety(button1.gety()); button1.setx(x); button1.sety(y);

do above in action function.

android

No comments:

Post a Comment