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

2017/12/14

Sharing variables through Actors

bromung bromung

2017/12/14

#
I want to make it so that when you click a button, it adds 1 onto the score (i got that one) then another button increases the amount the click increases for a certain amount of points Here is my code for the clicker
import greenfoot.*;

/**
 * Write a description of class BlueClick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BlueClick extends Actor
{
    private int var = 0;
    private int add = 1;
    /**
     * Act - do whatever the BlueClick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getWorld().showText("Score: " +var, 100, 40);
        if (Greenfoot.mouseClicked(this))
        {
            var = var + add;
        }
    }    
}
and then here is my increase click code
import greenfoot.*;

/**
 * Write a description of class BlueClick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BlueClick extends Actor
{
    private int var = 0;
    private int add = 1;
    /**
     * Act - do whatever the BlueClick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getWorld().showText("Score: " +var, 100, 40);
        if (Greenfoot.mouseClicked(this))
        {
            var = var + add;
        }
    }    
}
Thanks.
Yehuda Yehuda

2017/12/14

#
You showed the same class (BlueClick) twice. Besides for that I don't understand what you're asking.
roonie01 roonie01

2017/12/15

#
what you could try is you could make another variable called var2 which is also equal to 1 and then do
  public void act() 
    {
        getWorld().showText("Score: " +var, 100, 40);
        if (Greenfoot.mouseClicked(this))
        {
            var = var + add;
        }
else if (Greenfoot.mouseClicked(this))
{
  var = var+var2;
}
    }    
Yehuda Yehuda

2017/12/16

#
@roonie01 Your code says "if mouse is clicked do something otherwise if mouse is clicked do something (else)".
roonie01 roonie01

2017/12/17

#
yeah and isn't that what you were asking
Yehuda Yehuda

2017/12/17

#
I didn't ask anything. I'll give you an easier code example of what you gave.
if (boolVar == true) { // if boolVar is true
    a = 1; // do something
} else if (boolVar == true) { // otherwise if boolVar is true
    a = 2; // do something (else)
}
KevMscotland KevMscotland

2017/12/18

#
I'm not entirely clear on what you want but I think it's something like this:
danpost danpost

2017/12/18

#
KevMscotland wrote...
I'm not entirely clear on what you want but I think it's something like this:
The detecting of 'space' being down will be triggered multiple times during a single press of the key. You would need a field to track the state of the key to determine the moment that the key goes down:
int score = 0;
int increment = 1;
boolean spaceDown = false;

public void act()
{
    if (Greenfoot.mouseClicked(this)) score += increment;
    if (spaceDown != Greenfoot.isKeyDown("space"))
    {
        spaceDown = ! spaceDown;
        if (spaceDown) increment++;
    }
}
You need to login to post a reply.