加速随机过程

时间:2017-07-11 11:03:23

标签: java android

我在一个简单的Android游戏上练习,当用户点击它时,在屏幕上随机放置一个圆形按钮。 它工作正常,但我想加快放置按钮的过程,以便游戏变得更难用户......

这是我正在使用的代码 -

public class GameWindow extends Activity implements View.OnClickListener {
static int score;
private Timer t;
private int TimeCounter = 29;
private boolean canMove = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    ////Remove title screen for activty.
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_game_window);
    moveButton();
    endonTimeOver();

}


public void endonTimeOver(){
    ////Activity timer for 60 seconds.

    final TextView timer = (TextView) findViewById(R.id.seconds);

    t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {

        ////Set string to timer.
        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                public void run() {
                    timer.setText(String.valueOf(TimeCounter)); // you can set it to a textView to show it to the user to see the time passing while he is writing.
                    TimeCounter = TimeCounter - 1;
                }
            });

        }
    }, 1000, 1000); // 1000 means start from 1 sec, and the second 1000 is do the loop each 1 sec.

    new Timer().schedule(new TimerTask(){
        public void run() {
            GameWindow.this.runOnUiThread(new Runnable() {
                public void run() {
                    startActivity(new Intent(GameWindow.this, Finished.class));
                }
            });
        }
    }, 30000);

}

////Move button.
private void moveButton()
{
    if(!canMove){ return; }

    runOnUiThread(
            new Runnable()
            {
                @Override
                public void run()
                {

                    Display display = getWindowManager().getDefaultDisplay();
                    Point size = new Point();
                    display.getSize(size);
                    int width = size.x;
                    int height = size.y;

                    Button button = (Button)findViewById(R.id.button);
                    Random r = new Random();

                    int startX = width/2;
                    int startY = height/2;

                    if(score==0){
                        button.setX(startX);
                        button.setY(startY);
                    }

                    else {

                        int x = r.nextInt(width - 210);
                        int y = r.nextInt(height - 200);

                        button.setX(x);
                        button.setY(y);
                    }
                }
            }
    );

}




////Display score
public void displayScore(int score) {
    TextView scoreView = (TextView) findViewById(R.id.score);
    scoreView.setText(String.valueOf(score));
}


@Override
public void onClick(View v) {
    MediaPlayer mp = MediaPlayer.create(this, R.raw.buttonsound);
    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            // TODO Auto-generated method stub
            mp.release();
        }

    });
    mp.start();

    score = score + 1;
    displayScore(score);
    switch (v.getId()) {
        case (R.id.button): {
           moveButton();
        }
    }

}

public static int getScore(){
    return score;
}}

2 个答案:

答案 0 :(得分:1)

对不会更改的值使用全局变量:

  • findViewById很慢
  • 每次都不需要创建新的随机
  • 每次都不需要获取窗口参数

答案 1 :(得分:0)

你似乎正在开始一个新线程并告诉它runonui,这可能会让你失望,试试这个:

private void moveButton()
{
    if(!canMove){ return; }

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    Button button = (Button)findViewById(R.id.button);
    Random r = new Random();

    int startX = width/2;
    int startY = height/2;

    if(score==0){
          button.setX(startX);
          button.setY(startY);
          }

    else {

       int x = r.nextInt(width - 210);
       int y = r.nextInt(height - 200);

       button.setX(x);
       button.setY(y);
       }
    }

计算本身似乎并不重,所以不需要另一个线程,你可以在主线程上执行,如果你从oncreate调用那意味着你已经在主线程上了如果我理解了这个问题,这可能会给你答案,试试吧