1
\$\begingroup\$

I'm trying to tell if an actor exists at a screen position so I can do something if the actor is clicked on. To do this, I first need to get the screen coordinates of the actor so I can compare it to the coordinates where I click, however, I am having some difficulty. I am currently trying to do this:

Actor actor = ...
// The position where I clicked obtained from touchDown() in my InputAdapter
Vector2 screenPos = ...
Vector2 actorStagePos = actor.localToStageCoordinates(new Vector2(0,0))
Vector2 actorScreenPos = actor.getStage().stageToScreenCoordinates(actorStageCoordinates)
Rectangle actorScreenBounds = new Rectangle(actorScreenPos.x, actorScreenPos.y, actor.getWidth(), actor.getHeight());
if (actorScreenBounds.contains(screenPos))
{
    System.out.println("An actor is there")
}

However, the coordinates don't seem to be converting correctly. They end up slightly below and to the right of where they should be. At first, I thought this was caused because I'm scaling some of the Image actors, but it also did not work when nothing was scaled up.

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you considered just putting an InputListener on the actor? The input listener reacts to the actor's local coordinates, so the touchDown method will not be invoked unless the cursor is within the actor's bounds. \$\endgroup\$ – nhydock Oct 27 '15 at 16:18
  • \$\begingroup\$ Dang I really have a problem with overcomplicating things. I didn't even think of doing this, I was too set on getting it to work the only way I thought it could. Thank you. \$\endgroup\$ – Panda Oct 30 '15 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.