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

2017/11/5

Struggling to get Greenfoot.mouseClicked() to work

arkod86 arkod86

2017/11/5

#
I have a variable named value under the Canvas class. I've created an Object named myCanvas that acts as an energy bar. I want to increment my energy value whenever my Pet class gets clicked. Any help would be appreciated. Let me know if you need more code
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
    {
         
        if(Greenfoot.mouseClicked(Food.class))
        {
             
            MyWorld realWorld = (MyWorld) getWorld();
            realWorld.myCanvas.value=realWorld.myCanvas.value+5;
        }
         
        idle();
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
GreenfootImage img = new GreenfootImage(142,27);
    int value = 0;
 
    int col = 1; //col =1=green, col=2=blue
     
    public Canvas()
    {
        img.setColor(Color.WHITE);
        img.drawRect (0, 0, 141, 26);
        img.drawString("NRG:" +value/81*100,1,18);       
        img.setColor(Color.BLUE);
        img.fillRect(60,1,value,25);
        setImage(img);
    }
 
    public void act()
    {
        
        updateBar();
    }   
     
     
    public void updateBar()
    {
        img.clear();
        img.setColor(Color.WHITE);
        img.drawRect (0, 0, 141, 26);
        img.drawString("NRG:"+value/81*100,1,18);
        img.setColor(Color.BLUE);       
        img.fillRect(60,1,value,25);
        setImage(img);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    Muffin myMuffin = new Muffin();
    Canvas myCanvas = new Canvas();
    Canvas myECanvas = new Canvas();
    HealthBar myBar = new HealthBar();
    Food myFood = new Food();
     
     
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(680, 425, 1);
        addObject (myBar, 100, 15);
        addObject(myCanvas, 75, 80);
        addObject(myECanvas, 75, 50);
        addObject(myFood, 600, 350);
         
    }
     
}
danpost danpost

2017/11/5

#
Where is the first code block given (the 'act' method with the mouse click checking) located? Reason for asking is you said to add energy when Pet class is clicked; but the clicks are checked on Food class -- confusing. In Canvas class you could just have the following for the constructor:
1
2
3
4
public Canvas()
{
    updateBar();
}
However, line 28 (which was also line 10), looks quite suspicious. Dividing by 81 before multiplying by 100 will lose accuracy. You might have to click 19 times (at 5 nrg points a click) before any value shows on the bar. Perform the multiplication first and the division last unless you are wanting to round to increments of 81.
arkod86 arkod86

2017/11/5

#
Right, the check is done on the Food class as I want to use it as a button to press to increase the value on the Pet. There was a mistake in my original question. Whenever the Food class is clicked increment energy of the Pet. The Act method is located on the Pet class. I think I saw my error. The check should be done on the Food class? Then my next error arrives how do I increment a value on one actor by accessing the World. I had a lesson on this but it was poorly explained. Code within food class:
1
2
3
4
5
6
7
8
9
public void act()
    {
         
        MyWorld realWorld = (MyWorld) getWorld();
        if(Greenfoot.mouseClicked(this))
        {
            realWorld.Pet.energy = realWorld.Pet.energy+5;
        }
    }
arkod86 arkod86

2017/11/5

#
What do I need to put in my code to access and change variables from another actor?
danpost danpost

2017/11/5

#
arkod86 wrote...
The check should be done on the Food class?
Actually, and I fell victim to this above (after seeing your code and not realizing the problem), the check is done on a specific object -- not a class at all. However, with the following, your check could be done anywhere:
1
2
Object clicked = null;
if (Greenfoot.mouseClicked(null) && (clicked = Greenfoot.getMouseInfo().getActor()) != null && clicked instanceof Food)
Anywhere, provided the check is repeatedly performed (like from within an act method). The check will be 'true' any time any Food actor is clicked on.
You need to login to post a reply.