0

I've created a simple test with a touchDown() event on an image actor. The event works on the stage but not on the actor. Here is the code:

I set the InputProcessor in the constructor of the parent class (AbstractScreen).

Gdx.input.setInputProcessor(this.stage);

In the sub class:

private TextureAtlas atlas;
private Image boardImg;

public void show(){
    super.show(); 

    atlas = new TextureAtlas(Gdx.files.internal("data/packs/mainMenu.atlas"));      
    AtlasRegion board = atlas.findRegion("board");

    boardImg = new Image(board);

    boardImg.addListener(new InputListener(){
           public boolean touchDown(InputEvent event, float x, float y, int pointer, int    button){
               Gdx.app.log("aaaa", "down");
               return true;
           }
    });

    stage.addActor(boardImg);

    stage.addListener(new InputListener() {
          public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
               Gdx.app.log("aaaa", "stage down");
               return true;
          }
    });     
}


@Override
public void resize(int width, int height) {
    super.resize(width, height);        
    boardImg.setPosition(stage.getWidth()/2 - boardImg.getWidth()/2, stage.getHeight()/4);  
}

I'm testing both on the desktop and on the android (same results). I get the "stage down" event but not the event of the actor. I've tried also without having the stage event.

Daahrien
  • 10,190
  • 6
  • 39
  • 71
Drorasta
  • 141
  • 2
  • 5
  • I can't find any clear doc about this, but does it work if your stage's listener returns "false" instead of "true"? (Its not clear to me what the return value really does ...) – P.T. Nov 09 '12 at 15:54
  • The problem happened because I've added another transparent image on top of the boardImage so it covered the touch area of the boardImage. I've mistakenly omitted this image from the question because I wanted to make the code shorter, sorry guys. I've got an answer in the badlogic libgdx forum suggesting to hide the top transparent images from being touchable. topmage.setTouchable(Touchable.disabled); – Drorasta Nov 16 '12 at 13:39

3 Answers3

0

Do all of these works:

  1. boardImg.setTouchable(Touchable.enabled)

  2. return false in stage listener or clear its listener.

  3. do not call setInputProcessor(this.stage) in parent class instead call in child class.

Aliaaa
  • 1,590
  • 2
  • 16
  • 37
0

I think the problem is because the touchDown() method of the stage returns true, and so the event will be "consumed" and won't be propagated to its children, the actors, and so the touchDown() method of boardImg will not be called.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
0

Image doesn't receive touch events because it has no size.

Read documentation carefully:

Note that the actor must specify its bounds in order to receive input events within those bounds.

Just know that boardImg = new Image(board) won't set width and height of actor for you. So you need to make it manualy. Assume you work with pixel perfect sizes:

boardImg = new Image(board);
boardImg.setWidth(board.getWidth());
boardImg.setHeight(board.getHeight());

That will do the trick. Good luck.

Metaphore
  • 749
  • 1
  • 7
  • 15