Question

I am trying to use the Libgdx Scene2dui library and want to catch the mouse touchdown event. I have a custom Actor and a dialog that instantiates an instance of the Actor and tries to listen for touch events. For some reason the touch events are not getting caught.

public class MMADialog136WhiteCanvasActor extends Actor {

    public MMADialog136WhiteCanvasActor () {
        super();
        setWidth(256);
        setHeight(256);
        setBounds(0, 0, getWidth(), getHeight());
        mMyRenderer = new ImmediateModeRenderer10();
    }

   @Override
    public Actor hit(float arg0, float arg1, boolean flag) {
        return null;
        }

    @Override
    public void draw(SpriteBatch batch, float arg1) {
         batch.end();
        Vector3 pos0 = new Vector3(1,1,0);
        Vector3 pos1 = new Vector3(100,100,0);
        mMyRenderer.begin(GL10.GL_LINES);        
        mMyRenderer.color(mColor.r,mColor.g,mColor.b,mColor.a);
        mMyRenderer.vertex(pos0);
        mMyRenderer.vertex(pos1);                                       
        mMyRenderer.end();
         batch.begin();
    }
    private ImmediateModeRenderer10 mMyRenderer;
    private Color mColor = MMAColor.MMAWHITE;
}

And the Actor instance listener...

mWhiteCanvas.addListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        System.out.println("x,y" + x + "," + y);
        return true;
    }
});

Any idea why the InputListener is not catching the event?

My initial thought was the Actor draw space needs bounds, so I added the setBounds() based on predefined width=256 and height=256, but this has no effect.

Was it helpful?

Solution

@Override
public Actor hit(float arg0, float arg1, boolean flag) {
    return null;
}

This is the problem. By always returning null you basically say that this actor is never hit. And it will never receive any enter, click, exit, keydown or other events.

If you want a standard bounding box test, then do this instead:

@Override
public Actor hit(float arg0, float arg1, boolean flag) {
    return super.hit(arg0, arg1, flag);
}

I'd also advice you to get the sources to your code. arg0 doesn't seem to help anyhow and you also don't have any JavaDoc I suppose. Otherwise you would probably know what the hit() method does. Because it is well documented.

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