1

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

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

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

我的程序由一个PongView作为游戏视觉部分的PongDriverActivity类和一个PongView用于其视图的类组成。如果我有一个循环线程无效PongView,我会收到一个错误,因为该线程无法触及在另一个线程上生成的视图。

我想我需要做一些事情AsyncTask,但我不知道如何循环。

关于什么是实现这一点的最佳方法的任何建议?

4

1 回答 1

1

有很多方法可以做到这一点。Android 确实支持普通的 JavaThread对象,您可以使用它们。您可以搜索(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为其设置更改侦听器。还有很多其他方法可以做到这一点。

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

如何在 Java 中从另一个线程暂停和恢复一个线程

更多阅读...

有关游戏循环速度的一些讨论,请参阅此文章...

也许也看看这个讨论

于 2013-04-19T07:35:45.297 回答