怎么可以忽略TouchEvents?

时间:2017-03-07 12:54:54

标签: android surfaceview ontouchevent

我的应用程序是一个小跳跃和运行游戏。你死的那一刻,活动就会休息1秒。如果我在那个时候触摸屏幕(以便OnTouchEvent被触发)我得到错误"不幸的是应用已经停止"。 有没有办法忽略触摸事件?

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
    private MainThread thread;
    private Background bg;
    private Player player;
    MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.background_sound);
    MediaPlayer copter = MediaPlayer.create(getContext(), R.raw.helicoptersound);

    public GamePanel(Context context) {
        super(context);
        getHolder().addCallback(this);
        setFocusable(true);
    }

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

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        setWillNotDraw(false);
        bg = new Background(BitmapFactory.decodeResource(getResources(), R.raw.grassbg1), 900);
        player = new Player(BitmapFactory.decodeResource(getResources(), R.raw.helicopter), 132, 47, 3);
        thread = new MainThread(getHolder(), this);

        // start game loop
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mp.release();
        copter.release();
        while (true) {
            try {
                thread.setRunning(false);
                thread.join();
                thread = null;
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (((Activity)getContext()) != null) {
            if (event.getAction() == MotionEvent.ACTION_DOWN && fuel > 0) {
                if (!player.getPlaying()) {
                    player.setPlaying(true);
                    player.setUp(true);
                }
                else {
                    player.setUp(true);
                }
                return true;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                copter.pause();
                player.setUp(false);
                return true;
            }

        }
        return super.onTouchEvent(event);
    }

    public void update() {
        postInvalidate();

        if (player.getPlaying()) {
            bg.update();
            player.update();

            // check if player out of range
            if (player.getY() > HEIGHT || player.getY() < 0) {
                player.setPlaying(false);
                newGame(score);
            }
        }
    }

    public boolean collision(GameObject a, GameObject b) {
        if (Rect.intersects(a.getRectangle(), b.getRectangle())) {
            return true;
        }
        return false;

    }

    @Override
        public void draw(Canvas canvas) {
        if (canvas != null) {
            bg.draw(canvas);
            player.draw(canvas);

        }
    }

    public void newGame(int score) {
        mp.release();
        copter.release();

        try {
            thread.setRunning(false);
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

        Bundle basket13 = new Bundle();
        basket13.putInt("key2", score);
        Context context = getContext();
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtras(basket13);
        context.startActivity(intent);
        ((Activity)context).finish(); // letztes was ausgeführt wird
    }
}

0 个答案:

没有答案