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

2016/5/15

How to make a variable accessible in all classes

TSTemple TSTemple

2016/5/15

#
I have a variable that needs access throughout all classes it is declared in class Buttons but i can't get MaleButton and FemaleButton to access it (they both extend from Buttons) do i have to make it a public variable or?
TSTemple TSTemple

2016/5/15

#
    private int Male = 0;
    private int Female = 0;
that's what i used to declare it in class Buttons
if (Greenfoot.mouseClicked(this))
        {
            Male++;
            if(Male != 1)
            {
                getWorld().removeObject(this);
            }    
        }
that's what i have in class MaleButton
if (Greenfoot.mouseClicked(this))
 
        {
            Female++;
            if(Female != 1)
            {
                getWorld().removeObject(this);
            }
        }
and thats what i have in femaleButton could anyone help?
Meh Meh

2016/5/15

#
in class Buttons:
protected int Male = 0;
protected int Female = 0;
protected is like public, but only subclasses have access to the variable
TSTemple TSTemple

2016/5/15

#
Meh wrote...
in class Buttons:
protected int Male = 0;
protected int Female = 0;
protected is like public, but only subclasses have access to the variable
the problem is i also need to access it in an if statement in another class that doesnt extend from Buttons
danpost danpost

2016/5/15

#
Why do you want the buttons to remain in the world unless clicked on a second time?
TSTemple TSTemple

2016/5/15

#
danpost wrote...
Why do you want the buttons to remain in the world unless clicked on a second time?
They aren't meant to is there a way to fix that? Edit: yeh i just saw the problem nevermind :)
danpost danpost

2016/5/15

#
TSTemple wrote...
< Quote Omitted > They aren't meant to is there a way to fix that? Edit: yeh i just saw the problem nevermind :)
As is, before a button is clicked, you have two Male fields and two Female fields -- one Male and one Female in each button. Changing the value of one in one button does not change the same value in the other. Also, once both buttons are removed, you will no longer have any Male or Female fields to access. This, however, is not really an issue as you have other conditions you can use to acquire the same value the field would currently hold. Just the fact that the button is no longer in the world is enough to know that it was clicked and the value would have been set to one. In other words, the fields are not even needed. You can have simple MaleButton and FemaleButton classes -- for example:
public class MaleButton extends Buttons {}
would be the entire MaleButton class, and instead of something like this
if (Male == 1) // which needs to acquire the button object to get access to the Male field
// you would be using something like this
if (getWorld().getObjects(MaleButton.class).isEmpty())
The line is a bit longer, but no field is required which needs to somehow be accessed. It is important to note that anytime you can use current state to get information rather than using a field, you should do so. The main thing is that you would be getting absolute data instead of recorded data, which could be very easily improperly maintained.
TSTemple TSTemple

2016/5/15

#
danpost wrote...
TSTemple wrote...
< Quote Omitted > They aren't meant to is there a way to fix that? Edit: yeh i just saw the problem nevermind :)
As is, before a button is clicked, you have two Male fields and two Female fields -- one Male and one Female in each button. Changing the value of one in one button does not change the same value in the other. Also, once both buttons are removed, you will no longer have any Male or Female fields to access. This, however, is not really an issue as you have other conditions you can use to acquire the same value the field would currently hold. Just the fact that the button is no longer in the world is enough to know that it was clicked and the value would have been set to one. In other words, the fields are not even needed. You can have simple MaleButton and FemaleButton classes -- for example:
public class MaleButton extends Buttons {}
would be the entire MaleButton class, and instead of something like this
if (Male == 1) // which needs to acquire the button object to get access to the Male field
// you would be using something like this
if (getWorld().getObjects(MaleButton.class).isEmpty())
The line is a bit longer, but no field is required which needs to somehow be accessed. It is important to note that anytime you can use current state to get information rather than using a field, you should do so. The main thing is that you would be getting absolute data instead of recorded data, which could be very easily improperly maintained.
i used this method and put it into code like so
public class FemaleButton extends Buttons
{
    /**
     * Act - do whatever the FemaleButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))
        {
            getWorld().removeObject(this);
        }
        if (getWorld().getObjects(MaleButton.class).isEmpty())
        {
            getWorld().removeObject(this);
        }
    }    
}
and
public class MaleButton extends Buttons
{
    /**
     * Act - do whatever the MaleButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))
        {
                getWorld().removeObject(this);
        }
        if (getWorld().getObjects(MaleButton.class).isEmpty())
        {
            getWorld().removeObject(this);
        }
    }    
}
but it threw an error with
java.lang.NullPointerException
	at MaleButton.act(MaleButton.java:21)
	at greenfoot.core.Simulation.actActor(Simulation.java:604)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
	at greenfoot.core.Simulation.runContent(Simulation.java:221)
	at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2016/5/15

#
Maybe you misunderstood. It might be that because I missed one change that you needed to make, you tried to fix it on your own. To be clear, do this:
// change your MaleButtton class to this:
public class MaleButton extends Buttons {}

// change your FemaleButton class to this:
public class FemaleButton extends Buttons {}

// change your Buttons class to this:
import greenfoot.*;

public class Buttons extends Actor
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) getWorld().removeObject(this);
    }
}
Then in other classes, when you need to check to see if a particular button was clicked on, use:
// in World subclass
if (getObjects(MaleButton.class).isEmpty())

// in Actor subclass
if (getWorld.getObjects(MaleButton.class).isEmpty())
You need to login to post a reply.