2015-03-28 4 views
-1

мой код работает следующим образом: buttongame1 - это место, где start_time перемещается по следующему i. Но buttongame1 должен выбрать это место случайно, и он делает это, но не я == 0, поэтому каждый раз, когда я открываю свою деятельность, buttongame1 не случайно помещенслучайное расположение кнопки в макете

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

ответ

0

Мне нужно было поставить (i = 0) до вызова onClick. как я это сделал, все сработало хорошо.

public class Game extends Activity implements OnClickListener{ 

    Button start_time; 
    Button buttongame1; 
    int i; 


     Random r; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 




     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_game); 
      start_time = (Button) findViewById(R.id.start_time); 
      start_time.setOnClickListener(this); 
      textview1 = (TextView) findViewById(R.id.over); 
      gameover = (Button) findViewById(R.id.gameover); 
      gameover.setOnClickListener(this); 

      i = 0; 
      r = new Random(); 
     buttongame1 = (Button) findViewById(R.id.buttongame1); 
     btn_start2 = (Button) findViewById(R.id.btn_start2); 


     smthin(); 


    } 


    public void smthin() { 


     DisplayMetrics displaymetrics = new DisplayMetrics(); 
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
      int height = displaymetrics.heightPixels; 
      int Width = displaymetrics.widthPixels; 


        try { 

         if (i == 0) { 
         buttongame1.setX(r.nextInt(Width - buttongame1.getHeight())); 
         buttongame1.setY(r.nextInt(height - buttongame1.getWidth())); 

         } 


        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

    } 

, а затем остальная часть моего кода.

1

у меня нет никакого кода для г == 0, установить случайную позицию в i == 0 ..

Смежные вопросы