This site requires JavaScript, please enable it in your browser!
Greenfoot back
rachpaguia@gmail.com
rachpaguia@gmail.com wrote ...

2017/3/16

end game

this is my code when my program ends
1
2
3
4
removeObjects(getObjects(A.class)); //this removes all actors from world
    GreenfootImage bg = getBackground();
    setBackground("Gameover.png");
    Greenfoot.stop(); //this stops the scenario
What code must I add so that the program will only end when an actor touches the bottom of the screen?
danpost danpost

2017/3/16

#
The code given should be inside a 'gameOver' method. In the actor class (the one that ends the game when touches bottom of the screen), put in act method:
1
2
3
4
5
if (getY() == getWorld().getHeight()-1)
{
    ((Bg).getWorld()).gameOver();
    return;
}
Thank you! When I inserted this in my code though, line 3 is an error because ".getworld" method cannot be found
Super_Hippo Super_Hippo

2017/3/16

#
Remove the dot (.) right in front of it.
Thank you! Is this how my gameover background's code will look like?
1
2
3
4
5
6
7
8
9
10
11
public class go extends World
{
     public void gameOver()
{
    {   
        setBackground("GO.png");
        GreenfootImage go = new GreenfootImage("GO.png");
        go.scale(getWidth() +1, getHeight() +1);
    }
}
}
danpost danpost

2017/3/16

#
I am a bit confused, I thought (by the previous code) that you were using the same world (the 'Bg' world). Now, it looks like you are going to change worlds altogether -- but I have not seen any codes that would change worlds. The last code snippet is not quite right -- but it is not like it may not work at all. More information is needed to determine what your code should look like.
I'm sorry for the confusion but I was planning to switch backgrounds once an actor touches the bottom of the screen. The new background says something like "gameover" here is my code for my bg world
1
2
3
4
5
6
7
   public void gameOver()
{
    removeObjects(getObjects(A.class)); //this removes all actors from world
    GreenfootImage go = getBackground();
    setBackground("GO.png");
    Greenfoot.stop(); //this stops the scenario
}
here is my code for my go world
1
2
3
4
5
6
7
8
9
10
11
public class go extends World
{
     public void gameOver()
{
    {   
        setBackground("GO.png");
        GreenfootImage go = new GreenfootImage("GO.png");
        go.scale(getWidth() +1, getHeight() +1);
    }
}
}
danpost danpost

2017/3/16

#
If you are switching backgrounds, then you do not need a 'go' world at all. Remove the 'go' class from your project (it should not compile as is, anyway). Change the 'gameOver' method to this:
1
2
3
4
5
6
public void gameOver()
{
    removeObjects(getObjects(Actor.class));
    setBackground("GO.png");
    Greenfoot.stop();
}
The 'if' statement I gave above, with the correction supplied by Hippo (thanx) along with this should be all you need.
Thank you! My problem is that my background isn't scaled to the screen of greenfoot. Will i be able to use this code to adjust?
1
bg.scale(getWidth() +35, getHeight() +30);
Super_Hippo Super_Hippo

2017/3/16

#
Why do you want the background to be larger than the visible world?
danpost danpost

2017/3/16

#
rachpaguia@gmail.com wrote...
My problem is that my background isn't scaled to the screen of greenfoot.
Then use this:
1
2
3
4
5
6
7
8
public void gameOver()
{
    removeObjects(getObjects(Actor.class));
    GreenfootImage bg = new GreenfootImage("GO.png");
    bg.scale(getWidth(), getHeight());
    setBackground(bg);
    Greenfoot.stop();
}
You need to login to post a reply.