Android - 为乒乓游戏创建一个Timer循环

时间:2013-04-19 00:58:53

标签: android loops timer android-asynctask pong

我有一个用Java编写的 Pong 游戏,我正在尝试将其移植到Android。 (这是我的第一款Android游戏;)。

在Java中,我使用了Timer对象,它基本上更新了游戏值(球/桨位置),然后重新绘制了屏幕。

我正在尝试在Android中实现相同的功能,但我遇到了一些错误。

我的程序包含一个PongView类,它是游戏的可视部分,以及一个PongDriverActivity类,它使用PongView作为视图。如果我有一个循环线程使PongView无效,我会收到一个错误,因为该线程无法触及另一个线程上生成的视图。

我认为我需要做某种AsyncTask,但我不确定如何循环。

有关实施此方法的最佳方法的任何建议吗?

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点。 Android确实支持普通的Java Thread对象,而可以使用它们。您可以搜索(SO)android game loop,并可能找到很多示例。但是,如果你想尝试AsyncTask,这里有一个示例实现:

public class PongView extends View {

    public PongView(Context context) {
        super(context);
    }    
    public void setBallPosition(int x, int y) {
        // relocate the ball graphic
    }    
}

然后,你的活动:

public class PongDriverActivity extends Activity implements OnSeekBarChangeListener {

    private enum GameSpeed { SLOW, MEDIUM, FAST };

    private PongView mGameView;
    private PongDriverTask mWorker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGameView = new PongView(this);
        mWorker = new PongDriverTask(GameSpeed.MEDIUM);

        // if you need to pass some data to the worker:
        mWorker.execute("one", "two", "three");  
        // else, declare the AsyncTask's first generic param as Void, and do:
        //mWorker.execute();

        setContentView(mGameView);
    }

    // you would connect this up to a button, to allow the user to stop  
    public void onStopClicked(View sender) {
        if (mWorker.isRunning()) {
            mWorker.stopRunning();
        }
    }

    // you could connect this up to a slider, to allow the user to control the paddle
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        // you may need to convert progress to a y-coordinate
        mWorker.setPaddlePosition(progress);
    }

然后(可能在PongDriverActivity.java里面,一个内部类):

private class PongDriverTask extends AsyncTask<String, Integer, Integer> {

    private int mDelay;
    private boolean mRunning = true;
    private int mPaddleY = 0;

    public PongDriverTask(GameSpeed speed) {
        if (speed == GameSpeed.SLOW) {
            mDelay = 100;
        } else if (speed == GameSpeed.MEDIUM) {
            mDelay = 50;
        } else if (speed == GameSpeed.FAST) {
            mDelay = 10;
        }
    }

    public synchronized boolean isRunning() {
        return mRunning;
    }
    public synchronized void stopRunning() {
        mRunning = false;
    }
    public synchronized void setPaddlePosition(int y) {
        mPaddleY = 0;
    }
    private synchronized int getPaddlePosition() {
        return mPaddleY;
    }

    @Override
    protected void onPostExecute(Integer score) {
        super.onPostExecute(score);
        // here you could show a UI that shows the final score
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mGameView.setBallPosition(0, 0);
        // here you could throw up some UI that shows just
        //  before the game really starts
    }

    @Override
    protected void onProgressUpdate(Integer... params) {
        super.onProgressUpdate(params);
        // retrieve updated game coordinates from params
        mGameView.setBallPosition(params[0], params[1]);  // x,y
    }

    @Override
    protected Integer doInBackground(String... args) {
        // If you have some information to pass to the background worker, 
        //  it would be passed in args[0], args[1], etc.
        // It doesn't have to be String data.  you could change the generic 
        //  to take something other than String as the first param, in which
        //  case, doInBackground() would take a variable length list of that
        //  other data type.
        int ballX = 0;
        int ballY = 0;
        int score = 0;

        while (isRunning()) {
            // use your game engine to recalculate the ball position
            //  on this background thread
            int paddleY = getPaddlePosition();
            ballX += 1;
            ballY += 2;
            score++;

            // now, update the UI, which must happen on the UI thread
            publishProgress(ballX, ballY);

            try {
                Thread.sleep(mDelay);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return score;
    }
}

任务的doInBackground()方法将在后台线程上运行,因此您不能直接从那里修改UI。但是,我在上面覆盖的所有其他AsyncTask方法都在UI线程上调用,因此可以安全地在其中任何一个中执行UI工作。

此代码假定拨片由搜索栏控制,您可以使Activity成为更改侦听器。还有很多其他方法可以做到。

我刚刚实施了一个粗略的游戏停止机制,你可以连接到一个按钮。如果您想允许用户暂停和恢复,您可以搜索暂停/恢复线程的实现,这也适用于此处。例如:

How to Pause and Resume a Thread in Java from another Thread

更多阅读......

See this writeup for some discussion of game loop speeds ...

and maybe take a look at this discussion, too