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

2012/11/14

Making a fader.

Zamoht Zamoht

2012/11/14

#
My scenario is using the Greenfoot.setWorld(World world) method to change between world, but this is a very instant change. I would like to have a smooth change using a fader. My change world method is very simple at the moment and looks like this.
Greenfoot.delay(100);
        getWorld().addObject(new Fader(), getWorld().getWidth() / 2, getWorld().getHeight() / 2);
        Greenfoot.setWorld(((BattleWorld) getWorld()).myWorld);
My fader looks like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Fader here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fader extends Actor
{
    public int fadeLevel = 0;
    /**
     * Act - do whatever the Fader wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    
    public Fader()
    {
        fade(0);
    }

    public void fade(int f)
    {
        if (f > 255)
        {
            f = 255;
        }
        else if (f < 0)
        {
            f = 0;
        }
        
        GreenfootImage fade = new GreenfootImage(600, 400);
        Color fadeColor;
        fadeColor = new Color(0, 0, 0, f);
        fade.clear();
        fade.setColor(fadeColor);
        fade.fillRect(0, 0, 600, 400);
        setImage(fade);
    }
}
Now I would like some kind of loop that made the fade go from 0 to 255 before the method "Greenfoot.setWorld(((BattleWorld) getWorld()).myWorld);" is executed, but I can't figure out how. Any help is appreciated and thanks for your time.
Zamoht Zamoht

2012/11/14

#
Okay it was way simpler than i thought. I removed "Greenfoot.setWorld(((BattleWorld) getWorld()).myWorld);" from the changeWorld() method and added it to the Fader act().
public void act() 
    {
        fade(fadeLevel++);
        
        if (fadeLevel > 255)
        {
            Greenfoot.setWorld(((BattleWorld) getWorld()).myWorld);
        }
    }
danpost danpost

2012/11/14

#
Was 'Wombat Fighter' the scenario you were trying to use fading in? Did you not get it to work OK? I posted a new scenario ('Fractal Slideshow') which utilizes a world-changing fading out and in technique. If you are interested in the code, I can publish it.
Zamoht Zamoht

2012/11/14

#
Yes the scenario was Wombat Fighter, and well I made it work so it's not really a problem now, but I bet your code is cleaner than mine. (I usually end up writing code that works, but at the same time it's probably not considered good programming terms). Thanks anyway.
You need to login to post a reply.