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

2014/6/27

Image Display when Key is Pressed

CaptainMorgan CaptainMorgan

2014/6/27

#
I need to make the question then the answer appear when I press space but I'm not sure what the code is and I can't find it. Help?
1
2
3
4
5
Used_1 = 1
                setImage (new GreenfootImage("27 - 26 ?", 46, Color.BLACK,Color.WHITE)); 
                Greenfoot.delay (120); 
                getWorld().addObject(new Counter(), 28, 28); 
                Greenfoot.isKeyDown("space");
That's all the code I've got.
IeuanMcCrushems IeuanMcCrushems

2014/6/27

#
Try putting the Greenfoot.isKeyDown("space"); before the set Image
NikZ NikZ

2014/6/27

#
isKeyDown() should be in an if statement
1
2
3
4
//example:
if (Greenfoot.isKeyDown("space")) {
      //do something
}
NikZ NikZ

2014/6/27

#
You can also use getKey() (returns the last pressed key as a char)
danpost danpost

2014/6/27

#
NikZ wrote...
You can also use getKey() (returns the last pressed key as a char)
Actually, it return the next key in the keyboard buffer (if there is one) as that keys String designation. This buffer will store keys in the order they were pressed and each time 'getKey' is called the next key in the buffer is returned and that key is essentially removed from the buffer (until no keys are left -- when it will return a 'null' value). So -- it may not necessarily return the last pressed key; and, it does not return a 'char' value.
CaptainMorgan CaptainMorgan

2014/7/1

#
Ok NikZ I've made Greenfoot.isKeyDown("space"); an if statement and I've moved it so it's above setImage but I can't get it to work, any ideas?
danpost danpost

2014/7/1

#
How about removing the semi-colon after the 'if' conditional and placing the code you want to execute if the condition is true within curly brackets.
NikZ NikZ

2014/7/2

#
danpost wrote...
How about removing the semi-colon after the 'if' conditional and placing the code you want to execute if the condition is true within curly brackets.
1
2
3
4
5
if (Greenfoot.isKeyDown("space")) {
    setImage (new GreenfootImage("27 - 26 ?", 46, Color.BLACK,Color.WHITE));   
    Greenfoot.delay (120);   
    getWorld().addObject(new Counter(), 28, 28); 
}
NikZ NikZ

2014/7/2

#
As you can see, the curly brackets have the code to be executed inside. You should (if your colors are on) see the if statement line shaded blue, as with the ending bracket. The if statement is something you cannot execute.
CaptainMorgan CaptainMorgan

2014/7/3

#
Ok, I've tried this but is this code meant to allow the if (Greenfoot.isKeyDown("space")) to place a counter on the coordinates when it's pressed?
IeuanMcCrushems IeuanMcCrushems

2014/7/3

#
Morgan?
IeuanMcCrushems IeuanMcCrushems

2014/7/3

#
I know you saw that
7788 7788

2014/7/3

#
8877
IeuanMcCrushems IeuanMcCrushems

2014/7/3

#
7788
danpost danpost

2014/7/3

#
CaptainMorgan wrote...
Ok, I've tried this but is this code meant to allow the if (Greenfoot.isKeyDown("space")) to place a counter on the coordinates when it's pressed?
As the code is shown by NikZ, counters will be placed at location (28, 28) when the space key is pressed.
You need to login to post a reply.