android - 这是在 LibGdx 中检测矩形触摸的正确方法吗?似乎对我不起作用

标签 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();

    }

我正在使用 libgdx 的动画类将我的气球图像更改为它爆裂的图像。 我是 libgdx 的新手,无法弄清楚我在这里做错了什么。 作为 Actor ,我应该创建一个表格并布置我的气球元素吗?

最佳答案

尝试这样的事情:

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;
}

请阅读此https://stackoverflow.com/a/18555705/2158970

关于android - 这是在 LibGdx 中检测矩形触摸的正确方法吗?似乎对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25460029/

相关文章:

libgdx - 为什么在 libGdx 中 Camera.unproject() 和 Camera.project() 不是彼此相反的?

java - 如何组合 Box2d 物体?

android - 如何安全地在 Android 应用程序中实现登录功能?

android - 无法接收邮件中的附件

android - 在android中的3个选项卡中的每个选项卡的 ListView 中添加不同的项目

java - LibGDX Actor 不会绘制 Action

java - 每隔特定毫秒调用一个方法

android - 发送字节[]到服务器,outOfMemoryError

每行具有不同布局的Android ListView

colors - 如何在libgdx中的纹理上叠加颜色?