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

2021/10/6

using a variable from another class in the world class

1
2
3
danpost danpost

2021/10/9

#
You assigned the WeatherInput object in the MainMenu class. All you have done in the BeeWorld object is declared a field to hold a WeatherInput object, which needs to be assigned the same object.
xixEmilyxix xixEmilyxix

2021/10/9

#
oh so because i used WeatherInput WeatherInput = new WeatherInput (); in MainMenu i dont need to in BeeWorld? Still not sure on how to transfer this into BeeWorld though
danpost danpost

2021/10/9

#
xixEmilyxix wrote...
oh so because i used WeatherInput WeatherInput = new WeatherInput (); in MainMenu i dont need to in BeeWorld? Still not sure on how to transfer this into BeeWorld though
Something like this (?? in StartButton ??):
BeeWorld beeWorld = new BeeWorld();
beeWorld.weatherInput = ((MainMenu)getWorld()).weatherInput;
Greenfoot.setWorld(beeWorld);
xixEmilyxix xixEmilyxix

2021/10/10

#
i added this so it now looks like this
    public void act() 
    {
        //Set image of button and when pressed change the screen
        
        if(Greenfoot.mouseClicked(this))
        {
          BeeWorld beeWorld = new BeeWorld();
          beeWorld.WeatherInput = ((MainMenu)getWorld()).WeatherInput;
          Greenfoot.setWorld(beeWorld);
        }
    }
and it still doesnt seem to be reading the weather input as it just does the lowTemperature simulation always
danpost danpost

2021/10/10

#
xixEmilyxix wrote...
it still doesnt seem to be reading the weather input as it just does the lowTemperature simulation always
Remove the first word from line 31 here.
xixEmilyxix xixEmilyxix

2021/10/12

#
I did this and it still is not working. It doesn't change based on the weather input, also it has made it so my other objects do not do anything when clicked now - a help menu button and add day button
danpost danpost

2021/10/12

#
xixEmilyxix wrote...
it has made it so my other objects do not do anything when clicked now - a help menu button and add day button
This change is not the cause of how other objects do (or not do) anything).
I did this and it still is not working. It doesn't change based on the weather input
Then something elsewhere is amiss. Did you add any implementation (code) to the regularTemperature and highTemperature methods?
xEmilyx xEmilyx

2021/10/12

#
    public void regularTemperature()
    {
        //add flowers to the bee world
        PinkFlower PinkFlower = new PinkFlower ();
        YellowFlower YellowFlower = new YellowFlower ();
        PurpleFlower PurpleFlower = new PurpleFlower ();
        addObject(new YellowFlower(), 500, 500);
        addObject(new PurpleFlower(), 100, 300);
        addObject(new YellowFlower(), 200, 400);
        addObject(new PinkFlower(), 100, 100);
        addObject(new YellowFlower(), 700, 400);
        addObject(new PurpleFlower(), 800, 200);
        addObject(new PinkFlower(), 600, 100);
        addObject(new PinkFlower(), 400, 700);        
        //add bees to the bee world
        Forager Forager = new Forager ();
        Worker Worker = new Worker ();
        Queen Queen = new Queen ();
        addObject(new Queen(), 500, 250);
        //add the hive to the bee world
        Hive Hive = new Hive ();
        addObject(new Hive(), 650, 400);
    }
    
    public void highTemperature()
    {
        //add flowers to the bee world
        PinkFlower PinkFlower = new PinkFlower ();
        YellowFlower YellowFlower = new YellowFlower ();
        PurpleFlower PurpleFlower = new PurpleFlower ();
        addObject(new YellowFlower(), 500, 500);
        addObject(new PurpleFlower(), 100, 300);
        addObject(new YellowFlower(), 200, 400);
        addObject(new PinkFlower(), 100, 100);
        addObject(new PinkFlower(), 700, 400);
        addObject(new PurpleFlower(), 800, 200);        
        //add bees to the bee world
        Forager Forager = new Forager ();
        Worker Worker = new Worker ();
        Queen Queen = new Queen ();
        addObject(new Queen(), 500, 250);
        //add the hive to the bee world
        Hive Hive = new Hive ();
        addObject(new Hive(), 650, 400);        
    }
this is the code in regularTemperature and highTemperature
danpost danpost

2021/10/12

#
Remove line 3 from here.
xEmilyx xEmilyx

2021/10/12

#
ive done this and now this error is coming up: java.lang.NullPointerException at BeeWorld.runSimulation(BeeWorld.java:61) at BeeWorld.<init>(BeeWorld.java:56) at StartButton.act(StartButton.java:25) this is the start button code:
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
          BeeWorld beeWorld = new BeeWorld();
          beeWorld.WeatherInput = ((MainMenu)getWorld()).WeatherInput;
          Greenfoot.setWorld(beeWorld);
        }
    }
danpost danpost

2021/10/12

#
Remove lines 46 and 47 from here and add the following after line 6 above:
beeWorld.runSimulation();
xixEmilyxix xixEmilyxix

2021/10/13

#
after the line '* @author (your name)' in BeeWorld?
danpost danpost

2021/10/13

#
xixEmilyxix wrote...
after the line '* @author (your name)' in BeeWorld?
No -- here.
Spock47 Spock47

2021/10/14

#
xixEmilyxix wrote...
ive added this and it does not seem to be taking into account what i set the weather value at, as it is only running lowTemperature()
    public void runSimulation()
    {
         WeatherInput = new WeatherInput();
         int weatherValue = WeatherInput.weatherValue;

         if(weatherValue >= -40 || weatherValue <= 15)
         {
             //if the weather value is between -40 and 15 then the low temp simulation will run
             lowTemperature();
         } else if(weatherValue >= 16 || weatherValue <= 27)
         {
             //if the weather value is between 16 and 27 then the regular temp simulation will run
             regularTemperature();
         } else if(weatherValue >= 28 || weatherValue <= 40)
         {
             //if the weather value is between 28 and 40 then the high temp simulation will run
             highTemperature();
         }     
    }
The condition in line "if(weatherValue >= -40 || weatherValue <= 15)" is always true. That's why "lowTemperature();" is always called. -> Just replace "||" (OR operator) with "&&" (AND operator). That way the given condition is only true for values between -40 and 15. You want to replace the operator in the other two lines, too. Live long and prosper, Spock47
danpost danpost

2021/10/14

#
Spock47 wrote...
The condition in line "if(weatherValue >= -40 || weatherValue <= 15)" is always true.
Good catch, Spock47! I did not think to look at that that closely. I guess I just presumed them to be "meaningful".
There are more replies on the next page.
1
2
3