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

2016/4/26

Trouble Adding an Object

carter carter

2016/4/26

#
I am making a Memory Game with cards (I am very new to greenfoot). I want it so if two cards are flipped, then an object that says "Correct" pops up, but I get an error when using addObject.
public class Cards extends Actor
{
    public static boolean card1Flipped = false;
    public static boolean card2Flipped = false;
    public static boolean card3Flipped = false;
    public static boolean card4Flipped = false;
     /** Act - do whatever the Cards wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        ifCorrect();
    }
    private void ifCorrect()
    {
        if((card1Flipped = true) && (card2Flipped = true))
        {
        getWorld().addObject(Correct, 289, 190);
        }   
    }
}
The object that says Correct is called correct. I get the error "Cannot find symbol - variable correct." Thank you for your help, and remember I am new to coding so if something is complicated, please explain.
danpost danpost

2016/4/26

#
That is a definition problem. Correct is the name of a class which describes an object; it is a Class object, not a Correct object. You can create multiple Correct objects from the class. An object is created from a class by use of the 'new' keyword placed before the class name which is then followed by a set of parenthesis (round brackets). A Correct object would be created with:
Correct correct = new Correct();
The right side creates the object and it is assigned to a newly declared variable of type Correct called 'correct'.
carter carter

2016/4/26

#
Whenever I have that code in there, it doesn't create the object though.
Super_Hippo Super_Hippo

2016/4/26

#
The line creates an object, it doesn't add it to the world. You could use this line after it:
getWorld().addObject(correct, 289, 190);
Or merge both lines into one:
getWorld().addObject(new Correct(), 289, 190);
carter carter

2016/4/26

#
Can I use this code in the class Cards, or does it have to be in Card1, Card2 etc.?
danpost danpost

2016/4/26

#
carter wrote...
Can I use this code in the class Cards, or does it have to be in Card1, Card2 etc.?
If you have similar members (methods and/or fields) in all the subclasses, then you only need to code it once, in the superclass -- as 'public' or 'protected'. So, yes. For example, your static Boolean fields for card#flipped -- you only need to declare one non-static boolean in the Cards class for that. 'Flipped' is a state of a card and is not something that pertains to all cards as a whole. So, it is the state of an instance of the Cards class. Now, something like the image on the back of the card, which all cards will have when flipped down -- that could be declared static (and final) in the Cards class and used for all the cards. I really do not thing that you should have what the 'act' and 'ifCorrecct' methods are doing in the Cards class, however. The actions taken there really belong in your World subclass (where the game is controlled). You can add a reference field in the world class to hold the first flipped card. If a card is flipped and its value is 'null' it is the first card (set it to the field); otherwise, it is the second card, which can then be compared to the one referenced in the field. Upon any comparison, flip the cards back over if not matched and reset the field back to 'null'.
You need to login to post a reply.