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

2014/8/15

Referencing 'Other Classes'-type Classes in Worlds

Dab1001 Dab1001

2014/8/15

#
I'm trying to pass two variables (a string and an int) between two worlds during a world change. The way I'm doing it is by having a non-actor, non-world class called InfoStorer. Variables in this class will be set by actors in the world GameBackdrop, and then retrieved by the world Menu in its constructor. However, I am having difficulty referencing this class. Does anybody know how to do it? Thanks in advance.
danpost danpost

2014/8/15

#
That seems like a lot of code to pass two variables. You should be able to just code a constructor in your Menu world to receive them directly:
1
public Menu(String str, int val)
You would create and set the Menu world active passing the values with:
1
Greenfoot.setWorld(new Menu(strValue, intValue));
You could accomplish this by passing the InfoStorer object instead; but, like I said -- that would use excess code to accomplish the task. Another way you can do this is by setting fields in the new world to those values you need to pass. For example:
1
2
3
4
Menu menu = new Menu();
menu.passedString = /* value of string to pass */
menu.passedInt = /* value of int to pass */
Greenfoot.setWorld(menu);
with the Menu world something like this:
1
2
3
4
5
6
7
8
9
public class Menu extends Actor
{
    public String passedValue;
    public int passedInt;
 
    public Menu()
    {
        super(...);
        // etc.
Dab1001 Dab1001

2014/8/17

#
danpost wrote...
That seems like a lot of code to pass two variables. You should be able to just code a constructor in your Menu world to receive them directly:
1
public Menu(String str, int val)
You would create and set the Menu world active passing the values with:
1
Greenfoot.setWorld(new Menu(strValue, intValue));
You could accomplish this by passing the InfoStorer object instead; but, like I said -- that would use excess code to accomplish the task. Another way you can do this is by setting fields in the new world to those values you need to pass. For example:
1
2
3
4
Menu menu = new Menu();
menu.passedString = /* value of string to pass */
menu.passedInt = /* value of int to pass */
Greenfoot.setWorld(menu);
with the Menu world something like this:
1
2
3
4
5
6
7
8
9
public class Menu extends Actor
{
    public String passedValue;
    public int passedInt;
 
    public Menu()
    {
        super(...);
        // etc.
I tried that at first, but it threw an error. I just tried it again, and it worked. I must've made a typo or syntax error somewhere. Thanks for your help!
danpost danpost

2014/8/17

#
It might have been my fault (I see that I extended Menu from Actor instead of from World; though, I think that would be an easy catch).
Dab1001 Dab1001

2014/8/17

#
danpost wrote...
It might have been my fault (I see that I extended Menu from Actor instead of from World; though, I think that would be an easy catch).
No, I mean before that (I originally tried setting it up in the constructor before asking the question here). Anyway, one more unrelated question. Do you know how I can detect if the left mouse button is being held down? Currently I'm using Greenfoot.getMouseInfo().getButton() == 1, but it only registers if the button is held and the mouse is moving.
danpost danpost

2014/8/17

#
I think you would need to add an instance Boolean and set it when the button is found pressed and unset it when a click action for that button is detected. I have not tested it out completely, but something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
// instance field
boolean rightButtonDown;
// checks
if (!rightButtonDown && Greenfoot.mousePressed(null) && Greenfoot.getMouseInfo().getButton() == 1)
{
    rightButtonDown = true;
    // anything else you might want to do here
}
if (rightButtonDown && Greenfoot.mouseClicked(null) && Greenfoot.geMouseInfo().getButton() == 1)
{
    rightButtonDown = false;
    // anything else you might want to do here
}
Then 'rightButtonDown' will hold the state you need.
Dab1001 Dab1001

2014/8/17

#
danpost wrote...
I think you would need to add an instance Boolean and set it when the button is found pressed and unset it when a click action for that button is detected. I have not tested it out completely, but something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
// instance field
boolean rightButtonDown;
// checks
if (!rightButtonDown && Greenfoot.mousePressed(null) && Greenfoot.getMouseInfo().getButton() == 1)
{
    rightButtonDown = true;
    // anything else you might want to do here
}
if (rightButtonDown && Greenfoot.mouseClicked(null) && Greenfoot.geMouseInfo().getButton() == 1)
{
    rightButtonDown = false;
    // anything else you might want to do here
}
Then 'rightButtonDown' will hold the state you need.
Thanks! I'm nearly done my game now :)
You need to login to post a reply.