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

2021/12/9

When actor clicked a counter increases by one

1
2
3
xixEmilyxix xixEmilyxix

2021/12/16

#
danpost wrote...
xixEmilyxix wrote...
am i meant to add the ''addedToWorld'' method anywhere?
Not sure what you mean by "anywhere"? Only as above would be sufficient. It was placed into your AddDay class to initialize the display, which could not be done until the AddDay object was created and added into a world.
like do i need to call the method in another method?
danpost danpost

2021/12/16

#
xixEmilyxix wrote...
do i need to call the method in another method?
No, you do not need to call the method from anywhere. When you add a new actor into a world, it is called automatically (via the addObject method). See addedToWorld in the Actor class API documentation.
xixEmilyxix xixEmilyxix

2021/12/19

#
Why would it not work then?
danpost danpost

2021/12/19

#
xixEmilyxix wrote...
Why would it not work then?
I do not know. Try adding this to your BeeWorld constructor (public BeeWorld()) after the super(...); line:
setPaintOrder(AddDay.class);
xixEmilyxix xixEmilyxix

2021/12/20

#
danpost wrote...
xixEmilyxix wrote...
Why would it not work then?
I do not know. Try adding this to your BeeWorld constructor (public BeeWorld()) after the super(...); line:
setPaintOrder(AddDay.class);
Tried it and it fixed, thanks!
danpost danpost

2021/12/20

#
xixEmilyxix wrote...
Tried it and it fixed, thanks!
That means some other actor was getting the clicks above the AddDay actors. Previously,
xixEmilyxix wrote...
Theres no actors above it
xixEmilyxix xixEmilyxix

2021/12/22

#
Ive started to add to the newDay() code and its made my day button disappear, any idea why? The issues i found where that its not removing any objects except my add day button but im pretty sure its doing the runSimulation() code. the if statement is also saying int cannot be converted to boolean which im not sure why either. I want it to basically reset to the correct simulation mode depending on the termperature value but keep values such as day value and add a new bee every 5 days/ This is what i added for my newday system:
    public void newDay()
    {
        //start new day
        //remove all objects
        List objects = getObjects(null);
        removeObjects(objects);
        runSimulation();
        
        //add bee every 5 days
        if(dayValue = 5 || dayValue = 10 || dayValue = 15 || dayValue = 20 || dayValue = 25 || dayValue = 30 || dayValue = 35 || dayValue = 40 || dayValue = 45 || dayValue = 50)
        {
            //add new bee
            addObject(new Forager(), 550, 250);                       
        }
        
    }
danpost danpost

2021/12/22

#
xixEmilyxix wrote...
the if statement is also saying int cannot be converted to boolean which im not sure why
The single equal sign mean to set the variable on the left to the value of the evaluated expression on the right. As such, it is not a boolean, but, in your case, an int. Use two equal signs to compare two numeric values (or to check if two reference point to the same exact object). Lines 10 thru 14 can be replaced with the following line:
if (dayValue%5 == 0) addObject(new Forager(), 550, 250);
Ive started to add to the newDay() code and its made my day button disappear, any idea why? << Code Omitted >>
Lines 5 and 6 would tend to remove every actor in the world. You could keep a reference to the addDay actor and either remove it from the list created in line 5 or just add it back into the world after line 6. Or, you could add a new one into the world after line 6.
xixEmilyxix xixEmilyxix

2021/12/23

#
danpost wrote...
xixEmilyxix wrote...
the if statement is also saying int cannot be converted to boolean which im not sure why
The single equal sign mean to set the variable on the left to the value of the evaluated expression on the right. As such, it is not a boolean, but, in your case, an int. Use two equal signs to compare two numeric values (or to check if two reference point to the same exact object). Lines 10 thru 14 can be replaced with the following line:
if (dayValue%5 == 0) addObject(new Forager(), 550, 250);
Ive started to add to the newDay() code and its made my day button disappear, any idea why? << Code Omitted >>
Lines 5 and 6 would tend to remove every actor in the world. You could keep a reference to the addDay actor and either remove it from the list created in line 5 or just add it back into the world after line 6. Or, you could add a new one into the world after line 6.
So i made some changes and now im getting an error because of the add day code and im not sure why, this is the error: at greenfoot.World.addObject(World.java:439) at BeeWorld.newDay(BeeWorld.java:189) at BeeWorld.updateDay(BeeWorld.java:176) at AddDay.addedToWorld(AddDay.java:34) and this is the code linked to it:
    public void updateDay()
    {
        //update what day it is and run the next day when the day button is clicked
        dayValue = dayValue +1;
        showText("Day: " + dayValue, 810, 50);
        newDay();
    }
    
    public void newDay()
    {
        //start new day
        //remove all objects
        List objects = getObjects(null);
        removeObjects(objects);
        
        //add the help and add day button back
        //add the add day button
        AddDay AddDay = new AddDay ();
        addObject (AddDay, getWidth (), getHeight()); 
        AddDay.setLocation(820,500);
   
        //add the help button
        HelpButton HelpButton = new HelpButton ();
        addObject (HelpButton, getWidth (), getHeight ()); 
        HelpButton.setLocation(100,190);
        
        //random chance to see if bee dies
        
        //add bee every 5 days
        if (dayValue%5 == 0)
        
        addObject(new Forager(), 550, 250);
        updateHoney();
    }
public class AddDay extends Actor
{ 
    public AddDay()
    {
       //Set image of button
       setImage(new GreenfootImage("addDaybutton.png"));
    }
    
    public void act()
    {
        //check if the button is being pressed
        addDay();
    }
    
    public void addDay()
    {
        if(Greenfoot.mouseClicked(this))
        {
           BeeWorld beeWorld = (BeeWorld) getWorld();
           beeWorld.updateDay();                     
        }    
    }
    
    protected void addedToWorld(World world)
    {
        ((BeeWorld)world).updateDay();
    } 
} 
danpost danpost

2021/12/23

#
xixEmilyxix wrote...
i made some changes and now im getting an error because of the add day code and im not sure why, this is the error: << Error Trace Omitted >> and this is the code linked to it: << Code Omitted >>
I think the error is due to line 6 of your BeeWorld code above. Remove the line to hopefully resolve the error. The newDay method is what causes updateDay to execute to begin with (via addedToWorld). If it calls back to newDay, then you get an infinite loop.
danpost danpost

2021/12/23

#
If I were to code something like this, I would keep a reference to the button. Button basics being:
import greenfoot.*;

public class BeeWorld extends World
{
    AddDay addDay = new AddDay();
    HelpButton helpButton = new HelpButton();
    int dayValue;
    
    public BeeWorld()
    {
        super(900, 600, 1);
        newDay();
    }
    
    private void newDay()
    {
        dayValue++;
        showText("Day: "+dayValue, 810, 50);
        removeObjects(getObjects(null));
        addObject(addDay(), 820, 500);
        addObject(helpButton, 100, 190);
        if (dayValue%5 == 0)
        {
            addObject(new Forager(), 550, 250);
            updateHoney();
        }
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(addDay)) newDay();
        if (Greenfoot.mouseClicked(helpButton)) help();
    }
    
    private void help()
    {
        ...
    }
}
with this as AddDay class:
import greenfoot.*;

public class AddDay extends Actor
{
    public AddDay()
    {
        setImage(new GreenfootImage("addDaybutton.png"));
    }
}
Actually, that is more than I would actually do for the AddDay class. I would not have it at all. Normally, I would have a simple Actor subclass to be used for all buttons and other GUI objects and explicitly set their image after creating their instance of the class. As such, I would have:
public class SimpleActor extends greenfoot.Actor {}
import greenfoot.*;

public class BeeWorld extends World
{
    Actor btnAddDay, btnHelp;
    int dayValue;
    
    public BeeWorld()
    {
        super(900, 600, 1);
        btnAddDay = new SimpleActor();
        btnAddDay.setImage(new GreenfootImage("addDaybutton.png"));
        btnHelp = new SimpleActor();
        btnHelp.setImage(new GreenfootImage("helpbutton.png"));
        
        newDay();
    }
    
    private void newDay()
    {
        removeObject(getObjects(null));
        dayValue++;
        showText("Day: "+dayValue, 810, 50);
        addObject(btnAddDay, 820, 500);
        addObject(btnHelp, 100, 190);
        if (dayValue%5 == 0)
        {
            addObject(new Forager(), 550, 250);
            updateHoney();
        }
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(btnAddDay)) newDay();
        if (Greenfoot.mouseClicked(btnHelp)) help();
    }
    
    private void help()
    {
        ...
    }
    
    public void updateHoney()
    {
        ...
    }
}
Here, there is no AddDay class and no HelpButton class; just a SimpleActor class as exactly as given above.
xixEmilyxix xixEmilyxix

2021/12/23

#
I will add this code and have a look, thanks
xixEmilyxix xixEmilyxix

2021/12/23

#
danpost wrote...
xixEmilyxix wrote...
i made some changes and now im getting an error because of the add day code and im not sure why, this is the error: << Error Trace Omitted >> and this is the code linked to it: << Code Omitted >>
I think the error is due to line 6 of your BeeWorld code above. Remove the line to hopefully resolve the error. The newDay method is what causes updateDay to execute to begin with (via addedToWorld). If it calls back to newDay, then you get an infinite loop.
I have added all this and changed all my buttons into this so this includes my: addday button, start button, help button and end simulation button. What seems to be not working now is the clicking part of the buttons. On any of the buttons i click it and nothing happens. Also, i am getting another error with what the start button code does when clicked. This is what my relevant code looks like:
public class MainMenu extends World
{
    //declare variables
    ErrorMessageWeather ErrorMessageWeather;
    WeatherInput weatherInput = new WeatherInput();
    Actor btnHelp, btnStartButton;
    /**
     * Constructor for objects of class mainMenu.
     * 
     */
    public MainMenu()
    {    
        // Create the main menu
        super(864, 540, 1);
        Greenfoot.start();
        setBackground(new GreenfootImage("MainMenu.png"));
        
        //add the help button
        btnHelp = new Buttons();
        btnHelp.setImage(new GreenfootImage("helpButton.png"));
        addObject(btnHelp, 100, 190);
        
        //add start button to main menu
        btnStartButton = new Buttons();
        btnStartButton.setImage(new GreenfootImage("startButton.png"));
        addObject (btnStartButton, 150, 350); 

        
        //add weather input box to main menu
        addObject (weatherInput, getWidth (), getHeight ()); 
        weatherInput.setLocation(400,250);
    }
    
    public void act()
    {
        if(Greenfoot.mouseClicked(btnStartButton))
        {
           startSimulation();
        }
    }
    
    private void startSimulation()
    {
        Greenfoot.setWorld(new BeeWorld(((MainMenu)getWorld()).weatherInput.weatherValue)); 
    }
}
from the BeeWorld
public class BeeWorld extends World
{
    //declare variables
    int beeValue;
    Flower PinkFlower;
    Flower PurpleFlower;
    Flower YellowFlower;
    StartButton StartButton;
    int weatherValue;
    Hive Hive;
    Bee Forager;
    Bee Worker;
    Bee Queen;
    public static Flower flower;
    int dayValue;
    int honeyValue;
    int nectarValue;
    Actor btnAddDay, btnHelp, btnEndSimulation;
    /**
     * Constructor for objects of class BeeWorld.
     * 
     */
    public BeeWorld(int weatherVal)
    {
        //set background to main screen when button is pressed
        super(864, 540, 1);
        setBackground(new GreenfootImage("Background.png"));
        
        //add buttons
        btnAddDay = new Buttons();
        btnAddDay.setImage(new GreenfootImage("addDaybutton.png"));
        btnHelp = new Buttons();
        btnHelp.setImage(new GreenfootImage("helpButton.png"));
        btnEndSimulation = new Buttons();
        btnEndSimulation.setImage(new GreenfootImage("EndSimulationbutton.png"));
        addObject(btnEndSimulation, 50, 500);
        
        //run new day
        newDay();       
        
        //add the counter for the bee value
        showText("Bees: ",  810, 20);
                    
        //declare weatherValue
        weatherValue = weatherVal;
                
        //show honey and nectar values
        nectarValue = 0;
        honeyValue = 0;
        showText("Nectar: " + nectarValue,  810, 80);
        showText("Honey: " + honeyValue,  810, 110);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(btnAddDay))
        {
           newDay();
        }
        if (Greenfoot.mouseClicked(btnHelp))
        {
           help();
        }
        if(Greenfoot.mouseClicked(btnEndSimulation))
        {
           endSimulation();
        }
    }
    private void endSimulation()
    {
        Greenfoot.setWorld(new EndScreen()); 
    }
    
    private void help()
    {
        //add help menu
        HelpMenu HelpMenu = new HelpMenu();
        addObject (HelpMenu, getWidth(), getHeight());
        HelpMenu.setLocation(650,200);
        
        //button to be able to remove help menu
        RemoveMenu RemoveMenu = new RemoveMenu();
        addObject (RemoveMenu, getWidth(), getHeight());
        RemoveMenu.setLocation(900,200);
    }
    public void updateDay()
    {
        //update what day it is and run the next day when the day button is clicked
        dayValue = dayValue +1;
        showText("Day: " + dayValue, 810, 50);
        newDay();
    }
    
    private void newDay()
    {
        //start new day
        //remove all objects
        List objects = getObjects(null);
        removeObjects(objects);
        
        //increase day value
        dayValue++;
        
        //increase honey value
        updateHoney();
        
        //add buttons back        
        showText("Day: "+dayValue, 810, 50);
        addObject(btnAddDay, 820, 500);
        addObject(btnHelp, 100, 190);
        addObject(btnEndSimulation, 50, 500);
        
        //run simulation again
        runSimulation();
        
        //random chance to see if bee dies
        
        //add bee every 5 days
        if (dayValue%5 == 0)
        {
           addObject(new Forager(), 550, 250);           
        }
    }
xixEmilyxix xixEmilyxix

2021/12/23

#
Forgot to mention: im getting the error on line 44 in the main menu. It's saying ''cannot find symbol - method getworld''
danpost danpost

2021/12/24

#
xixEmilyxix wrote...
im getting the error on line 44 in the main menu. It's saying ''cannot find symbol - method getworld''
Change the line to:
Greenfoot.setWorld(new BeeWorld(weatherInput.weatherValue));
(it is coded in the MainMenu class; and, you cannot use the Actor instance method getWorld in a class extending World) The line could also be written as:
Greenfoot.setWorld(new BeeWorld(this.weatherInput.weatherValue));
There are more replies on the next page.
1
2
3