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

2013/3/16

Accessing A Boolean From Another Class

al_griff al_griff

2013/3/16

#
Is this even remotely possible while sticking to the conventions of object oriented programming? What I'm trying to do is access a boolean that is in my obstacle class, which is set when a certain image is set (if the object is on), so that if the player interacts with the object when it's 'on' it will call upon the death method in my player class. I don't know if you need any of the code, but if you do, just say so.
Gevater_Tod4711 Gevater_Tod4711

2013/3/16

#
Using the method getOneIntersectingObject you can get a reference to an object that is intersecting your actor. With this reference you can easily get the boolean of this actor.
//if your boolean is declared public you can just get it's value like this:
Actor actor = getOneIntersectingObject(Actor.class);//instead of all the Actor classes you have to use the name of the class you want to find;
if (actor != null) {
    if (actor.boolean) {//instead of boolean you have to use the name of the boolean;
        //do whatever you wanna do if the boolean you was searching for is true;
    }
}
If the boolean is declared private (what is the normal way in object oriented programming) you have to use methods to get and set it's value (therefore this methods are called getter and setter).
//in your class which contains the boolean;

private boolean aBoolean = false;

public boolean getABoolean() {
    return aBoolean;
}
public void setABoolean(boolean aBoolean) {
    this.aBoolean = aBoolean;
}
Then you can get (or set) the value of this boolean from your other class like the way I showed you before. You first have to get a reference to the object. The only thing that is different is that you don't get the value of the boolean like before:
if (actor.aBoolean) {
    //...
}
but:
if (actor.getABoolean()) {
    //...
}
al_griff al_griff

2013/3/22

#
So I went about implementing this, and the first solution didn't work however, I got no errors. So I went about setting the boolean to private and then accessing it the way that you mentioned. After then trying to access it in my player class I got the error 'non-static variable killMode cannot be referenced from a static context' (killMode is the name of my boolean) Any thoughts? Would you also be able to explain what the error means? I'm relatively new to Greenfoot and programming in general.
danpost danpost

2013/3/22

#
The error message means you are trying to access a method or field that belongs to an object by using the class name of the object. As an example, let us say you have a class 'Apples' and a boolean 'hasWorm'; then you are trying to ask Apples.hasWorm which really does not make any sense. Any object of the class (any apple) may or may not have a worm, but asking if the class 'Apples' has a worm is not what you are trying to do. What you can do, since you are relying on an intersecting of the two actors is, in the class of the player, either in the 'act' method or a method it calls (assuming your obstacle class is called 'Obstacle'):
Obstacle obstacle = (Obstacle) getOneIntersectingObject(Obstacle.class);
if (obstacle != null && obstacle.killMode)
{
    // code to kill
}
You need to login to post a reply.