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

2016/8/28

How to unload worlds?

1
2
Laurence Laurence

2016/8/28

#
I've been implementing Loading Screens in my game for the purpose of loading another world and unloading the previous but I'm stuck to unloading each and every individual object in the previous world, here is my code so far -
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Loading here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Loading extends World
{
    private int timer = 300;
    /**
     * Constructor for objects of class Loading.
     * 
     */
    public Loading()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 464, 1);
        prepare();
    }
    public void act()
    {
             if (timer>0)
    {
        timer--;
        if(timer == 0) Greenfoot.setWorld(new Menu());
    }
    }
        
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        LoadingText loadingtext = new LoadingText();
        addObject(loadingtext,413,231);
    }
}
If anyone knows how to unload an entire world, I'd appreciate it.
danpost danpost

2016/8/28

#
I think you need to be a little more detailed as to what the issue is. Give us an idea of what you mean by "unload an entire world" as just setting a new world active (with the 'Greenfoot.setWorld(World world)' method) will completely eradicate the currently active one.
Laurence Laurence

2016/8/28

#
I think that may work but the general concept I'm trying to grasp at is - You have two worlds: World1 and World 2 along with inbetween a loading screen. When World1 transfers to the Loading Screen World1 unloads all its assets to prevent it causing an unneeded burden upon Greenfoot's memory heap. Of course, I may be wrong and setting a new world may remove the old one but I'm not 100% sure on this.
Laurence Laurence

2016/8/28

#
I've bumped into another issue, all of my worlds connect to one loading screen, I'm using a public static boolean to determine which world the loading screen loads next -
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Loading here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Loading extends World
{
    private int timer = 300;
    /**
     * Constructor for objects of class Loading.
     * 
     */
    public Loading()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 464, 1);
        prepare();
    }
    public void act()
    {
             if (timer>0)
    {
        timer--;
        if(timer == 0) Greenfoot.setWorld(new Menu());
    }
    }
        
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        LoadingText loadingtext = new LoadingText();
        addObject(loadingtext,413,231);
    }
}
However, I don't know how to put this code within the timer so that if a Boolean is set to true then it'll transport to a certain world and if another is set to true it'll transport to that world. I've already coded a small thing to make sure that if the Boolean detected is true, it's instantly set to false again to avoid bugs.
danpost danpost

2016/8/28

#
Laurence wrote...
all of my worlds connect to one loading screen, I'm using a public static boolean to determine which world the loading screen loads next
Not a good use for a 'static' field. Better would be to place an instance field in the Loading world to hold the world to proceed to:
// instance field
private World nextWorld;

// constructor
public Loading(World gotoWorld)
{
    super(800, 464, 1);
    prepare();
    nextWorld = gotoWorld;
}

// line 27 (from above)
if (timer == 0) Greenfoot.setWorld(nextWorld);
Laurence Laurence

2016/8/28

#
I'm relatively confused on what you are proposing, apologies. Maybe if you could implement your idea into the example I gave earlier, that'd help - I have two worlds: World1 and World 2 along with inbetween a loading screen.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class World extends World
{

    public World()
    {    
        super(600, 400, 1); 
        prepare();
} Both worlds having this code. This is just so I can understand what code to put on the other worlds to make sure this concept functions in sync with the loading screen. Thank you so much for your support thus far!
danpost danpost

2016/8/28

#
Maybe you can look at my Crayon Babies: A World Changing Demo scenario which does what I think you are trying to do here. The mechanisms to proceed to the next world may be different, but the transition from world to world with an in-between world is demonstrated.
Laurence Laurence

2016/8/30

#
A quick question about Timers which are in the loading screen code. How can I use that to make multiple events happen? For example, the time is 9000 (150 seconds), how can I make it so when it's 125 seconds, this happens (which I can already do) but then when it reaches 100 seconds something else happens from there on and etc.?
danpost danpost

2016/8/30

#
Laurence wrote...
A quick question about Timers which are in the loading screen code. How can I use that to make multiple events happen? For example, the time is 9000 (150 seconds), how can I make it so when it's 125 seconds, this happens (which I can already do) but then when it reaches 100 seconds something else happens from there on and etc.?
if (timer.getValue() == 150*60) doSomething();
if (timer.getValue() == 100*60) doSomethingElse();
Laurence Laurence

2016/8/30

#
Thank you for the quick response, heavily appreciated!
Laurence Laurence

2016/8/30

#
I get an error on '.getValue()' as 'Int cannot be dereferenced'.
private int timer = 9000;
    public IntroSequence()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
     public void act()
    {
     if (timer.getValue() == 150*60) Text1 text1 = new Text1();
            addObject(text1,117,427);
     if (timer.getValue() == 100*60) Text2 text2 = new Text2();
            addObject(text2,117,427);
    }
Here is the code I am using for the timer.
danpost danpost

2016/8/30

#
Apparently, you were referring to an internal timer, not a Timers object (which was implied by the first letter being uppercase. Remove '.getValue()' from lines 10 and 12.
Laurence Laurence

2016/8/30

#
Alright, it works now! Thank you
Laurence Laurence

2016/8/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class IntroSequence extends World
{
    private int timer = 9000;
    Text1 text1 = new Text1();
    Text2 text2 = new Text2();
    public IntroSequence()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
        act();
    }
     public void act()
    {
     if (timer == 145*60) 
     addObject(text1,117,427);
     if (timer == 140*60) 
     addObject(text2,117,627);
    }
        private void prepare()
    {
    }
}
The point of this code is to spawn in one piece of text then after another period of time spawn the next. For some reason, this isn't happening despite all errors cleared up.
Super_Hippo Super_Hippo

2016/8/30

#
You don't change the value of 'timer' anywhere.
There are more replies on the next page.
1
2