This site requires JavaScript, please enable it in your browser!
Greenfoot back
SpiderStacey
SpiderStacey wrote ...

2016/10/3

How do I know what element of an object array was clicked?

SpiderStacey SpiderStacey

2016/10/3

#
Sorry, I'm new to Greenfoot (and Java for that matter). I have 10 instances of an actor (called Card). I want to know which element or index number was clicked on. Thanks.
danpost danpost

2016/10/3

#
You will need to show what code you are using -- both for the array and how you detect a click on a card. Will need to know what class the code you give is located in and what class that class extends.
SpiderStacey SpiderStacey

2016/10/4

#
It's for a Memory game. whichClick is a variable to track whether it's the first or second card revealed. public class Card extends Actor { int whichClick = 1; public void act() { if (Greenfoot.mouseClicked(null)) { Actor cardClicked = Greenfoot.getMouseInfo().getActor(); if (cardClicked instanceof Card) { if (whichClick == 1) { //reveal card and store value of card clicked on } else if (whichClick == 2) { //reveal second card and compare with first } } } } }
danpost danpost

2016/10/4

#
You need to track more than whether it is the first or second card. You need to keep a reference to the first card itself -- until the second card is chosen, so you can check their matching state. Therefore, instead of an 'int' field, you should use a Card reference field. When its value is 'null', then the first choice is being made; and when its value has a Card reference (the first card), then the second choice is being made. Once the comparison is made between the two cards, whether they match or not, the field can be set back to 'null'. Another thing you should track is whether a card is flipped or not, unless you remove cards that are found to match immediately. That way, you can tell whether the card is a valid choice or not.
SpiderStacey SpiderStacey

2016/10/6

#
I'm still not really getting this. All I want to know is the index number of the card that I clicked on. I have 10 card objects - 0, 1, 2, 3 . . . 9. I just want something to return the value of that index number. Can that not be done in Greenfoot?
danpost danpost

2016/10/6

#
SpiderStacey wrote...
I'm still not really getting this. All I want to know is the index number of the card that I clicked on. I have 10 card objects - 0, 1, 2, 3 . . . 9. I just want something to return the value of that index number. Can that not be done in Greenfoot?
Sure it can:
1
2
3
4
// with the following array (filled with Card objects)
Card[] cards = new Card[10];
// you can do
int index = Arrays.asList(cards).indexOf(clickedCard);
If 'index' is '-1', the clicked object was not in the list; otherwise, the position of the card in the array is set to 'index'.
SpiderStacey SpiderStacey

2016/10/6

#
Thank you very much. This sounds like exactly what I need. Except - what is "Arrays" referring to? I get "cannot find symbol" when I put Arrays, so I assume I have to put something else there.
SpiderStacey SpiderStacey

2016/10/6

#
OK. added java.util. and it worked. Only problem is that no matter what card I click on, index = -1
SpiderStacey SpiderStacey

2016/10/6

#
Got it. Thanks for the help.
You need to login to post a reply.