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

2015/11/23

How to make a retry and continue button

1
2
sanderbakker sanderbakker

2015/11/23

#
How can I make a button which directs me to the next level? So when I'm in level 3 I want it to go to level 4 and if it is in level 2 I want it to go to level 3. Also how can I make a button which return to the level where I'm at in that moment so when I'm in level 1 I want it to return to level 1 and if I'm in level 3 I want it return to level 3. I've made a world and when there are 3 birds shooted and the pig isn't killed it will show my gameover screen another class I've made. - So what I think I need to do is to check the last world I was in and then make the button return to that world. But how do I do this? Thanks in advance
Super_Hippo Super_Hippo

2015/11/23

#
If all worlds are similar, you can just have one world class and have a variable which is different for each level. Let's say the variable is called 'level' and the class is 'Level'.
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
35
36
37
38
39
private int level;
 
public Level()
{
    super(600,400,1);
    //everything which is same for every level
}
 
public Level(int lvl)
{
    this();
    level = lvl;
    switch (level)
    {
        case 1:
        //everything for level 1 here
        break;
         
        case 2:
        //everything for level 2 here
        break;
         
        //and so on
}
 
public void restartLevel()
{
    Greenfoot.setWorld(new Level(level));
}
 
public void nextLevel()
{
    Greenfoot.setWorld(new Level(level+1));
}
 
public void gameOver()
{
    //...
}
To start, you could have a Menu world and start the game with 'Greenfoot.setWorld(new Level(1));'. Hope this helps.
sanderbakker sanderbakker

2015/11/23

#
No all my worlds are diffrent form each other that's the whole point.
sanderbakker sanderbakker

2015/11/23

#
I've made several diffrent world's/level's in my game and they are all different from each other this is why I don't know how to do this
Super_Hippo Super_Hippo

2015/11/23

#
Then you can do it without the variables. For 'nextLevel', in Level1 you use:
1
Greenfoot.setWorld(new Level2());
And so on.
sanderbakker sanderbakker

2015/11/24

#
No no that's is not how it is fixed because my GameOver world is a class and my levels are also classes. So I want it to return to the last level I was in or go to the next level in line. You understand what I mean
danpost danpost

2015/11/24

#
sanderbakker wrote...
No no that's is not how it is fixed because my GameOver world is a class and my levels are also classes. So I want it to return to the last level I was in or go to the next level in line. You understand what I mean
Add two World type parameters to the GameOver world constructor and have them (the last level world and the next level one) passed to the buttons
sanderbakker sanderbakker

2015/11/24

#
What do you mean by this? What will my code look like?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import greenfoot.*;
 
/**
 * Write a description of class GameOver here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class GameOver extends World
{
 
    /**
     * Constructor for objects of class GameOver.
     *
     */
    public GameOver()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(325, 551, 1);
         
        addObject(new Menu(), 163, 495);
         
    }
}
Add the moment I have this. I know I need to add two Object called "Continue" & "Retry" but after that I'm stuck
danpost danpost

2015/11/24

#
Maybe I was too hasty about saying to pass them to the buttons (doing that is one way to do it; but, it is not necessary). My Value Display Tutorial scenario might be of some help here. It shows how to create an object that displays a value (in this case, a constant string value -- 'Continue' or 'Retry') and how you can detect actions on them by keeping references to them in your GameOver world class. You just need the worlds passed to and saved to fields in the GameOver world. You need a constructor in the GameOver world with the following declaration line:
1
public GameOver(World lastWorld, World nextWorld)
and any level can then do this (level one to level 2):
1
2
3
// in a method in the Level1 world class
Level2 nextLevel = new Level2();
Grenfoot.setWorld(new GameOver(this, nextLevel));
sanderbakker sanderbakker

2015/11/24

#
So I need to add the the first code you gave me in the GameOver class and the other code in my level 1 class?
sanderbakker sanderbakker

2015/11/24

#
I've made three button in my greenfoot class. 1 button for retry one for continue and one for menu. menu is done. But the 2 other classes how do I make them to redirect them to the next or the same level I was in at that moment
Super_Hippo Super_Hippo

2015/11/24

#
After the line danpost gave you, you can use the variables 'lastWorld' and 'nextWorld', which are the worlds the buttons will lead to, to create the buttons. So for example:
1
2
3
4
5
6
public GameOver(World lastWorld, World nextWorld)
{
    this();
    addObject(new Button(lastWorld), 100, 100); //Retry
    addObject(new Button(nextWorld), 100, 200); //Continue
}
And then, in the classes for the buttons:
1
2
3
4
5
6
7
8
9
10
11
World target;
public Button(World w)
{
    //...creating image
    target = w;
}
 
public void act()
{
    if (Greenfoot.mousePressed(this)) Greenfoot.setWorld(target);
}
danpost danpost

2015/11/24

#
Like I said before, if you save references (add fields) in your GameOver world class to hold the 'last' and 'next' worlds AND add references (two more fields) to hold the buttons you created for 'Continue' and 'Retry', the GameOver world class act method can then be coded to detect mouse clicks on those object and go their respective worlds.
Super_Hippo Super_Hippo

2015/11/24

#
Edit last post: In the *class* for the buttons. (Couldn't edit fast enough, before the next post was made) :P Or you do it like danpost just said.
sanderbakker sanderbakker

2015/11/24

#
Super_Hippo wrote...
After the line danpost gave you, you can use the variables 'lastWorld' and 'nextWorld', which are the worlds the buttons will lead to, to create the buttons. So for example:
1
2
3
4
5
6
public GameOver(World lastWorld, World nextWorld)
{
    this();
    addObject(new Button(lastWorld), 100, 100); //Retry
    addObject(new Button(nextWorld), 100, 200); //Continue
}
And then, in the classes for the buttons:
1
2
3
4
5
6
7
8
9
10
11
World target;
public Button(World w)
{
    //...creating image
    target = w;
}
 
public void act()
{
    if (Greenfoot.mousePressed(this)) Greenfoot.setWorld(target);
}
In this code you gave me is it only possible to return to 1 selected world but I want to go to different worlds so I need to see which world I was in before and then redirect to another one.
There are more replies on the next page.
1
2