How do I get a clicked object?


1 2 3 4 | if (Greenfoot.mouseClicked( this )) { //what you want the actor to do when clicked on } |
1 2 3 4 5 | if (Greenfoot.mouseClicked(Card. class )) { Card card = //(get clicked object); currentValue = card.getCurrentValue(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //reference to the card private Card card; //you will most likely replace that with an array of cards if you have multiple ones, but first, let's take it simple //creating the card and adding it to the world addObject(card = new Card(), <x>, <y>); //checking if a click on the card happened if (Greenfoot.mouseClicked(card)) //I personally prefer mousePressed but that's obviously up to you { //… } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //reference to all cards in hand private ArrayList<Card> cardsInHand = new ArrayList<Card>(); //whenever card x moves to the hand cardsInHand.add(x); //whenever card x is removed from the hand cardsInHand.remove(x); //check if any card is clicked for (Card card : cardsInHand) { if (Greenfoot.mouseClicked(card)) { //call card.getCardValue() here and do with it whatever you want } } |