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

2020/7/17

How to check for other object's variables in a different class?

Falafel_ Falafel_

2020/7/17

#
if(karotteAtLocation != null && karotteAtLocation.isEaten == false) 
The Error is karotte.isEaten, the error being 'cannot find symbol - variable isEaten. I'm trying to change specifically the variable of the karotte i'm touching. So i'm trying to check if isEaten (in the class karotte) is true from the class Hase2. isEaten is set to false by default and must be able to be different in every object of the class, as i'm trying to change isEaten of only karotte that is eaten. Removing the eaten karotte isn't an option as I have an array that would break otherwise, setting isEaten to static doesn't work because it needs to change for every object. What do I need to change to make my code work? More code:
 
Actor karotteAtLocation = getOneObjectAtOffset(0, 0, karotte.class);

        if(karotteAtLocation != null && karotte.isEaten == false) 
{
            karotteAtLocation.setImage("null.png");
            karotteAtLocation.isEaten = true;
}
danpost danpost

2020/7/17

#
I am a bit confused here. What does your Actor class tree look like? Is karotte a child class of Hase?
Falafel_ Falafel_

2020/7/17

#
Sorry if i was unclear. So I have Hase2 and karotte, both actors. The code is in Hase2 and i'm trying to change a variable in karotte.
danpost danpost

2020/7/17

#
Falafel_ wrote...
Sorry if i was unclear. So I have Hase2 and karotte, both actors. The code is in Hase2 and i'm trying to change a variable in karotte.
Show all codes in karotte class (from 1st import to end);
Falafel_ Falafel_

2020/7/17

#
Ok here it is:
import greenfoot.*; 

public class karotte extends Actor
{
    int x;
    int y;
    int rotation;
     public boolean isEaten = false;
    public karotte()
    {
            rotation = 0;
    }
    
    int rotationGeben()
    {
        return rotation;
    }
    
    public void act() 
    {
          x = getX();
          y = getY();
         if(rotation == 315)
{ 
// The rest is just to make the carrot wiggle 
}
}
My problem being It doesn't recognize any variable of karotte except for pre-defined ones such as setX(), setImage(), etc.
danpost danpost

2020/7/17

#
Oh, crap. I just realized what was going on. The getOneObjectAtOffset method returns an Actor reference. However, the field is not in the Actor (or Object) class -- it is in the karrotte class. Use:
if (karrotteAtLocation != null && ((karrotte)karroteAtLocation).isEaten == false)
or
if (karrotteAtLocation != null && !((karrotte)karroteAtLocation).isEaten)
Falafel_ Falafel_

2020/7/18

#
Thanks! It works now. Just for clarification: by adding the ((karotte)...)... it makes the game look in karotte class for an object, instead of in the actor class for one? Also, why do pre-defined methods such as setImage() work without the change?
danpost danpost

2020/7/18

#
Falafel_ wrote...
Just for clarification: by adding the ((karotte)...)... it makes the game look in karotte class for an object, instead of in the actor class for one?
Well, it will be looking for a variable or method (not an object); but, yes, the change will allow it to also look in the karotte class for it (in this case, the 'boolean isEaten' variable.
Also, why do pre-defined methods such as setImage() work without the change?
The 'setImage' method is defined in the Actor class. So, it can be invoked on any actor assigned to a reference of type Actor (or child of Actor). The following will fail on line 2:
Object carrot = new karotte();
carrot.setImage(new GreenfootImage(1, 1));
Because 'carrot' is a reference of type Object, (from line 1) the 'setImage' method will not be found. However, this will work for line 2:
((Actor)carrot).setImage(new GreenfootImage(1, 1));
as well as this:
((karotte)carrot).setImage(new GreenfootImage(1, 1));
Falafel_ Falafel_

2020/7/18

#
Ok, that makes sense. Thanks!
You need to login to post a reply.