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

2020/3/17

Display text on world from variable on non actor or world class

whattheheck whattheheck

2020/3/17

#
I have a separate class called Heater in my project, and I want to display the int value from the tempValue method as text on my world at a location. I want to be able to update the value if the value changes. How would I set up this? Code: Heater:
public class Heater  
{
    //temperature variable
    int temp;
    
    /**
     * Constructor for objects of class Heater
     */
    public Heater() {
        temp = 15; //initialize temp to 15
    }
    void warmer() {
        this.temp += 5; //add 5 to temp
    }
    void cooler() {
        this.temp -= 5; //subtract 5 from temp
    }
    public int tempValue() {
        return this.temp; //return value of temp
    }
}
MyWorld: public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); } }
danpost danpost

2020/3/17

#
Where is the code that creates the Heater object the world is to get the value from?
whattheheck whattheheck

2020/3/17

#
It is:
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        Heater heater = new Heater();
    }
}
danpost danpost

2020/3/18

#
Move line 12 to line 3.
You need to login to post a reply.