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

2020/5/24

Is there any other way to get user keyboard input than getKey and isKeyDown?

mshep mshep

2020/5/24

#
Hi, I'm relatively new to Greenfoot and I'm trying to make a game where "kirby" can first swallow "enemies", and the user can either press "s" to copy its ability and "e" to spit it out to hit other enemies. To "swallow", the user presses space, and then after swallowed the user can press e or s. I use isKeyDown for checking first if the user pressed "space" and then after "swallow" method is called s or e. It works, but it looks like I have to press space and e or s at the same time, and it's really hard to control. Is there any other way of doing this?
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
public void act(){
if(Greenfoot.isKeyDown("space")){//if space pressed swallow
        swallow();
        }
}
public void swallow(){//swallow
        setImage("swallowingkirby.png");
        List enemies=getObjectsInRange(150,enemies.class);
        if(enemies.size()>0){
            Actor enemy = (Actor)enemies.get(0);
            setImage("swallowed.png");
            swallowed=true;
            if(Greenfoot.isKeyDown("e")){
                if(swallowed){
                spitout();
                swallowed=false;}
            }
            else if(Greenfoot.isKeyDown("sz")){
            for(int x=0;x<enemies.size();x++){
                if(enemy instanceof swordenemies){//if swallow sword enemy, become sword kirby
                getWorld().addObject(new swordkirby(),getX(),getY());
                 
                getWorld().removeObject(this);}
                else if(enemy instanceof neutralenemies){
                getWorld().removeObject(enemy);
                }
                else if(enemy instanceof throwenemy){
                getWorld().addObject(new throwkirby(),getX(),getY());
                getWorld().removeObject(enemy);
                getWorld().removeObject(this);   
                }
              }
            }
           }
   
    }
danpost danpost

2020/5/24

#
There are multiple problems with the shown code. However, to deal with your stated issue, everything from line 13 down should be in different methods. I would start with this act method:
1
2
3
4
5
6
public void act()
{
    if (!swallowed && Greenfoot.isKeyDown("space")) swallow();
    if (swallowed && Greenfoot.isKeyDown("s")) copyAbility();
    if (swallowed && Greenfoot.isKeyDown("e")) spitOut();
}
The major problem with this, however, is that the enemies object won't be available to copy its ability. To account for this, instead of using the boolean swallowed, use an enemies swallowed field: private boolean swallowed private enemies swallowed; Then the act method will be:
1
2
3
4
5
6
public void act()
{
    if (swallowed == null && Greenfoot.isKeyDown("space")) swallow();
    if (swallowed != null && Greenfoot.isKeyDown("s")) copyAbility();
    if (swallowed != null && Greenfoot.isKeyDown("e")) spitOut();
}
Assign enemy (from your line 10 above) to swallowed in the swallow method and re-assign null to swallowed in both the copyAbility and spitOut methods.
mshep mshep

2020/5/24

#
Makes sense. Thanks a lot!
You need to login to post a reply.