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

2012/12/27

Pop-up Message

ctgreenfoot ctgreenfoot

2012/12/27

#
Hi, I've searched all over the internet for a tutorial on how to make a pop-up message that says "Game over! You win!" or something similar, but there is nothing! Is anyone willing to give me a quick explanation or tell me about a tutorial that they know about?
danpost danpost

2012/12/27

#
Create a sub-class of Actor called 'GameOver':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*;
import java.awt.Color;
 
public class GameOver extends Actor
{
    String text = " GAME OVER! \n YOU WIN! ";
    GreenfootImage inner = new GreenfootImage(text, 48, Color.black, new Color(0, 0, 0, 96));
 
    public GameOver()
    {
    }
 
    // create a gameover image the size of the world
    public void addedToWorld(World world)
    {
        int wide = world.getWidth();
        int high = world.getHeight();
        GreenfootImage outer = new GreenfootImage(wide, high);
        int leftX = (wide - inner.getWidth())/2;
        int topY = (high - inner.getHeight())/2;
        outer.drawImage(inner, leftX, topY);
        setImage(outer);
    }
 
    // the following is optional
    // remove pop-up when clicked on
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
             getWorld().removeObject(this);
        }
    }
}
ctgreenfoot ctgreenfoot

2012/12/27

#
thanks!
ctgreenfoot ctgreenfoot

2012/12/27

#
thanks!
ctgreenfoot ctgreenfoot

2012/12/27

#
However (sorry I am still learning and do not really understand this part!) how do I link it to the Counter actor so that when the counter hits 0, the message comes up?
danpost danpost

2012/12/27

#
Anywhere you have code that decreases or changes the counter (where it could end up to be zero), put a check immediately following that code:
1
if(counter.getValue()==0) getWorld().addObject(new GameOver(), getWorld().getWidth()/2, getWorld().getHeight()/2);
Now, I do not know if 'counter.getValue()' would be correct in your case, but whatever code retrieves the value of the counter goes there.
tiana_nikole tiana_nikole

2015/2/20

#
what if i want it to pop up when two actors collide?
danpost danpost

2015/2/20

#
Then change the condition. Instead of 'counter.getValue() == 0', use the 'isTouching' method of the Actor class (or one of the other collision checking methods provided in that class).
kev kev

2015/10/31

#
danpost wrote...
Create a sub-class of Actor called 'GameOver':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import greenfoot.*;
import java.awt.Color;
 
public class GameOver extends Actor
{
    String text = " GAME OVER! \n YOU WIN! ";
    GreenfootImage inner = new GreenfootImage(text, 48, Color.black, new Color(0, 0, 0, 96));
 
    public GameOver()
    {
    }
 
    // create a gameover image the size of the world
    public void addedToWorld(World world)
    {
        int wide = world.getWidth();
        int high = world.getHeight();
        GreenfootImage outer = new GreenfootImage(wide, high);
        int leftX = (wide - inner.getWidth())/2;
        int topY = (high - inner.getHeight())/2;
        outer.drawImage(inner, leftX, topY);
        setImage(outer);
    }
 
    // the following is optional
    // remove pop-up when clicked on
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
             getWorld().removeObject(this);
        }
    }
}
in the world or sub gameover
You need to login to post a reply.