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

2014/9/13

Pressing three arrow keys, third arrow key doesn't do anything

TurboLover004 TurboLover004

2014/9/13

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void checkKeys()
{
    if (onGround() == true) {
        if (Greenfoot.isKeyDown("up")) {
            isJumping = true;
        }
    }
    if (Greenfoot.isKeyDown("right")) {
        runningRight = true;
    } else {
        runningRight = false;
    }
    if (Greenfoot.isKeyDown("left")) {
        runningLeft = true;
    } else {
        runningLeft = false;
    }
}
1
2
3
4
5
6
7
8
9
10
11
public void run()
{
    if (runningRight != runningLeft) {
        if (runningRight == true) {
            xMovement = speed;
        } else if (runningLeft == true) {
            xMovement = -speed;
        }
        setLocation(getX() + xMovement, getY());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
public void jump()
{
    if (isJumping == true) {
        jumpTimer++;
        if (jumpTimer < 10) {
            addForce(jump);
        } else {
            isJumping = false;
        }
        move();
    }
}
Holding the left arrow key and the right arrow key stops the character's movement along the x-axis, which is what I want, but if I press the up arrow key while doing this I cannot initiate a jump. Holding the left and up arrow keys makes the character jump to the left, but if I press the right arrow key while doing this, it continues moving left and jumping - vice versa for holding the right and up arrow keys and continuing to move right when the left arrow key is pressed. Any ideas? The vertical movement uses SmoothMover 2.1 and Vector 2.0.
danpost danpost

2014/9/13

#
This may be a restriction imposed by your keyboard. Create a new scenario with the following World class and check for what combination of keys you can detect being down at once:
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
import greenfoot.*;
 
public class KeyWorld extends World
{
    private static final String keylist = "abcdefghijklmnopqrstuvwxyz";
    private static final String[] keyArray = { "escape", "shift", "control", "backspace", "space", "up", "down", "right", "left", "enter" };
     
    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++;
        for (int i=0; i<keyArray.length; i++) if (Greenfoot.isKeyDown(keyArray[i])) count++;
        if (count != lastCount)
        {
            lastCount = count;
            actor.setImage(new GreenfootImage(""+count, 30, null, null));
        }
    }
}
BTW, my keyboard detects the pressing of the 'up' key when both 'left' and 'right' are pressed.
TurboLover004 TurboLover004

2014/9/13

#
Can only press two arrow keys at once; I guess I'll use WASD from now on. Thanks for clearing that up, Danpost.
You need to login to post a reply.