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/9

#
Im trying to make it so i have a day counter in the top corner of my screen. Im trying to make it so when the user clicks on an object, the dayValue is increased by one and this is displayed. This is what i have for the code so far but it is not working. This is in the BeeWorld World.
  
  public int dayValue;
  public void addDay()
    {
        if(Greenfoot.mouseClicked(null))
        {
            Actor actor = Greenfoot.getMouseInfo().getActor();
            if(actor != null)
            {
                dayValue = dayValue + 1;           
            }
        }    
    }
    
danpost danpost

2021/12/9

#
xixEmilyxix wrote...
Im trying to make it so i have a day counter in the top corner of my screen. Im trying to make it so when the user clicks on an object, the dayValue is increased by one and this is displayed. This is what i have for the code so far but it is not working. This is in the BeeWorld World. << Code Omitted >>
Looks to me like the code would work for counting clicks on Actor objects. However, I do not see any codes given for displaying the value. Nor do I see where this method is called from.
xixEmilyxix xixEmilyxix

2021/12/11

#
ive changed where i put it and added some more but it still isn't working, this is what i added. In the AddDay class:
    public void addDay()
    {
        if(Greenfoot.mouseClicked(this))
        {
           BeeWorld beeWorld = (BeeWorld) getWorld();
           beeWorld.update();                     
        }    
    }
and in the BeeWorld class
    public void update()
    {
        //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
    }
danpost danpost

2021/12/11

#
xixEmilyxix wrote...
ive changed where i put it and added some more but it still isn't working, this is what i added. << Code Omitted >>
I still do not see:
where this method is called from.
(referring to addDay)
xixEmilyxix xixEmilyxix

2021/12/11

#
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.update();                     
        }    
    }
}    
this is where i am calling the method
danpost danpost

2021/12/12

#
Maybe you are not being very clear. From what I see the counter will show up and will increment once the button is clicked. However, before the button is clicked, you will not have a visible counter unless you add the following method to the AddDay class:
protected void addedToWorld(World world)
{
    ((BeeWorld)world).update();
}
Declare the dayValue field in the BeeWorld class initialized at zero:
private int dayValue = 0;
// or just
private int dayValue;
xixEmilyxix xixEmilyxix

2021/12/13

#
Ive added this and now it shows a value at the start but still doesnt change when i click the button
danpost danpost

2021/12/13

#
xixEmilyxix wrote...
Ive added this and now it shows a value at the start but still doesnt change when i click the button
What code did you place into the newDay method?
xixEmilyxix xixEmilyxix

2021/12/13

#
danpost wrote...
xixEmilyxix wrote...
Ive added this and now it shows a value at the start but still doesnt change when i click the button
What code did you place into the newDay method?
There is none at the moment, as i am only trying to make the counter for the day increase by one for now
danpost danpost

2021/12/13

#
xixEmilyxix wrote...
There is none at the moment, as i am only trying to make the counter for the day increase by one for now
From what I can tell, the displayed value should increase when the AddDay actor is clicked. Might there be another actor on top of the AddDay actor that is getting the clicks instead?
xixEmilyxix xixEmilyxix

2021/12/13

#
danpost wrote...
xixEmilyxix wrote...
There is none at the moment, as i am only trying to make the counter for the day increase by one for now
From what I can tell, the displayed value should increase when the AddDay actor is clicked. Might there be another actor on top of the AddDay actor that is getting the clicks instead?
Theres no actors above it im not sure if ive messed something up in the add day class this is the code for it:
public class AddDay extends Actor
{ 
    private int dayValue = 0;
    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.update();                     
        }    
    }
    
    protected void addedToWorld(World world)
    {
        ((BeeWorld)world).update();
    } 
} 
danpost danpost

2021/12/13

#
The only thing "wrong" here is line 3, which can be removed. That field should already be in your BeeWorld class.
xixEmilyxix xixEmilyxix

2021/12/14

#
I got rid of this and it still doesn't work
xixEmilyxix xixEmilyxix

2021/12/15

#
danpost wrote...
The only thing "wrong" here is line 3, which can be removed. That field should already be in your BeeWorld class.
am i meant to add the ''addedToWorld'' method anywhere?
danpost danpost

2021/12/15

#
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.
There are more replies on the next page.
1
2
3