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

2014/1/28

How do I pass a variable between different worlds?

Lethal_Vitamin Lethal_Vitamin

2014/1/28

#
How do I pass a variable between different worlds?
danpost danpost

2014/1/28

#
The following statement is not intended to 'knit-pick'; but, you cannot pass a variable between worlds, you can only pass the value of a variable from one world to another. (1) set up local fields to reference both worlds; (2) assign the value of the variable in the first world to the variable in the second;
Lethal_Vitamin Lethal_Vitamin

2014/1/28

#
May I see an example?
danpost danpost

2014/1/28

#
How about showing what you have tried!
Lethal_Vitamin Lethal_Vitamin

2014/1/28

#
Very well. My Scenario I want to pass the boolean "wantsMusic" in my MusicToggle.class to MyWorld.class, but I have no clue how to do this.
danpost danpost

2014/1/28

#
There are a couple (at least) ways to do this; however, it will be the job of the PlayButton to accomplish this since it is what creates the MyWorld world object. The code is in the act method within the 'if' block of the PlayButton class. The current code is this:
1
2
3
4
if (Greenfoot.mouseClicked(this))
{
    Greenfoot.setWorld(new MyWorld());
}
To get a reference to the new world, line 3 will need to be replaced with:
1
2
MyWorld mw = new MyWorld();
Greenfoot.setWorld(mw);
Now, the value of the wantsMusic field in the MusicToggle class needs to accessed.
1
boolean wantsToPlay = ((MusicToggle)getWorld(),getObjects(MusicToggle.class).get(0)).wantsMusic;
I know this is a long drawn-out sequence, so bear with me: (1) 'getWorld' returns the TitleScreen world that the PlayButton object is in; (2) 'getObjects(MusicToggle.class)' gets a list of all MusicToggle objects in that world; (3) 'get(0)' gets the first element from that list; (4) '((MusicToggle)... ) typecasts the element (which is an Object from the list) as a MusicToggle instance; (5) 'wantsMusic' get the value from the 'wantsMusic' field of that MusicToggle instance; Ok, now we need to set that value to the 'wantsMusic' field of the new MyWorld instance.
1
mw.wantsMusic = wantsToPlay;
Although it does not really matter, I would do this between the two lines that split the initial line 3 as follows:
1
2
3
4
5
6
if (Greenfoot.mouseClicked(this))
{
    MyWorld mw = new MyWorld();
    mw.wantsMusic = ((MusicToggle)getWorld().getObjects(MusicToggle.class).get(0)).wantsMusic;
    Greenfoot.setWorld(mw);
}
Once you set your TitleScreen as the initial world, however, you will find that your 'started' method in the MyWorld class will not execute (the scenario must be started, or stopped and re-started, for this method to be automatically called. Fortunately, you can call the method programmatically. As a final line in the 'if' block above, place this:
1
mw.started();
danpost danpost

2014/1/28

#
You were wondering why the circles subtly change upon starting the scenario. It is because you are setting a new image to the actor as the alpha value changes. If you instead just adjust the transparency of the image that is already assigned to the actor, they you will not notice any changes in it. Simply put and not to hard to understand:
1
if (alpha > 0) getImage().setTransparency((int)alpha); else getWorld().removeObject(this);
Maybe even easier would be (I think the mind likes this one better):
1
if (alpha == 0) getWorld().removeObject(this); else getImage().setTransparency((int)alpha);
Lethal_Vitamin Lethal_Vitamin

2014/1/28

#
Thank you so much! One more thing, on the Java applet on the Greenfoot website, how do I get my TitleScreen World to appear to the user first?
danpost danpost

2014/1/28

#
That is something that is done before uploading to the Greenfoot site. After compiling, right-click on the TitleScreen class icon and select 'new TitleScreen()' from the pop-up menu. Instantiating a world in this way makes it the start-up world class (the world that is initially created during resets). Then re-upload the scenario (after testing not only that it comes up first, but that the scenario runs properly as well -- with special attention to the music).
You need to login to post a reply.