这是检测LibGdx中矩形触摸的正确方法吗?似乎不适合我

Nit*_*ain 8 android libgdx

这是我的游戏屏幕的代码,我想在触摸时爆破我的气球.方向是肖像.但它似乎对我不起作用.

 public class GameScreen implements Screen {
    final BB game;
    private BitmapFont font;
    private static final int no_of_frames = 2;
    Texture ballonFrames;
    TextureRegion[] burstFrames = new TextureRegion[no_of_frames];
    Animation burstAnimation;
    Array<Rectangle> ballons;
    TextureRegion currentFrame;
    long lastBallonTime;
    int ballonBursted;
    OrthographicCamera camera;
    int ballonMissed;

    Sound ballonBursting;

    public GameScreen(final BB gam) {
        this.game = gam;

        ballonFrames = new Texture(Gdx.files.internal("ballon_burst.png"));
        font = new BitmapFont(Gdx.files.internal("font.fnt"), false);
        ballonBursting = Gdx.audio.newSound(Gdx.files
                .internal("BallonBursting.wav"));
        TextureRegion[][] tmp = TextureRegion.split(ballonFrames,
                ballonFrames.getWidth() / 2, ballonFrames.getHeight());
        burstFrames[0] = tmp[0][0];
        burstFrames[1] = tmp[0][1];

        burstAnimation = new Animation(3.0f, burstFrames);
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

        ballons = new Array<Rectangle>();

        spawnBallon();

    }

    private void spawnBallon() {
        Rectangle ballon = new Rectangle();
        ballon.x = MathUtils.random(0, 800 - 64); //
        ballon.y = 0;
        ballon.width = 40;
        ballon.height = 80;
        ballons.add(ballon);
        lastBallonTime = TimeUtils.nanoTime();
    }

    private boolean ballonBursted(Rectangle ballon) {
        Vector2 touch = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        if (ballon.contains(touch))
            return true;
        else
            return false;
    }

    @Override
    public void render(float delta) {
        // TODO Auto-generated method stub
        Gdx.gl.glClearColor(0, 0, 0.3f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        InputProcessor processor;

        camera.update();
        game.batch.setProjectionMatrix(camera.combined);
        game.batch.begin();
        font.draw(game.batch, "Ballon Bursted :" + ballonBursted, 0, 700);
        font.draw(game.batch, "Ballon Missed:" + ballonMissed, 275, 700);
        for (Rectangle ballon : ballons) {
            game.batch.draw(burstFrames[0], ballon.x, ballon.y);
        }

        if (TimeUtils.nanoTime() - lastBallonTime > 1000000000) {
            spawnBallon(); // a ballon every second
        }

        Iterator<Rectangle> iter = ballons.iterator();
        while (iter.hasNext()) {
            Rectangle ballon = iter.next();
            ballon.y = ballon.y + 100 * Gdx.graphics.getDeltaTime();

            if (ballonBursted(ballon) == true) {
                ballonBursted++;
                game.batch.draw(burstFrames[1], ballon.x, ballon.y);
                ballonBursting.play();
                iter.remove();
            }
            else if (ballon.y + 64 > 800) {
                iter.remove();
                ballonMissed++;
            }
        }

        if (ballonMissed > 5) {
            game.setScreen(new ScoreScreen(game, ballonBursted));
        }
        game.batch.end();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        ballonFrames.dispose();
        ballonBursting.dispose();
        game.batch.dispose();

    }
Run Code Online (Sandbox Code Playgroud)

我正在使用libgdx的动画类来改变我的气球图像到它爆裂的那个.我对libgdx很新,无法弄清楚我在这里做错了什么.我应该创建一个表格并将我的气球元素布局为演员吗?

Yur*_*raj 11

尝试这样的事情:

private boolean ballonBursted(Rectangle ballon) {
        Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);

        if (ballon.contains(touchPos.x, touchPos.y))
            return true;
        else
            return false;
}
Run Code Online (Sandbox Code Playgroud)

请阅读此/sf/answers/1298899381/