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

2018/5/1

if key is down

Miguel.Valdez Miguel.Valdez

2018/5/1

#
I have a simple code that populates in my menu (showing a certain text), and when key ("space") is pressed it gets removed... but how could I make it so that if ("space") is pressed again, the text shows again. Making so that When game begins, instructions should appear on the screen and stay on the screen until the user presses the spacebar. If the user presses the space bar again, the instructions should re-appear.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class GameInstructions extends Actor
{
    /**
     * Act - do whatever the GameInstructions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        //setImage(new GreenfootImage("Use/hold UP arrow key to prevent hitting any obstacles, ENJOY!",30, Color.WHITE, Color.BLACK));
        show();
    }
     
    public void show()
    {
        setImage(new GreenfootImage("Use/hold UP arrow key to prevent hitting any obstacles, ENJOY!",30, Color.WHITE, Color.BLACK));
        if (Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObject(this);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Menu extends World
{
    static int xDimension       = 800;
    static int yDimension       = 600;
    GameInstructions instruc = new GameInstructions();
     
    /**
     * Constructor for objects of class Menu.
     *
     */
    public Menu()
    {   
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(xDimension, yDimension, 1, false);
         
        setPaintOrder (GameInstructions.class, Play.class);
        addObject(new Play(), xDimension/2, yDimension/2);
         
        addObject(instruc, 397, 388);
danpost danpost

2018/5/2

#
Track the state of the space key being up or down. Use it to determine when the key changes states. Only act on one of the changes (going down or going up). To determine whether to add or remove the object, just ask if it in the world (are any objects of its type in the world -- or, is the world empty of that type object).
Miguel.Valdez Miguel.Valdez

2018/5/2

#
So i have something like this down, but still isnt doing the job...
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
public class GameInstructions extends Actor
{
    public boolean spaceDown;
    /**
     * Act - do whatever the GameInstructions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        show();
    }
     
    public void show()
    {
        setImage(new GreenfootImage("Use/hold UP arrow key to prevent hitting any obstacles, ENJOY!", 30, Color.WHITE, Color.BLACK));
        if (spaceDown != Greenfoot.isKeyDown("space"))
        {
            getWorld().removeObject(this);
            spaceDown =! spaceDown;
            if (spaceDown != Greenfoot.isKeyDown("space"))
            {
                setImage(new GreenfootImage("Use/hold UP arrow key to prevent hitting any obstacles, ENJOY!", 30, Color.WHITE, Color.BLACK));
            }
        }
    }
Super_Hippo Super_Hippo

2018/5/2

#
Add that method:
1
2
3
4
public GameInstructions()
{
    //move line 15 here
}
Line 20 should be
1
if (spaceDown)
And after that you need an else which removes the image / make it transparent instead of removing it in 18. If you want to do it with adding/removing, you have to place the code into your world.
danpost danpost

2018/5/2

#
Oh my -- no. Control of the showing needs to be done by your world, not by that which you want to show. Also, what makes you think line 20 would ever be true to execute line 22? If line 16 was ever true, line 19 would make that same set of conditions false -- always. The GameInstructions object has no actions -- it just exists (whether in the world or not). The class can simply be this:
1
2
3
4
5
6
7
public class GameInstructions extends Actor
{
    public GameInstructions()
    {
        setImage(new GreenfootImage("Use/hold UP arrow key to prevent hitting any obstacles, ENJOY!", 30, Color.WHITE, Color.BLACK));
    }
}
The field tracking the key and action codes to detect the key and add/remove the GameInstructions object go in your world subclass.
Miguel.Valdez Miguel.Valdez

2018/5/3

#
danpost wrote...
Oh my -- no. Control of the showing needs to be done by your world, not by that which you want to show. Also, what makes you think line 20 would ever be true to execute line 22? If line 16 was ever true, line 19 would make that same set of conditions false -- always. The GameInstructions object has no actions -- it just exists (whether in the world or not). The class can simply be this:
1
2
3
4
5
6
7
public class GameInstructions extends Actor
{
    public GameInstructions()
    {
        setImage(new GreenfootImage("Use/hold UP arrow key to prevent hitting any obstacles, ENJOY!", 30, Color.WHITE, Color.BLACK));
    }
}
The field tracking the key and action codes to detect the key and add/remove the GameInstructions object go in your world subclass.
So now my game instructions class only likes like what you showed.... and I moved everything else to the world subclass(here being Menu)
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
public class Menu extends World
{
    static int xDimension       = 800;
    static int yDimension       = 600;
    GameInstructions instruc    = new GameInstructions();
     
    /**
     * Constructor for objects of class Menu.
     *
     */
    public Menu()
    {   
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(xDimension, yDimension, 1, false);
         
        addObject(instruc, 397, 388);
    }
     
    public void act()
    {
        show();
    }
     
    private void show()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            removeObject(instruc);
        }
    }
}
I'm just not quite sure how to add a tracker so that if the space key is pressed again it shows the game instructions again... would a boolean be involved?
danpost danpost

2018/5/3

#
Miguel.Valdez wrote...
I'm just not quite sure how to add a tracker so that if the space key is pressed again it shows the game instructions again... would a boolean be involved?
Yes, a boolean to retain the last known state of the "space" key -- you could call it spaceDown.
Miguel.Valdez Miguel.Valdez

2018/5/3

#
danpost wrote...
Miguel.Valdez wrote...
I'm just not quite sure how to add a tracker so that if the space key is pressed again it shows the game instructions again... would a boolean be involved?
Yes, a boolean to retain the last known state of the "space" key -- you could call it spaceDown.
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
public class Menu extends World
{
    static int xDimension       = 800;
    static int yDimension       = 600;
    GameInstructions instruc    = new GameInstructions();
    boolean spaceDown           = false;
     
    /**
     * Constructor for objects of class Menu.
     *
     */
    public Menu()
    {   
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(xDimension, yDimension, 1, false);
         
        addObject(instruc, 397, 388);
    }
     
    public void act()
    {
        show();
    }
     
    private void show()
    {
        if (spaceDown == false && Greenfoot.isKeyDown("space"))
        {
            removeObject(instruc);
            spaceDown = true;
        }
        if (spaceDown != true && Greenfoot.isKeyDown("space"))
        {
            addObject(instruc, 397, 388);
            spaceDown = false;
        }
    }
}
So I have something like this now, am I on the right track? :/ The image will disapear, but will not reappear if space is pressed again.
danpost danpost

2018/5/3

#
Line 32 is asking the exact same thing as line 27 -- just in a different way (being false is the same as not being true). So, when the conditions are met, the value is set to true, and never can be set back to false. (hence, no reappearance). Try comparing both conditions to each other (once).
Miguel.Valdez Miguel.Valdez

2018/5/3

#
danpost wrote...
Line 32 is asking the exact same thing as line 27 -- just in a different way (being false is the same as not being true). So, when the conditions are met, the value is set to true, and never can be set back to false. (hence, no reappearance). Try comparing both conditions to each other (once).
Lol I just noticed that hahah, idk what I was thinking... so right now my theory behind my code is that when space key is pressed, it removes the object & changes spaceDown to be true and then being spaceDown is = to true & if space is pressed it adds the object and sets spaceDown = false. But you say to compare the the conditions to each other. Could I get a hint in how to do this? (I don't want the exact answer, I like the problematic sense behind coding. It's why I'm taking CS classes lol) P.S. sorry for errors in my text, I'm on my phone right now.
Miguel.Valdez Miguel.Valdez

2018/5/3

#
1
2
3
4
5
6
7
8
9
10
11
12
13
private void show()
{
    if (spaceDown == false && Greenfoot.isKeyDown("space"))
    {
        removeObject(instruc);
        spaceDown = true;
         
    } else if (spaceDown == true && Greenfoot.isKeyDown("space"))
    {
        addObject(instruc, 397, 388);
        spaceDown = false;
    }
}
now this is just making it appear and disappear really fast when i press space.... so something is still off.
danpost danpost

2018/5/3

#
You should compare the field value against the current state:
1
if (spaceDown != Greenfoot.isKeyDown("space"))
to detect a change in the state of the key. When they are different, update the boolean field to match the new state:
1
spaceDown = ! spaceDown;
and check to see if the key way pressed:
1
2
3
4
if (spaceDown)
 
// or (if you prefer to perform an operation on the boolean first)
if (spaceDown == true)
This is when you either add or remove the instructions to/from the world. A simple check to determine which to do is:
1
if (instruc.getWorld() == null) /** add */ else /** remove */
Miguel.Valdez Miguel.Valdez

2018/5/3

#
danpost wrote...
You should compare the field value against the current state:
1
if (spaceDown != Greenfoot.isKeyDown("space"))
to detect a change in the state of the key. When they are different, update the boolean field to match the new state:
1
spaceDown = ! spaceDown;
and check to see if the key way pressed:
1
2
3
4
if (spaceDown)
 
// or (if you prefer to perform an operation on the boolean first)
if (spaceDown == true)
This is when you either add or remove the instructions to/from the world. A simple check to determine which to do is:
1
if (instruc.getWorld() == null) /** add */ else /** remove */
I figure it out!!!! thank you. I have another simple question, which I aswell referenced to an old post you made. about stringing a class name into the System.out.print. but i will start another thread.
You need to login to post a reply.