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

2017/1/15

Fading Starting Screen

Hondrok Hondrok

2017/1/15

#
I want the startscreen to fade when I press enter and of course to open the myworld world. Is there any option to do that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
public class Starting_Screen extends World
{
    public Starting_Screen()
    {   
        super(1195, 730, 1); 
    }
     
    public void act()
    {
        if(Greenfoot.isKeyDown("enter"))
        {
            Greenfoot.setWorld(new MyWorld());
        }
    }
}
Super_Hippo Super_Hippo

2017/1/15

#
Instead of setting the new world in line 14, create a new actor. This actor has a black image with the size of the world. It is the first one in the paint order (if there are any objects in the world which should also fade.) I am sure you could handle all that in the world itself, but usually I create a new class for that. In this class, you create its image in the constructor and set the transparency to 0 (=invisible). In its act method, you increase the transparency and when it is 255 (=you only see black in the scenario), you set the new world active directly from there. You can also do the same thing the other way around for fading in.
danpost danpost

2017/1/15

#
Hondrok wrote...
I want the startscreen to fade when I press enter and of course to open the myworld world. Is there any option to do that?
There is no option to do that. But there is a way to accomplish that programmatically. For example, I wrote a 'PauseWorld' support class that will take the current view of a world and make a still out of it. You can add an actor object that fills the entire viewport that goes from transparent to opaque and (same type or different type actor) from opaque to transparent for the fading out and in. The combination would require the following world changes: old world >> old world still + fade out >> new world still + fade in >> new world There are other ways to do this; but it can get tricky in the MyWorld world because you would not want actors in that world to be interacting while the fading in is happening.
Hondrok Hondrok

2017/1/15

#
Super_Hippo wrote...
Instead of setting the new world in line 14, create a new actor. This actor has a black image with the size of the world. It is the first one in the paint order (if there are any objects in the world which should also fade.) I am sure you could handle all that in the world itself, but usually I create a new class for that. In this class, you create its image in the constructor and set the transparency to 0 (=invisible). In its act method, you increase the transparency and when it is 255 (=you only see black in the scenario), you set the new world active directly from there. You can also do the same thing the other way around for fading in.
danpost wrote...
Hondrok wrote...
I want the startscreen to fade when I press enter and of course to open the myworld world. Is there any option to do that?
There is no option to do that. But there is a way to accomplish that programmatically. For example, I wrote a 'PauseWorld' support class that will take the current view of a world and make a still out of it. You can add an actor object that fills the entire viewport that goes from transparent to opaque and (same type or different type actor) from opaque to transparent for the fading out and in. The combination would require the following world changes: old world >> old world still + fade out >> new world still + fade in >> new world There are other ways to do this; but it can get tricky in the MyWorld world because you would not want actors in that world to be interacting while the fading in is happening.
and how do I add transparency?
danpost danpost

2017/1/15

#
You create a subclass of Actor with an image the size of the world that is filled black. The actor should know two things. One, whether fading in or out; and two, the world to proceed to. Use the 'getImage' method to get the actors image and use the 'getTransparency' and 'setTransparency' methods on the image to change its transparency and to check its value to see if you are ready to proceed to the next world.
Hondrok Hondrok

2017/1/16

#
danpost wrote...
You create a subclass of Actor with an image the size of the world that is filled black. The actor should know two things. One, whether fading in or out; and two, the world to proceed to. Use the 'getImage' method to get the actors image and use the 'getTransparency' and 'setTransparency' methods on the image to change its transparency and to check its value to see if you are ready to proceed to the next world.
I tried something but I got stuck in the double/int thing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import greenfoot.*;
 
public class Start_scr_a extends Actor
{
    public double fade = 255;
     
    public void act()
    {
        if(Greenfoot.isKeyDown("enter"))
        {
            while(fade != 0)
            {
                getImage().setTransparency(fade);
                fade = fade - 0.5;
            }
        }
    }   
}
danpost danpost

2017/1/16

#
The transparency of an image is given in int values. This means that if you wish to fade back in as slowly as you are doing here, then 'fade' in line 13 will need to be converted to an 'int' value. I cannot say why you added the 'if' statement to the code as the Start_scr_a object would either be placed into a world because of that condition or automatically. Also, using a 'while' loop is not going to accomplish what you want as it is ran completely (in a single execution of act) before any screen updates -- lines 11 through 15 ( with 'fade' in line 13 corrected to '(int)fade' ) would be equivalent to:
1
getImage().setTransparency(0);
The code needs to execute over a number of act cycles for the fading to be realized. There are 256 value between 0 and 255 (inclusive) and it will take close to 5 seconds for the fading out and 5 more seconds for the fading in of the new world. All actors that happen to be in either world will still be acting and interacting with each other during those 5 seconds in both worlds. That is why I suggested multiple worlds for the transition.
You need to login to post a reply.