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

2016/12/27

Reset Button After Greenfoot.stop

Davey1999 Davey1999

2016/12/27

#
I am trying to create a reset button to appear after the scenario stops so that when I click on the reset button it sets the world again, but when I try to click on the button I made to reset the world, I cannot because the scenario is stopped and the mouse click does not work. Here is my coding for my actor that when he touches any of the methods below it will end the game and the end screen (scoreboard) appears with the reset button:
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
40
41
42
43
/**
     * touchLine() - if touching line stop game
     */
    public void touchBlueLine()
    {
        int range = 1; //set the range to 1 pixel
        List<Dot1> dots = getObjectsInRange (range, Dot1.class); //Make a list for dots to getObjectsInRange of dots
        for(Dot1 d : dots)
        {
            if (d != null) //if d is not equal to null
            {
                Greenfoot.stop(); //stop the game
                getWorld().addObject(new ScoreBoard(), 400, 300);
                getWorld().addObject(new ResetButton(), 440, 340);
            }
        }
    }
 
    /**
     * touchLine2() - if touching line stop game
     */
    public void touchRedLine()
    {
        if (isTouching(Dot2.class)) //Code 3.2 - if Player is touching dot do the following
        {
            Greenfoot.stop(); //stop the game
            getWorld().addObject(new ScoreBoard(), 400, 300);
            getWorld().addObject(new ResetButton(), 440, 340);
        }
    }
 
    /**
     * isAtEdge()
     */
    private void touchesEdge()
    {
        if (isAtEdge()) //if actor is at the edge of the world do the following
        {
            Greenfoot.stop(); //end game
            getWorld().addObject(new ScoreBoard(), 400, 300);
            getWorld().addObject(new ResetButton(), 440, 340);
        }
    }
Is there a way to stop the game while being able to click the reset button. I tried changing the code so that instead of Greenfoot.stop() it would remove all the objects in the world, but then I couldn't add the scoreBoard and resetButton and error messages would pop up for some reason.
danpost danpost

2016/12/27

#
Davey1999 wrote...
Is there a way to stop the game while being able to click the reset button. I tried changing the code so that instead of Greenfoot.stop() it would remove all the objects in the world, but then I couldn't add the scoreBoard and resetButton and error messages would pop up for some reason.
You can get a reference to the world before clearing it of all actors:
1
World world = getWorld();
then after clearing the world, you can still add the ScoreBoard and ResetButton objects to the world, not by using 'getWorld()', but by using 'world'.
Davey1999 Davey1999

2016/12/27

#
Ok I tried your suggestion but now whenever I go into the line and try to remove all objects a terminal pops up called: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at Player1.generateLine(Player1.java:79) at Player1.act(Player1.java:41) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at Player1.touchRedLine(Player1.java:119) at Player1.act(Player1.java:39) Here is my full code with generate line method:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class Player1 extends Actor
{
    private int delay; //call delay
    private static final int MAX_DELAY = 52; //set delay to 52 seconds
 
    public void act()
    {
        if (delay < MAX_DELAY)  //if delay is less than 52 seconds
        {
            delay++; //increment delay
            return; //return to previous running state
        }
        checkKeyPress();
        move(6); //2.2 Move forward 6 pixels
        touchBlueLine();
        touchRedLine();
        touchesEdge();
        generateLine();
    }
 
    /**
     * Check whether a keyboard key has been pressed and react if it has.
     */
    private void checkKeyPress()
    {
        if (Greenfoot.isKeyDown("w")) //if "w" key is down do the following
        {
            setRotation(-90); //change direction to North
        }
 
        if (Greenfoot.isKeyDown("s")) //if "s" key is down do the following
        {
            setRotation(90); //change direction to South
        }
 
        if (Greenfoot.isKeyDown("a")) //if "a" key is down do the following
        {
            setRotation(180); //change direction to West
        }
 
        if (Greenfoot.isKeyDown("d")) //if "d" key is down do the following
        {
            setRotation(360); //change direction to East
        }
    }
 
    private void generateLine()
    {
        World world = getWorld(); //call variable world
        if (getRotation() == 90) //if rotation = 90 degrees do the following
        {
            world.addObject(new Dot1(), getX(), getY() -5); //add new dot 5 pixels behind
        }
        if (getRotation() == 270) //if rotation = 270 degrees do the following
        {
            world.addObject(new Dot1(), getX(), getY() +5); //add new dot 5 pixels behind
        }
        if (getRotation() == 0) //if rotation = 0 degrees do the following
        {
            world.addObject(new Dot1(), getX() -5, getY()); //add new dot 5 pixels behind
        }
        if (getRotation() == 180) //if rotation = 180 degrees do the following
        {
            world.addObject(new Dot1(), getX() +5, getY()); //add new dot 5 pixels behind
        }
    }
 
    /**
     * touchLine() - if touching line stop game
     */
    public void touchBlueLine()
    {
        int range = 1; //set the range to 1 pixel
        List<Dot1> dots = getObjectsInRange (range, Dot1.class); //Make a list for dots to getObjectsInRange of dots
        for(Dot1 d : dots)
        {
            if (d != null) //if d is not equal to null
            {
                World world = getWorld();
                getWorld().removeObjects(getWorld().getObjects(null));
                world.addObject(new ScoreBoard(), 400, 300);
                world.addObject(new ResetButton(), 440, 340);
            }
        }
    }
 
    /**
     * touchLine2() - if touching line stop game
     */
    public void touchRedLine()
    {
        if (isTouching(Dot2.class)) //Code 3.2 - if Player is touching dot do the following
        {
            World world = getWorld();
            getWorld().removeObjects(getWorld().getObjects(null));
            world.addObject(new ScoreBoard(), 400, 300);
            world.addObject(new ResetButton(), 440, 340);
        }
    }
 
    /**
     * isAtEdge()
     */
    private void touchesEdge()
    {
        if (isAtEdge()) //if actor is at the edge of the world do the following
        {
            World world = getWorld();
            getWorld().removeObjects(getWorld().getObjects(null));
            world.addObject(new ScoreBoard(), 400, 300);
            world.addObject(new ResetButton(), 440, 340);
        }
    }
}
danpost danpost

2016/12/27

#
Yeah, you will get that because you calling method after method that could remove the actor from the world. With each line, 16 through 18, add the following condition to each call:
1
if (getWorld() != null)
Davey1999 Davey1999

2016/12/28

#
Thank you it works!!! I spent hours trying to figure this out!!!
You need to login to post a reply.