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

2020/10/29

How to memorize the last key pressed?

AnJoMorto AnJoMorto

2020/10/29

#
I'm making a simple game with GIF animations. I would like to have the character look left when the last key pressed was the predefined left key. I just need the programme to memorize the last key and if it equals l it's the standingL animation that should play: Here's what I've come with:

String l    = "a";     //key to move left
String r    = "d";     //key to move right

String lastKey = Greenfoot.getKey();        

if(onGround() == true 
            && !Greenfoot.isKeyDown(r) 
            && !Greenfoot.isKeyDown(l))
        {
            
            if(lastKey != null){
                
                if(lastKey.equalsIgnoreCase(l))
                {
                    
                    setImage(standingL.getCurrentImage());
                    
                } 
                
                if(lastKey.equalsIgnoreCase(r)) 
                {
                    
                    setImage(standing.getCurrentImage());
                    
                }
                
            } else{
            
                setImage(standing.getCurrentImage());
            
            }
            
        }

This code doesn't work, can anyone help me please?
danpost danpost

2020/10/29

#
AnJoMorto wrote...
I'm making a simple game with GIF animations. I would like to have the character look left when the last key pressed was the predefined left key. I just need the programme to memorize the last key and if it equals l it's the standingL animation that should play: Here's what I've come with: << Code Omitted >>
Best would be to keep gifs in fields and have a specific field for the active gif. Something along these lines:
private GifImage standing = new GifImage("standing.gif");
private GifImage standingL = new GifImage("standingL.gif");
private GifImage activeGif = standing;

public void act()
{
    // ...
    if (onGround() == true)
    {
        int dir = 0
        if (Greenfoot.isKeyDown("r")) dir++;
        if (Greenfoot.isKeyDown("l")) dir--;
        if (dir == 1 && !activeGif == standing) activeGif = standing;
        if (dir == -1 && !activeGif == standingL) activeGif = standingL;
    }
    // ...
    setImage(activeGif.getCurrentImage());
}
AnJoMorto AnJoMorto

2020/10/29

#
danpost wrote...
Best would be to keep gifs in fields and have a specific field for the active gif. Something along these lines: << Code Omitted >>
You made some mistakes it seems, but I corrected it and it works perfectly! Thank you so much @danpost . You've been the person behind my whole project since you answer all possible questions I find in this platform. Thank you again. And here's the corrected code for those who need it:
        if (onGround() == true)
        {
            int dir = 0;
            if (Greenfoot.isKeyDown(r)) dir++;
            if (Greenfoot.isKeyDown(l)) dir--;
            if (dir == 1 && activeGif != standing) activeGif = standing;
            if (dir == -1 && activeGif != standingL) activeGif = standingL;
        }
You need to login to post a reply.