1
\$\begingroup\$

I have a class Card with the method setOwner. I have set up my touch input to use a hitActor to identify touches by getting the Actor's name like so :

public boolean touchDown(int screenX, int screenY, int pointer, int button) {
   Vector2 hitCoord = stage.screenToStageCoordinates(new Vector2((float) screenX, (float) screenY));
   Actor hitActor = stage.hit(hitCoord.x, hitCoord.y, false);
    if (hitActor != null) {
            stage.getRoot().findActor(hitActor.getName()).setOwner();// here it doesnt identify the setOwner() method;
    }
    return false;
}

The problem is that it doesn't identify the method setOwner(). My Card actors are in a ListArray deck so I type casted them like so :

  for (Card c : deck) {
        c = (Card) stage.getRoot().findActor(c.getName() + c.getSuit());
    }

But still it doesn't identify the methods declared in the Card class. My goal is to call the methodsetOwner on the Actor that got touched.

\$\endgroup\$
  • \$\begingroup\$ Perhaps you should simply attach listeners to the actors instead of the stage. \$\endgroup\$ – Wabbitseason Oct 28 '16 at 12:11
0
\$\begingroup\$

I am assuming your Card class extends Actor, but since setOwner only exists on Card you can't call it without casting the Actor to a Card first.

Also, since the hit test returns an Actor you shouldn't have to ask the Stage for it by name using the findActor method.

Consider changing your code to something like this;

if (hitActor != null && hitActor instanceof Card) {
    ((Card)hitActor).setOwner ();
}
\$\endgroup\$
  • \$\begingroup\$ It worked ! Could you please point me to some read material ,so that I can understand what happens better? @bornander \$\endgroup\$ – SlavenIvanov Oct 28 '16 at 12:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.