Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the right way to detect a touch on a rectangle in LibGdx ? Does not seem to be working for me

Tags:

android

libgdx

This is the code for my gamescreen where i want burst my ballon when its touched . orientation is portrait. but it does not seem to work for me.

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

    }

I am using animation class of libgdx to change my image of ballon to the one where its bursted . I am fairly new to libgdx and unable to figure out what wrong am i doing here . Should i create a table and layout my ballon elements as actor?

like image 811
Nitin Jain Avatar asked Aug 23 '14 08:08

Nitin Jain


2 Answers

Try something like this:

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

please read this https://stackoverflow.com/a/18555705/2158970

like image 115
Yuraj Avatar answered Oct 08 '22 09:10

Yuraj


If I understand it right you just want to know if the touchpoint is contained in the Rectangle ballon. Then you could use Rectangle#contains() method:

 ballon.contains(Gdx.input.getX(), Gdx.input.getY());

see also the source code of Rectangle class

like image 1
donfuxx Avatar answered Oct 08 '22 11:10

donfuxx