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

2017/5/21

Incomparable Image and String Error

Nameless477 Nameless477

2017/5/21

#
I'm trying to create a 5 Card Draw program. The idea is that when you click a card, it gets marked for swapping out (Discard). The goal of this part is that when i click on a card that is face up (Suit and Number) it should "flip over", or show the back of the card. From there, I want to make it so that if you change your mind and want to keep that card, you can unmark it and flip it back over (face up). Here's what I've got in the act method of the WorldClass
if(Greenfoot.mouseClicked(hand[1])){
           hand[1].getImage();
           if (hand[1].getImage() != "cardback.gif"){
               hand[1].setImage("cardback.gif");
           } else if (hand[1].getImage() == "cardback.gif"){
               hand[1].turnOver();
            }
        }
I had the initial flip over working earlier, though I'm not sure how I did it.. Any help is much appreciated :)
Nameless477 Nameless477

2017/5/21

#
Quick note, turnOver() returns the card to the face up position
Yehuda Yehuda

2017/5/21

#
The getImage() method returns a GreenfootImage not a string, you have to see if the images are the same (and line 2 in the above code won't work - or do anything).
danpost danpost

2017/5/21

#
You might consider keeping the "cardback.gif" image held in a public static final GreenfootImage field in the Card class:
public static final GreenfootImage BACK_IMG = new GreenfootImage("cardback.gif");
and always set the image of a face-down card using
setImage(BACK_IMG);

// or from a different class
hand[1].setImage(Card.BACK_IMG);
and compare using:
if (hand[1].getImage() == Card.BACK_IMG)
or '!='.
Nameless477 Nameless477

2017/5/22

#
Ok, I understand what you're saying. However after changing the original code around with that, it's saying "cannot find symbol - variable BACK_IMG"
public static final GreenfootImage BACK_IMG = new GreenfootImage("cardback.gif");

public void act(){
if(Greenfoot.mouseClicked(hand[1])){
           if (hand[1].getImage() != Card.BACK_IMG){
               hand[1].setImage(Card.BACK_IMG);
           } else if (hand[1].getImage() == Card.BACK_IMG){
              hand[1].turnOver();
           }
        }
    }
Where am I going wrong?
Nameless477 Nameless477

2017/5/22

#
Scratch that, I misread your post. After moving the "public static final" line into the Card class, it's working :D Thanks a million!
Nameless477 Nameless477

2017/5/22

#
Now I have another question, if you don't mind me continuing in this thread. I want to run this same thing for each card, there's 5 in all. I was thinking of some kind of switch setup, replacing each hand(i) with the switch. Is this possible, or will I run into problems due to the hands being part of an array?
Yehuda Yehuda

2017/5/22

#
If you want this to happen for every 'hand', then this code should probably be in the 'hand' class act method, but you can still do it in the World with a for loop.
for (Card card : hand) { // only if the 'hand' array is an array of Cards
    if (Greenfoot.mouseClicked(card)) {
        if (card.getImage() == Card.BACK_IMG) {
            card.turnOver();
        } else {
            card.setImage(Card.BACK_IMG);
        }
    }
}
Nameless477 Nameless477

2017/5/22

#
I tried playing around with it, but it appears I don't have the 'hand' array set up the way it should be/ in a way that works here. in the world class:
private Card [] hand=new Card[5];
and in the Deck class:
private Card[] theDeck=new Card[52];
again, not sure how I would set it up to work with your code. I tried replacing instances of "card" with hand(i) (using brackets) but that seemed to freeze up greenfoot..
Nameless477 Nameless477

2017/5/22

#
aha! placing the code in the act method of the Card class achieves the desired results. Thank you :)
You need to login to post a reply.