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

2015/11/16

isKeyDown() not working on the website

Paulsmlim Paulsmlim

2015/11/16

#
I created a scenario which uses the isKeyDown() function multiple times, but it seems like they're not executing fast enough to finish the full animation. Scenario This is my scenario, and the problem occurs whenever I hold Q, W, or R. The arrow keys that make spiderman move work properly but whenever I press Q it seems to not do full animation, which is supposed to be spiderman creating a web shield. I am using the checkKeys() method in the act() method to check the keys that the user presses.
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public void act()
    {
        checkKeys();
        checkFall();
        nextLevel();
        pickUpUSB();
        checkGameWin();
        checkDie();
    }
 
    public void checkKeys()
    {
        //This set of if statements makes sure that Paul can't move while using any skills
        //Makes Paul use his shield whenever the "q" key is pressed
        if(Greenfoot.isKeyDown("q")){
            if(!inAir){
                shield();
            }
        }
        else{
            isShielding = false;
            if(Greenfoot.isKeyDown("right")){
                moveRight();
            }
            //Makes Paul move in the left direction whenever the "left" key is pressed
            else if(Greenfoot.isKeyDown("left")){
                moveLeft();
            }
            else{
                if(isWalking){
                    isWalking = false;
                }
                //Makes Paul shoot his web bolt whenever the "w" key is pressed
                if(Greenfoot.isKeyDown("w")){
                    if(!inAir){
                        shootWeb();
                    }
                }
                //Makes Paul attacks whenever the "e" key is pressed
                else if(Greenfoot.isKeyDown("e")){
                    if(!inAir){
                        attack();
                    }
                }
                //If none of the keys are pressed, Paul remains standing idle
                else{
                    standing();
                }
            }
        }
        //Makes Paul jump whenever the "space" key is pressed
        if(Greenfoot.isKeyDown("space")){
            if(!inAir){
                jump();
            }
        }
        //Makes Paul go down a platform whenever the "down" key is pressed
        if(Greenfoot.isKeyDown("down")){
            downPlatform();
        }
    }
danpost danpost

2015/11/16

#
The following was posted in another discussion on the same issue:
davmac wrote...
It's your keyboard. Put simply, keyboards can only handle a limited number of keys pressed at the same time. After a certain point, they won't register new keypresses. Certain key combinations will work, and others won't. Better keyboards can handle a larger number.
Paulsmlim Paulsmlim

2015/11/17

#
I am using the keyboard of a Macbook pro, so most other people would have the same issue? Is there any way to fix this issue through alternative ways of coding without getting a better keyboard?
danpost danpost

2015/11/17

#
Paulsmlim wrote...
I am using the keyboard of a Macbook pro, so most other people would have the same issue? Is there any way to fix this issue through alternative ways of coding without getting a better keyboard?
Try different key combinations.
Paulsmlim Paulsmlim

2015/11/17

#
Sorry I don't understand what you mean by different key combinations. Do you mean like try making the "z" key as shield instead of the "q" key?
Paulsmlim Paulsmlim

2015/11/17

#
One other thing I noticed however is that if I hold the command button while I press Q, then it works properly but the spiderman still uses shield even when I let go of the command button and the Q button. Could the issue be related to this?
danpost danpost

2015/11/17

#
Paulsmlim wrote...
Sorry I don't understand what you mean by different key combinations. Do you mean like try making the "z" key as shield instead of the "q" key?
Yes; that is what I mean.
Paulsmlim wrote...
One other thing I noticed however is that if I hold the command button while I press Q, then it works properly but the spiderman still uses shield even when I let go of the command button and the Q button. Could the issue be related to this?
I think that is a programming issue. You have code to begin the shield (which, by the way, is continuously being done while the 'q' key is down), but none to remove it.
Paulsmlim Paulsmlim

2015/11/17

#
Sorry I did not provide this code before, but it does remove the shielding once the "q" key is not down. The code for shielding() is
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
public void shield()
    {
        //If Paul just activated his shield, then imageNum resets.
        if(!isShielding){
            imageNum = 1;
        }
        //Paul is shielding from attacks so he will take reduced damage from enemies
        isShielding = true;
        shooting = false;
        attacking = false;
        //Increments the amount of times this method was called
        time++;
        //For every 12 times I call this method, Paul changes image
        if(time >= 12){
            time = 0;
            //Sets which direction Paul is facing while using shield
            GreenfootImage img = new GreenfootImage("PaulShielding-" + imageNum + ".png");
            if(facingRight){
                setImage(img);
            }
            else{
                img.mirrorHorizontally();
                setImage(img);
            }
            //Changes the number of the image if the animation is still needed. If not, then keeps image number the same.
            if(imageNum < 4){
                imageNum++;
            }
            if(imageNum == 3){
                Greenfoot.playSound("Shield.wav");
            }
        }
    }
When shielding, the spiderman's image is set as a specific image which shows himself as if he is shielding. However, If the "q" key is not pressed, as you can see in the code I provided before, the standing() function will execute and spiderman will remain standing and its image will also be set as an image which shows him as standing. The code for standing() is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void standing()
    {
        imageNum = 1;
        shooting = false;
        attacking = false;
        GreenfootImage img = new GreenfootImage("PaulStanding.png");
        if(facingRight){
            setImage(img);
        }
        else{
            //Flips horizontally the image of Paul standing in the right direction
            img.mirrorHorizontally();
            setImage(img);
        }
    }
You need to login to post a reply.