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

2014/4/2

Limiting a keyDown

docness14 docness14

2014/4/2

#
Is there anyway to limit a keyDown to run only once? For example I want to roll two dice but want it for everytime space is pressed, but when space is held it continuously rolls both dice. Here is the part of the code for the keyDown: if (Greenfoot.isKeyDown("space")) { roller = false; int xx = Greenfoot.getRandomNumber(6);
bourne bourne

2014/4/2

#
Check out Greenfoot.getKey() (note that this method returns null when no key has been pressed). Or you can do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
boolean spaceKeyIsDown = false;
...
if (Greenfoot.isKeyDown("space")) {
    if (!spaceKeyIsDown) {
 
        // Do stuff
 
        spaceKeyIsDown = true;
    }
}
else
    spaceKeyIsDown = false;
docness14 docness14

2014/4/2

#
That seems to be doing the exact same thing that was happening earlier, here is the code I have so far
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
private void checkThrow()
{
     
    if (Greenfoot.isKeyDown("space"))
         {
          
             roller = false;
             int xx = Greenfoot.getRandomNumber(6);
             if(xx == 0)
             {
                 xx = 6;
                }
             num = xx;
             if (xx == 1)
             {
               setImage("1.png");
            }
            if (xx == 2)
             {
               setImage("2.png");
            }
            if (xx == 3)
             {
               setImage("3.png");
            }
            if (xx == 4)
             {
               setImage("4.png");
            }
            if (xx == 5)
             {
               setImage("5.png");
            }
            if (xx == 6)
             {
               setImage("6.png");
            }
            }
            else
            {
                timer++;
            }
bourne bourne

2014/4/2

#
Oh so you have this in the Dice class, so each die is executing this code per act cycle.
docness14 docness14

2014/4/2

#
Yeah, but the problem is if its held down it keeps executing the dice roll until it is let go, I want it to execute once everytime space is pushed.
bourne bourne

2014/4/2

#
Yes, what I did above makes it only execute once per time the spacebar is pressed. The problem is you have two different objects and I guess you don't want both to go per time the spacebar is pressed. So you are going to need to share something like my spaceKeyIsDown variable between the instances.
docness14 docness14

2014/4/2

#
How would I do that? (Sorry for all the question I'm a beginner in coding!)
bourne bourne

2014/4/2

#
Well you could make the variable spaceKeyIsDown a static class field. But then I think you will run into the problem that the first dice who acts will roll and never the others. To overcome this, once a dice has been rolled, it should no longer listen for the spacebar being pressed. But then you might need to consider when the dice should listen again. In addition to making sure the actual "first" dice rolls first i.e. it acts first before the others in the World. An easier approach might be to handle all this somewhere else like in the World class, and each time the spacebar is pressed, a new dice gets added to the World (and rolls itself) etc.
You need to login to post a reply.