Reputation: 449
I've added InputListener
to my actor and now I want to check if touchUp
event is inside the actor.
Simple example: I'm starting draging mouse inside my actor and i'm finishing outside my actor.
I though that touchUp
event will start only if mouse is inside my actor but it also start outside my actor (when touchDown
event start inside my actor).
How to check if touchUp
event is inside my actor only?
Upvotes: 0
Views: 584
Reputation: 8584
Sorry miss read your question noticed when you changed it.
Still, I would add a listener and simply check coordinates of actor. The x and y given with a clicklistener just return local coordinates of the actor so a simple check vs width and height is enough.
ClickListener cl = new ClickListener()
{
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
if (x > 0 && y > 0 && x < getWidth() && y < getHeight())
{
System.out.println("Released within actor");
}
}
};
actor.addListener(cl);
Upvotes: 0
Reputation: 13581
I see two solutions here:
To use some flag to check if the pointer is inside the actor and handle it with exit method:
image.addListener(new InputListener(){
boolean touched = false;
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
touched = true;
System.out.println("TOUCH DOWN");
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
if(touched)
{
touched = false;
System.out.println("TOUCH UP");
}
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor)
{
touched = false;
}
});
To check if pointer is inside the actor inside touchUp
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button)
{
Stage stage = event.getTarget().getStage();
Vector2 mouse = stage.screenToStageCoordinates( new Vector2(Gdx.input.getX(), Gdx.input.getY()) );
if(stage.hit(mouse.x, mouse.y, true) == event.getTarget())
{
System.out.println("TOUCH UP");
}
}
Both solutions need some extra code but both should be working fine.
Upvotes: 1