如何在Android游戏中重启Java Thread?

时间:2017-01-06 11:26:21

标签: java android java-threads

我正在Android中创建一个用于学习目的的演示游戏。在游戏中,当在两个对象之间检测到碰撞时 - 我想显示“游戏结束”对话框。在对话框内有一个再次播放按钮。按下时,应再次启动线程。

我收到以下错误 java.lang.IllegalThreadStateException: Thread already started

这是我的代码:

public class GamePanel extends SurfaceView implements Runnable {

    private Thread thread = null;
    private Ball ball;
    private SurfaceHolder surfaceHolder;
    private Paint paint;
    private Canvas canvas;

    volatile boolean playing = true;

    private int hurdleCount = 3;
    private Hurdles[] hurdles;

    private int screenX, screenY;
    private Rect ball_detectCollision;

    public GamePanel(Context context, final int screenX, final int screenY) {
        super(context);

        ball = new Ball(context, screenX, screenY);
        surfaceHolder = getHolder();

        this.screenX = screenX;
        this.screenY = screenY;

        paint = new Paint();
        canvas = new Canvas();

        hurdles = new Hurdles[hurdleCount];
        for (int i = 0; i < hurdleCount; i++) {
            hurdles[i] = new Hurdles(context, screenX, screenY);
        }

        ball_detectCollision = new Rect(ball.getBall_x(), ball.getBall_y(), ball.getBitmap().getWidth(), ball.getBitmap().getHeight());
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {

                System.out.println("Surface Created");
                setUpdated_x(ball.getBall_x());
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                System.out.println("Surface Changed");
                thread = new Thread(GamePanel.this);
                thread.start();
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

                System.out.println("Surface Destroyed");
            }
        });
    }

    public int getUpdated_x() {
        return updated_x;
    }

    public void setUpdated_x(int updated_x) {
        this.updated_x = updated_x;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        setUpdated_x((int) event.getX());

        switch (event.getAction() & MotionEvent.ACTION_MASK) {

            case MotionEvent.ACTION_UP:

                break;

            case MotionEvent.ACTION_DOWN:

                initial_X = getUpdated_x();
                break;

            case MotionEvent.ACTION_MOVE:

                if (getUpdated_x() < screenX - ball.getBitmap().getWidth() || getUpdated_x() > screenX - ball.getBitmap().getWidth()) {
                    draw(getUpdated_x());
                }
                break;
        }
        return true;
    }

    private void draw(int updatedValue) {

        canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(Color.RED);
        canvas.drawBitmap(ball.getBitmap(), updatedValue, ball.getBall_y(), paint);
        ball.setBall_x(updatedValue);

        ball_detectCollision.left = ball.getBall_x();
        ball_detectCollision.top = screenY - ball.getBitmap().getHeight() - 260;
        ball_detectCollision.right = ball.getBall_x() + ball.getBitmap().getWidth();
        ball_detectCollision.bottom = screenY - ball.getBitmap().getHeight() - 260 + ball.getBitmap().getHeight();

        for (int i = 0; i < hurdleCount; i++) {
            canvas.drawBitmap(hurdles[i].getBitmap(), hurdles[i].getX(), hurdles[i].getY(), paint);
        }
        surfaceHolder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void run() {

        while (playing) {
            update();
            draw(getUpdated_x());
            control();
        }
    }

    private void update() {

        for (int i = 0; i < hurdleCount; i++) {
            hurdles[i].update();

            if (Rect.intersects(getBall_detectCollision(), hurdles[i].getDetectCollision())) {
                System.out.println("Collision Detected");

                playing = false;
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        showGameOverMessage();

                    }
                });
            }
        }
    }


    public void pause() {

        playing = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void showGameOverMessage() {

        Toast.makeText(getContext(), "Game Over", Toast.LENGTH_SHORT).show();
        Dialog showDialog = new Dialog(getContext());
        showDialog.setContentView(R.layout.dialog_game_over);
        Button playAgainButton = (Button) showDialog.findViewById(R.id.playAgainButton);
        playAgainButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                playing = true;
                thread.start();
            }
        });
        showDialog.show();

    }
}

2 个答案:

答案 0 :(得分:3)

您无法重新启动已停止的主题。解决方案是简单地实例化一个新的并启动它。

答案 1 :(得分:0)

当游戏结束时,只需停止线程并再次播放按钮即可启动新线程。

如果您需要保存以前线程的任何实例,请将它们保存在外部变量中。无论如何,在这种情况下有很多选择。

修改

你完全停止了你的线程。您只需要在启动新游戏时启动新线程。

我建议保留这样的功能并删除implements Runnable

private void startGame() {
    // Start a new game
    playing = true; 

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                while (playing) {
                    update();
                    draw(getUpdated_x());
                    control();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    thread.start();
}