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

2012/11/18

Key release?

Johnathon Johnathon

2012/11/18

#
Hey, i'm having a bit of trouble getting a "on Key release" effect on one of my actors. I've been trying to do something along these lines:
       if (getKey() == "right" && !Greefoot.isKeyDown("right"))
            {
                //what happens on key release.
            } 
Im trying to make it so... It wont react so long as the key is held down, but it will do what it calls for as soon as the key is released. The problem is... I don't understand how to use the 'getKey' method at all. I'm rather new to java, and too greenfoot so... please give a good explanation on what you do. Thanks in advance!
danpost danpost

2012/11/18

#
Just like 'isKeyDown', 'getKey' is a method in the Greenfoot class, and must be preceded by 'Greenfoot.' One thing to be aware of, however, is that you only have one chance to record what is being returned with 'getKey'.
// if you use
if ("right".equals(Greenfoot.getKey()))
// you will not afterward have a chance to do
if ("left".equals(Greenfoot.getKey()))
// better is to save any possible return
String key = Greenfoot.getKey();
// and only act upon it, if it is not 'null'
if (key != null)
{
    // now do compares
    if ("right".equals(key))
    {
        // act on 'right' key release
    }
    // if checking other keys as well
    if ("left".equals(key))
    // etc.
}
The reason for saving the key returned is because any subsequent call to 'getKey' within the same 'act' for an object will return 'null'. Only the first call will possibly return a non-null value.
Johnathon Johnathon

2012/11/20

#
Ohh, thanks for that. I don't know why I didn't think of that... lol. I don't fully understand what you mean by that though, would it not get the last key used? Er, Just tried it and got pretty much nowhere. I don't understand what to do with this. Basically, what im trying to do is make it so while the left or right key is down it will play my .gif file (I found a code to make greenfoot accept gif files too so that isn't the problem.) This will make it so if you hold left or right, my actor (Teemo) will look as if he walks, not just float around. I then want him to stop playing the .gif if the key is released, and put him back to his standing state. The problem is, the exact opposite happens for me.... The key is down, he stands still, key let up, he starts walking in place. Heres the where I feel the problem is:
    private void keys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("TeemoRunLeft.gif");
            moveLeft();
        }

        if (Greenfoot.isKeyDown("right"))  
            {
            setImage("TeemoRunRight.gif");    
            moveRight();
            }
            
        if (Greenfoot.getKey() == "right" && !Greenfoot.isKeyDown("right"))
            {
                setImage("TeemoRight.png");
            } 
        
        if (Greenfoot.getKey() == "left" && !Greenfoot.isKeyDown("left"))
            {
                setImage("TeemoLeft.png");
            }
            
        if (Greenfoot.isKeyDown("up") )
        {
            if (onGround())
                jump();
        }
    }    
Thanks for the help provided, and thanks for any future help :]
danpost danpost

2012/11/20

#
What class is this actor class a sub-class of? The condition in line 20 will always return 'false' as 'null' will never equals "left". I say this because you are calling 'getKey' in line 15 and any subsequent call to 'getKey' in the method (or act segment) will return 'null'. Change lines 15 through 23 to the following (not saying everything else is corrent in this segment -- just fixing this one issue)
String key = Greenfoot.getKey(); // saves the key returned
if ("right".equals(key))
{
    setImage("TeemoRight.png");
}
if ("left".equals(key))
{
    setImage("TeemoLeft.png");
}
I removed the other condition in lines 15 and 20 because 'getKey' is set at the time that the key is released (so the key is NOT down).
You need to login to post a reply.