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

2015/10/31

How to know when a specific key is up ?

Extreame Extreame

2015/10/31

#
Hi, I am working on a little project and have stumbled over a problem. I want to detect when a key is up and change to a different img. The problem I have is that my code works only for the last key pressed .
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
62
63
64
65
66
67
68
69
70
71
72
private void keyMovement(){
         
        int x = getX();
        int y = getY();
         
        if(!keyIsReleased && Greenfoot.isKeyDown("right")){
            x = x+2;
            setLocation(x, getY());
            setImage(image4);
            keyIsReleased = true;
        }
         
        if(keyIsReleased && !Greenfoot.isKeyDown("right")){
            setImage(image1);
            keyIsReleased = false;
        }
         
        if(!keyIsReleased && Greenfoot.isKeyDown("left")){
            x = x-2;
            setLocation(x, getY());
            setImage(image5);
            keyIsReleased = true;
        }
         
        if(keyIsReleased && !Greenfoot.isKeyDown("left")){
            setImage(image1);
            keyIsReleased = false;
        }
         
        if (Greenfoot.isKeyDown("space") && ((Level1) getWorld()).BulletOut()){
            if(((Level1) getWorld()).PowerUp()){
                getWorld().addObject(new ShipBullet(), getX(), getY()-15);
                getWorld().addObject(new ShipBullet(), getX()-10, getY()-10);
                getWorld().addObject(new ShipBullet(), getX()+10, getY()-10);
                ((Level1) getWorld()).BulletSet(0);
                ((Level1) getWorld()).AmmoDepleated();
                Greenfoot.playSound("shoot.wav");
                 
            }else{
                getWorld().addObject(new ShipBullet(), getX(), getY()-15);
                ((Level1) getWorld()).BulletSet(0);
                Greenfoot.playSound("shoot.wav");
            }
        }
         
        if (!keyIsReleased && Greenfoot.isKeyDown("up")){
            if(getY() > 350 ){
            y = y-2;
            setLocation(getX(),y);
            setImage(image2);
            keyIsReleased = true;
            if (timer == 4){
                setImage(image3); 
                timer = 0;
            }else if (timer ==8){
                    setImage(image2);
                    timer++;
                }else
                timer++;
           }
        }
         
        if(keyIsReleased && !Greenfoot.isKeyDown("up")){
            setImage(image1);
            keyIsReleased = false;
        }
         
        if (Greenfoot.isKeyDown("down")){
            y = y+2;
            setLocation(getX(),y);
        }
    }
If anybody knows how to make the left and right key display the image. Right now the last if for up key changes the picture to the original messes everything up. Thank you.
Super_Hippo Super_Hippo

2015/10/31

#
Instead of using 'keyIsReleased' for right, left and up, use one variable for each of them, so 'rightKeyIsReleased', 'leftKeyIsReleased' and 'upKeyIsReleased'. I think the variables are used to see if they are pressed or not, not if they are released, so they maybe should be renamed. There is another problem. If any key was pressed and now it doesn't, you set it to image1. You could put all these codes to the end to set the variables to 'false' again when the keys aren't pressed and only set the image back to 'image1' when all of these variables are false.
Extreame Extreame

2015/11/3

#
Thanks for the suggestion. That was my problem that it went through the last if and changed to image1. I will try what you said, but it sounds like it works. Thank you again. UPDATE : I tried it and it work. Thank you.
You need to login to post a reply.