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

2016/4/26

Changing a Boolean value

carter carter

2016/4/26

#
So if I have:
    public static boolean card1Flipped = false;
How can I change the value to true if an object named card1 is clicked?
danpost danpost

2016/4/26

#
As an instance boolean field (maybe called 'showing'), it (the field), would be easy to change when needed. However, you can use the image of any card to determine its flipped state:
/** in Cards class */

// class field
public static final GreenfootImage back = new GreenfootImage("redback.png");

// instance field
private GreenfootImage face;

// in constructor
setImage(back);
face = new GreenfootImage(<? to be determined by card value and rank ?>);

// a method to flip the card
public void flip()
{
    if (getImage() == back) setImage(face); else setImage(back);
}
The flip method uses the current flipped state to determine the new image. From a different class, you can still use this:
if (someCard.getImage() == Cards.back)
to check if card is not flipped (of course, 'someCard' would have to refer to a Card object).
carter carter

2016/4/26

#
If I try
if (Card1.getImage() == Card1.front)
I get the error: "non-static method getImage() cannot be referenced from a static context
danpost danpost

2016/4/26

#
carter wrote...
If I try
if (Card1.getImage() == Card1.front)
I get the error: "non-static method getImage() cannot be referenced from a static context
That is correct. Only the 'back' image can be taken from the classes. However, that is not a problem as you can ask if it is not the 'back' image (if it is not the back one, then it must be the front one):
if (someCard.getImage() != Cards.back)
Again, 'someCard' has to be a reference to an actor object of Cards type -- not a class name. Where are you trying to use the line and what code is within the method that that line is in?
carter carter

2016/4/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Card1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Card1 extends Cards
{ private GreenfootImage front = new GreenfootImage("images-1_burned.png");
  private GreenfootImage back = new GreenfootImage("card1Back.jpg");
    /**
     * Act - do whatever the Card1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        changeImage();
        ifCorrect();
        
    }    
    public void changeImage()
        {
            if(Greenfoot.mouseClicked(this)) 
          
            if (getImage() == front)
            {
                setImage(front);
            }
            else
            {
                setImage(back);
            }     
    }
    private void ifCorrect()
    {
        if((Card1.getImage() == front) && (Card1.getImage() == front))
        {
        Correct correct = new Correct();
        getWorld().addObject(correct, 289, 190);
    }
    }
}



There's all my code. I still get the same error when using the code you provided
danpost danpost

2016/4/26

#
Do you use more than one image for the back of the cards or are all the backs of the same image?
carter carter

2016/4/26

#
Every card has its own image for the back
danpost danpost

2016/4/26

#
carter wrote...
Every card has its own image for the back
Then you cannot do it the way I suggested above. You will need a boolean field; but again, it should be an instance field (not a static field for each card). In fact, maybe it would be easier to put the two images for each card in an array and use an int field to point (as an index for the array) to the current image:
/** in Cards class */
protected Greenfootimage[] images = new GreenfootImage[2];
protected int imagesIndex;

public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        imagesIndex = (imagesIndex+1)%2; // alternates in values between zero and one
        setImage(images[imagesIndex]);
        if (imagesIndex == 1) getWorld().addObject(new Correct(), getX(), getY()+100);
    }
}

/** in Card1 class (and similar in others) */
public Card1()
{
    images = { new GreenfootImage("card1Back.jpg"), new GreenfootImage("images-1_burned.png") };
}
You will need to remove all previous imaging codes (fields and methods). In fact, the only thing that should be in the Card1, Card2, etc. classes is a constructor like the one above ('public Card1()').
You need to login to post a reply.