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

2018/7/24

Can't add onscreen message

idk1234 idk1234

2018/7/24

#
Hi I'm trying to add on screen messages if the character dies, but I can't seem to get it to work. I've tried multiple methods.
public class Two_Player extends World
{
    private Actor[] players;
    private Yellow_Crushed yellow_crushed;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Two_Player()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1366, 768, 1); 
        prepare();
        //display1();
        display2();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        //Yellow yellow = new Yellow();
        //addObject(yellow,163,610);
        Crusher_1 crusher_1 = new Crusher_1();
        addObject(crusher_1,215,162);
        Crusher_2 crusher_2 = new Crusher_2();
        addObject(crusher_2,406,162);
        Crusher_3 crusher_3 = new Crusher_3();
        addObject(crusher_3,598,162);
        Crusher_4 crusher_4 = new Crusher_4();
        addObject(crusher_4,786,162);
        Crusher_5 crusher_5 = new Crusher_5();
        addObject(crusher_5,975,162);
        Crusher_6 crusher_6 = new Crusher_6();
        addObject(crusher_6,1164,162);
        //Yellow_Crushed yellow_Crushed = new Yellow_Crushed();
        //addObject(yellow_Crushed,1164,162);
    }

    public void display1()
    {
        if (players == null)
        {
            players = new Actor[] { new Yellow(), new Blue() };
            addObject(players[0],163,610);
            addObject(players[1],163,610);
            //players = new Actor[] { new Yellow(), new SurvivorARROW() };
            addObject(new Yellow_Crushed(), 695, 393);
        }
    }

    public void display2()
    {
        Yellow yellow = new Yellow();
        addObject(yellow,163,610);
        if (getObjects(Players.class).isEmpty())
        {            
            addObject (new Yellow_Crushed(), 695, 393); 
        }
    }
}
Thanks in advance.
Super_Hippo Super_Hippo

2018/7/24

#
Remove line 15, uncomment lines 24+25. Change 'display2' in line 54 to 'act' and remove lines 56+57.
idk1234 idk1234

2018/7/26

#
Hi, thanks so much, it spawns now. However I wanted it to pop up and then disappear after a timer. It works if you pause the world as manually place the on-screen message, but does not when it is spawned in with the above method. Any ideas? I think the above code keeps looping the spawning of the object as you said to place it in the act method. Any ideas? Popup timer if you need it:
public class Yellow_Crushed extends OnScreenPopups
{
    int timer = 700;
    /**
     * Act - do whatever the Yellow_Crushed wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        timer = timer - 1;
        if (timer == 0) getWorld().removeObject(this);
    }    
}
danpost danpost

2018/7/26

#
You could have the pop-up run independent of anything else:
public class Yellow_Crushed extends OnScreenPopups
{
    protected void addedToWorld(World world)
    {
        for (int i=0; i<700; i++) Greenfoot.delay(1);
        world.removeObject(this);
    }
}
Then, you can simply add the Yellow object back "instantly":
public void display2()
{
    if (getObjects(Players.class).isEmpty())
    {
        addObject (new Yellow_Crushed(), 695, 393);
        Yellow yellow = new Yellow();
        addObject(yellow,163,610);
    }
}
idk1234 idk1234

2018/7/27

#
Hi, I tried the above but the image still doesn't appear. If I manually pause the game and place it, it pauses the entire game for the amount of the delay as the code suggests. However, what I want is a pop up message to appear and then disappear after a couple of seconds whilst the game is still running. Any ideas?
Super_Hippo Super_Hippo

2018/7/27

#
If the Yellow_Crushed object is created from a 'addObject(new Yellow_Crushed(), …' command or manually shouldn't make any difference. Question: You said that "it spawns now" and now you said that "the image still doesn't appear". What exactly is happening?
idk1234 idk1234

2018/7/27

#
It works without a timer, but it spawns an infinite about of Yellow_Crushed. So I commented that I would like it fixed and with a Timer, and so I tried to follow Danpost's code, but as I said it does not appear currently.
Super_Hippo Super_Hippo

2018/7/27

#
Show what you have right now (without danpost's suggestion if you don't want to pause the whole scenario while the message is up).
danpost danpost

2018/7/28

#
idk1234 wrote...
It works without a timer, but it spawns an infinite about of Yellow_Crushed. So I commented that I would like it fixed and with a Timer, and so I tried to follow Danpost's code, but as I said it does not appear currently.
Try this for the Yellow_Crushed class:
public class Yellow_Crushed extends OnScreenPopups
{
    private int timer;

    public void act()
    {
        if (++timer == 600) getWorld().removeObject(this);
    }
}
and this for the display2 method:
public void display2()
{
    if (getObjects(Players.class).isEmpty() && getObjects(Yellow_Crushed.class).isEmpty())
    {
        addObject (new Yellow_Crushed(), 695, 393);
        addObject(new Yellow(), 163, 610);
    }
}
You need to login to post a reply.