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

2013/10/22

more getKey's

ddvink ddvink

2013/10/22

#
Because I need more situations where a getKey is more elegant then the isKey, I was wondering if that is possible, because of....
      if ("a".equals(Greenfoot.getKey()))
        {
            sequence = 4;
            luckySeven(richting);
        }
        if (getWorld().getObjects(LevelUp.class).isEmpty() && Greenfoot.isKeyDown("u")){
            levelUp = new LevelUp();
            getWorld().addObject(levelUp, getX(), getY()-181+70);
        }
        if (getWorld().getObjects(Thombstone.class).isEmpty() && Greenfoot.isKeyDown("y")){
            thombstone = new Thombstone();
            getWorld().addObject(thombstone, getX(), -200);
        }

        if ("b".equals(Greenfoot.getKey()))
        {
            System.out.println("De b key is ingedrukt");
        }
This code only reacts on the 'a' key and doesn't seem to do anything with the 'b' key.... Is there a solution for this? Yours, Dennis
davmac davmac

2013/10/22

#
Calling getKey() consumes the key press. If you want to check for several values, you need to save the return value in a variable.
String key = Greenfoot.getKey();
if ("a".equals(key)) {
    ...
}

if ("b".equals(key)) {
    ...
}
ddvink ddvink

2013/10/22

#
Thanx!!!!!
You need to login to post a reply.