Peter Poliwoda
Peter Poliwoda

Reputation: 699

Is there a method that checks if an libgdx Scene2d Image or Actor is touched?

here's my sample from the libgdx android game that I want to create. Nothing special yet, cause I'm just starting my adventure with Android games.

Here is a couple of questions I would like to get answered

So far I am using the Gdx.input.isTouched function and check if that matches the bunny coordinates, I do that for each bunny and at the end I would like to have 11 of them and that's just too much to write. Is there a way to check if the Image class from import com.badlogic.gdx.scenes.scene2d.ui.Image; was touched? Or else does the Actor class has that feature?

Another thing is that when I instantiate the bunny class and I click the bunny, all of them are changing to "scared". It's because the Scene2d.Image texture has to be static. Is there a way to change that?

if(Gdx.input.isTouched()){  

            x = Gdx.input.getX();  
            y = Gdx.graphics.getHeight() - Gdx.input.getY();


         // **** Show Coordinates **** \\ 
            if (x < 420)
                font.draw(batch, "x:" + x + 
                    "\n y: " + y, x, y);
            else
                font.draw(batch, "x:" + x + 
                        " y: " + y, x-65, y);
         // **** End Show Coordinates **** \\

            //if krolik (bunny) is touched add highscore and change texture to scared
            if (x >= krolik.pos.x && y >= krolik.pos.y
                    && x <= krolik.pos.x + 64 && y <= krolik.pos.y + 64)
            {   
                krolik.scared();
                highscore+=100;
            }
            else if (x >= krolik2.pos.x && y >= krolik2.pos.y
                    && x <= krolik2.pos.x + 64 && y <= krolik2.pos.y + 64)
            {   
                krolik2.scared();
                highscore+=100;
            }
       } 
        else{

        krolik.normal();

        }
        // **** Show Highscore **** \\ 
        font.draw(batch, "Highscore: " + highscore, 350, 300);
        batch.draw(krolik.getTexture(), krolik.pos.x, krolik.pos.y);
        batch.draw(krolik2.getTexture(), krolik2.pos.x, krolik2.pos.y);
            batch.end();

Upvotes: 2

Views: 3497

Answers (1)

broken-e
broken-e

Reputation: 840

Why is it that you are using Image but not drawing with Stage? Image and other Actors are for using Stage which can take care of your touch events and drawing very easily.

Create them like this:

for (int i = 0; i < KROLIKS_COUNT; i++) {
    final Image krolik = new Image(nonScaredTextureRegion);
    krolik.setClickListener(new ClickListener() {
        @Override
        public void click(Actor actor, float x, float y) {
            krolik.setRegion(scaredTextureRegion);
            highscore+=100;
        }
    });
    krolik.x = startPosX;
    krolik.y = startPosY;
    stage.addActor(krolik);
}

set the stage as the input processor:

Gdx.input.setInputProcessor(stage);

then in render, simply:

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();

Upvotes: 5

Related Questions