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

2013/5/25

How do I fade one world into another?

johnguackmbl johnguackmbl

2013/5/25

#
{
        hasStarted = true;
        writeTitle();
        Greenfoot.delay(90);
        Greenfoot.setWorld(new Menu());
}
So here's my code. I want to fade the world into the Menu, but I don't know how to. I had the idea of making an object that was a black screen that faded by changing its colors, but I'm wondering if there's any other more efficient way. Thanks!
danpost danpost

2013/5/25

#
The black screen idea is not a bad idea; but you do not have to change its color. Initialize its transparency to zero, and increase its value a little each act method. When its value reaches 255 or more , then change worlds. Perform the check before setting the new transparency. In the new world constructor, add the black screen object and have its transparency start at 255 and go down until it is zero, then remove the object. A simple class code for such an object would be like this:
import greenfoot.*;

public class Curtain extends Actor
{
    private int transVal;
    private int direction;
    World nextWorld;

    public Curtain(World inWorld)
    {
        nextWorld = inWorld;
        if (nextWorld == null) direction = -4;  else direction = 4;
    }

    public void addedToWorld(World world)
    {
        setImage(new GreenfootImage(world.getWidth(), world.getHeight()));
        getImage().fill(); // black is default color for new GreenfootImages
    }

    public void act()
    {
        transVal = (transVal+direction+256)%256; // update value
        if (transVal == 0) // check value to see if we are done yet
        {
            if (nextWorld == null) getWorld().removeObject(this); // for new world
            else Greenfoot.setWorld(nextWorld); // for old world
        }
        else getImage().setTransparency(transVal); // for when not done yet
    }
}
When the conditions are right to change worlds, use:
World world2 = new World2(); // create using name of your new world
Curtain curtain = new Curtain(world2); // create curtain object
int x = getWorld().getWidth()/2;
int y = getWorld().getHeight()/2;
addObject(curtain, x, y); // add curtain into world
In the World2 constructor, add this:
addObject(new Curtain(null), getWidth()/2, getHeight()/2);
Either add the curtain into the world as the last things you add, or use: setPaintOrder(Curtain.class);
johnguackmbl johnguackmbl

2013/5/25

#
Where's the second part of the code supposed to go, danpost?
danpost danpost

2013/5/25

#
johnguackmbl wrote...
Where's the second part of the code supposed to go, danpost?
The five-line snippet should probably go within an 'if' block whose conditions determine that it is time to change worlds. The code was not particularly written for being in a world class code or an actor class code. Line 5 should start with 'getWorld().' if in an actor class and lines 3 and 4 should not have 'getWorld().' if in a world class.
You need to login to post a reply.