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

2019/5/17

How can I have a value across every last world and actor?

DJSpicyDeluxe DJSpicyDeluxe

2019/5/17

#
In a Greenfoot scenario in Java, how can I have a value stay consistent across every single actor and world class (class, not just the objects)? Some of them would have to be defined by the player during runtime.
danpost danpost

2019/5/17

#
Using the static modifier on a field will cause it to associated with the class rather than to any object (world or actor). For constant values, use final as well:
public static final int WIDE = 600, HIGH = 400;
In a MyWorld class, this line can be used for the dimensions of all MyWorld objects:
public MyWorld()
{
    super(WIDE, HIGH, 1);
}
Declared public, as above, these values can be accessed from anywhere within your project using the name of the class where they are located:
int worldWidth = MyWorld.WIDE;
int worldHeight = MyWorld.HIGH;
Constant fields (those declared final), by convention, are given names in all uppercase characters. Please note that any non-final (variable) static field will retain its current value when resetting the scenario (only re-compiling will reset the field to its initial value). So, your initial world should ensure it is set appropriately at the start of a run.
DJSpicyDeluxe DJSpicyDeluxe

2019/5/20

#
Thank you very much danpost, this is what I will do!
You need to login to post a reply.