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

2017/5/11

How to use isKeyDown() to add only one object

brookexann brookexann

2017/5/11

#
I am trying to make a method that checks if two keys are pressed. If they are, it adds an object to the screen. It all works correctly except even if I only press the keys once, multiple of the same object is added to the same place. I only want one object to be added. Here is my code:
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
public class spawn1 extends spawns
{
    /**
     * Act - do whatever the spawn1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        spawn();
    }
    public void spawn()
    {
        int numWalkers1 = 0;
        int numSpiders1 = 0;
        int numCreepers1 = 0;
        if (Greenfoot.isKeyDown("1") && Greenfoot.isKeyDown("w") && numWalkers1 < 1)
        {
            getWorld().addObject(new walker(), 110, 160);
            numWalkers1 = 1;
        }
        if (Greenfoot.isKeyDown("1") && Greenfoot.isKeyDown("s") && numSpiders1 < 1)
        {
            getWorld().addObject(new spider(), 110, 160);
            numSpiders1 += 1;
        }
        if (Greenfoot.isKeyDown("1") && Greenfoot.isKeyDown("c") && numCreepers1 < 1)
        {
            getWorld().addObject(new creeper(), 110, 160);
            numCreepers1 += 1;
        }
    }
}
lehrerfreund lehrerfreund

2017/5/11

#
Instead of the isKeyDown-method you might have to use getKey(), like
1
2
3
String pressedKey = Greenfoot.getKey()
if("left".equals(pressedKey)
    this.move(-3)
danpost danpost

2017/5/11

#
Probably the best way to fix it is to track the state of the keys that are individual to each spawning:
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
// add instance field
private boolean wDown;
private boolean sDown;
private boolean cDown;
// I think these should be fields also
private int numWalkers1;
private int numSpiders1;
private int numCreepers1;
 
// spawn method
public void spawn()
{
    if (wDown != Greenfoot.isKeyDown("w"))
    {
        wDown = !wDown
        if (wDown && Greenfoot.isKeyDown("1"))
        {
            getWorld().addObject(new walker(), 110, 160);
            numWalkers += 1;
        }
    }
    if (sDown != Greenfoot.isKeyDown("s"))
    // similar
    // and similar for "c"
}
lehrerfreund lehrerfreund

2017/5/11

#
(I am not able to edit my post, so I meant:
1
2
3
4
5
6
7
8
private String pressedKey = Greenfoot.getKey()
 
public void act(){
if("left".equals(pressedKey))
   {
    this.move(-3)
   }
}
Super_Hippo Super_Hippo

2017/5/11

#
@lehrerfreund: 'pressedKey' would be 'null' and never change (despite the fact that there are two semicolons missing).
lehrerfreund lehrerfreund

2017/5/11

#
@super_hippo Yes, you are right; the reason is that I am using Stride and most of my examples lack brackets and semicolons. Unfortunately I cannot edit or delete my posts for some reason. So I think you have to initialize pressedKey not with declaration but at the beginning of every act-cycle like
1
2
3
4
5
6
7
8
9
private String pressedKey;
  
public void act(){
pressedKey = Greenfoot.getKey();
if("left".equals(pressedKey))
   {
    this.move(-3);
   }
}
Sorry again to all for spamming a lot of errors, but as mentioned I cannot edit or delete my posts.
Super_Hippo Super_Hippo

2017/5/11

#
Yes. You can always only edit/remove your post if it is the very last in the discussion. If there is any other post below, it is not working anymore.
You need to login to post a reply.