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

2012/12/12

Key press question

BradH BradH

2012/12/12

#
if((Greenfoot.isKeyDown("a"))) if("a".equals(Greenfoot.getKey())) These are two ways to call on a key being pressed to then make something happen in my case the key press spawns a unit, the 2nd one is what I want to use for all the actors in one of my worlds, because when "a" is pressed it is to spawn a unit, when I use the first one if i press the key only once it spawns like 3 or 4 guys. For some reason I can only use the 2nd one once in my world, so my question would be can you somehow use the 2nd one for all the actors in a single world, thanks for your time
danpost danpost

2012/12/12

#
What kind of action is supposed to happen to all the other actors when the key is registered? Actually, that does not matter because if you use the 'isKeyDown' method for one, then as soon as the key is released for that action, the other action will occur anyway (whether you want it to or not).
BradH BradH

2012/12/12

#
In my game a,s,d,f,g,h, each spawn a different unit (each unit costs gold to create). I was just wondering when I have all the buttons spawn a unit and I assign("a".equals(Greenfoot.getKey())) for each key (changing the "a" to "s" and so on) then the keys will not spawn the unit that each key is specialized for, it only works if I have one key assigned with that 2nd code and the other keys assigned with that first code. for example if "a" is pressed then it should spawn one knight which it does when I use if("a".equals(Greenfoot.getKey())). When I use if((Greenfoot.isKeyDown("a"))) and the key is pressed it spawns 3 or 4 knights instead of one I would like to only spawn one each time the key is pressed. is there a way for this to happen? *this is kind of hard to explain so thanks for your patience
danpost danpost

2012/12/12

#
I believe the reason is does not work with the one method is because after the first call to 'getKey', the rest of them return 'null'. Try setting a String field to the key returned and compare that string with the possible keys.
String key = Greenfoot.getKey();
if("a".equals(key)) // spawn object one
if("b".equals(key)) // spawn object two
// etc.
BradH BradH

2012/12/13

#
Ok I will try it out thanks
BradH BradH

2012/12/13

#
Just to verify I would go to each class and type a field 01.String key = Greenfoot.getKey(); 02.if("a".equals(key)) // spawn object one 03.if("s".equals(key)) // spawn object two 04.// etc.
danpost danpost

2012/12/13

#
Why not just put it in the world 'act' method.
BradH BradH

2012/12/13

#
public class Multiplayer extends World { String key = Greenfoot.getKey(); public void act() { if("a".equals(key)) if("s".equals(key)) if("d".equals(key)) if("f".equals(key)) if("g".equals(key)) if("h".equals(key)) } } I have the field then an act method below with the (.equal(key))
danpost danpost

2012/12/13

#
public class Multiplayer extends World
{
    // constructor and instance fields
    
    public void act()
    {
        String key = Greenfoot.getKey();
        if("a".equals(key)) addObject(new ObjectOne(), nnn, nnn);
        if("s".equals(key)) addObject(new ObjectTwo(), nnn, nnn);
        // etc.
    }
You have to supply the Class names to replace 'ObjectOne' and 'ObjectTwo' plus the rest.
You need to login to post a reply.