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

2014/8/18

How do I set values in an array at the start of a class?

1
2
danpost danpost

2014/8/19

#
I will need to do some testing on that.
danpost danpost

2014/8/19

#
After some testing, I have found that even with certain pairings of keys, some other keys may not be detected. For example, if 'b' and 'n' are held down, 'z', 'x', 'c', 'v', 'a', and 'f' will not be detected (testing the letter keys only). I have found some combinations of up to eight different keys being detected at one time, however (well, at least one). I cannot say if the keyboard itself has anything to do with the results; but, it is highly possible that some keyboards may respond differently than others. I tested with this:
import greenfoot.*;

public class KeyWorld extends World
{
    private static final String keylist = "abcdefghijklmnopqrstuvwxyz";
    private Actor actor;
    private int lastCount;
    
    public KeyWorld()
    {
        super(100, 100, 1);
        actor = new Actor(){};
        actor.setImage(new GreenfootImage("0", 30, null, null));
        addObject(actor, 50, 50);
    }
    
    public void act()
    {
        int count = 0;
        for (int i=0; i<keylist.length(); i++)
            if (Greenfoot.isKeyDown(""+keylist.charAt(i)))
                count++;
        if (count != lastCount)
            actor.setImage(new GreenfootImage(""+(lastCount = count), 30, null, null));
    }
}
K_wow K_wow

2014/8/19

#
I have also done some testing with this code, and found that if I hold down enough keys for long enough, the number displays higher than zero after I have let go of all the keys. This is the problem I'm having with my greenfoot games. The computer thinks that keys are pressed when they're not. I have not tested this with other programs yet, although I think I have had this problem with non-greenfoot games before.
K_wow K_wow

2014/8/19

#
My father in IT says that it's just a problem with the keyboard, so there does not seem to be any way around the issue.
danpost danpost

2014/8/19

#
Best thing to do is use 'getKey' wherever possible and use 'isKeyDown' only when necessary; and spread out the possible down keys over the keyboard (since the problem tends to be more prominent when possible down keys are in the same area on the keyboard.
K_wow K_wow

2014/8/20

#
Well, seeing as pretty much all the actions in my greenfoot games run in a loop, 'getKey' would really limit my games... Also for things like diagonal movement, that requires multiple keys close together down at the same time. However, I do have a sort of fix:
if (Greenfoot.isKeyDown("e") || Greenfoot.isKeyDown("["))
            {
                Greenfoot.delay(120);
            }
It temporarily pauses the game so that players can press any keys that are locked in and reset them. It would be nice if I could just get the computer to do that itself though... I think my programming with Greenfoot may be coming to an end...
danpost danpost

2014/8/20

#
You should be able to detect at least two keys in close proximity to each other. That is enough for any eight-way movement key detection. I have used this in several scenarios. One is in my Super Level Support Class scenario. Test out the different directions/rotations that it detects. See how your keyboard responds with it. Then, check out the code for the Bug class.
K_wow K_wow

2014/8/20

#
Oh, the keyboard can detect at least two keys close together, and my game has no problem with single player, but the keyboard screws up in multiplayer, when there can be up to six keys held down at once.
You need to login to post a reply.
1
2