Question

Here is the code that is confusing me. I might be missing something here but couldn't figure it out.

public class TStage extends Stage {

    public TStage(float width, float height, boolean stretch) {
        super(width, height, stretch);
    }

    @Override
    public Actor hit(float x, float y) {
        Gdx.app.debug("HUNT", "in hit of TStage");
        return super.hit(x, y);
    }
}

public class TActor extends Actor {

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        // draw something here
    }

    @Override
    public Actor hit(float x, float y) {
        Gdx.app.debug("HUNT", "in hit of TActor");
        return null;
    }
}

    /* Code to set stage*/

    TStage stage = new TStage(Hunt.GAME_WIDTH, Hunt.GAME_HEIGHT, false);
    Gdx.input.setInputProcessor(stage);
    TActor actor1 = new TActor();
    stage.addActor(tactor);

when I touch the screen

output:

in hit of TActor  

what I am expecting:

in hit of TStage  
in hit of TActor 

[edit]
I add following code to TStage class

@Override
    public Actor touchDown(int x, int y, int pointer, int button) {
        Gdx.app.debug("HUNT", "in touchDown of TStage");
        return super.touchDown(x, y, pointer, button);
    }

Now the output is:

in touchDown of TStage  
in hit of TActor 
Was it helpful?

Solution

There's some confusion about which method does what.

The method hit() returns the actor that is at those coordinates. The method you want is touchDown(). There's almost no information in the javadocs, so read this here. You'll see that TActor.hit() is being called because that's how Stage.touchDown() finds the Actor that is at those coordinates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top