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

2015/12/13

How to tell what key was pressed?

SonofArith SonofArith

2015/12/13

#
Hi everyone! So I am creating a game which involves a player moving up, down, left, and right, and the game is in eagle eye view so all you see is the player's head. I gave this player an animation in Greenfoot, which the code for looks like this:
if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("d")) //These are the images for my player's animation.
        {
            image++;
            if(image == 0)
            {
                setImage("Player4.png");
            }
            if(image == 10)
            {
                setImage("Player5.png");
            }
            if(image == 20)
            {
                setImage("Player6.png");
            }
            if(image == 30)
            {
                setImage("Player5.png");
            }
            if(image == 40)
            {
                setImage("Player4.png");
            }
            if(image == 50)
            {
                setImage("Player3.png");
            }
            if(image == 60)
            {
                setImage("Player2.png");
            }
            if(image == 70)
            {
                setImage("Player3.png");
            }
            if(image == 80)
            {
                setImage("Player4.png");
                image=0;
            }
        }
However, this animation only happens when the player is moving, so when he is stationary, I had to get it to set the player back to the original picture. This is how I did that:

if(!Greenfoot.isKeyDown("w") && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("d")) //Sets image to normal standing image.
        {
            setImage("Player.png");
        }
Then I have my problem. The picture I use has the player facing a certain direction, toward the bottom of the screen. This means that if you are moving left, and facing left when you stop moving, then it will make it look like you turned back toward the bottom of the screen. Basically what I am trying to do is make the image set to the standing image while facing the direction in which you stopped, instead of facing downward. If need be I can post the rest of my code for the player. Thank you very much for any help, and I am sorry if my code is junk, just starting out with Greenfoot :)
danpost danpost

2015/12/13

#
You may have to add a boolean field to track the moving state of the actor:
private boolean moving;
Then insert at line 3 in the first code set:
if ( ! moving )
{
    moving = true;
    turn(-90);
}
and replace line 3 in the second code set with:
if ( moving )
{
    moving = false;
    turn(90);
    setImage("Player.png");
}
SonofArith SonofArith

2015/12/13

#
danpost wrote...
You may have to add a boolean field to track the moving state of the actor:
private boolean moving;
Then insert at line 3 in the first code set:
if ( ! moving )
{
    moving = true;
    turn(-90);
}
and replace line 3 in the second code set with:
if ( moving )
{
    moving = false;
    turn(90);
    setImage("Player.png");
}
Hi Danpost, thank you very much for the response. I am afraid that did not work :/ Just to be sure, I wanted to post my movement method for you:
private boolean moving;

    public void movement()
    {     
        int speed = 4;
        if(Greenfoot.isKeyDown("w")) //|| Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move(speed);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            setRotation(180);
            move(speed);            
        }
        if(Greenfoot.isKeyDown("s"))
        {
            setRotation(90);
            move(speed);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            setRotation(0);
            move(speed);
        }        

        if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("d")) //These are the images for my player's animation.
        {            
            image++;

            if ( ! moving )
            {
                moving = true;
                turn(-90);
            }

            if(image == 0)
            {
                setImage("Player4.png");
            }
            if(image == 10)
            {
                setImage("Player5.png");
            }
            if(image == 20)
            {
                setImage("Player6.png");
            }
            if(image == 30)
            {
                setImage("Player5.png");
            }
            if(image == 40)
            {
                setImage("Player4.png");
            }
            if(image == 50)
            {
                setImage("Player3.png");
            }
            if(image == 60)
            {
                setImage("Player2.png");
            }
            if(image == 70)
            {
                setImage("Player3.png");
            }
            if(image == 80)
            {
                setImage("Player4.png");
                image=0;
            }
        }

        if(!Greenfoot.isKeyDown("w") && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("s") && !Greenfoot.isKeyDown("d")) //Sets image to normal standing image.
        {
            if ( moving )
            {
                moving = false;
                turn(90);
                setImage("Player.png");
            }
        }

        if(Greenfoot.isKeyDown("a") && (Greenfoot.isKeyDown("d"))) //If both a and d are pressed at the same time, this makes it so that the player won't be walking in place.
        {
            setImage("Player.png");
        }
        if(Greenfoot.isKeyDown("w") && (Greenfoot.isKeyDown("s"))) //If both s and w are pressed at the same time, this makes it so that the player won't be walking in place.
        {
            setImage("Player.png");
        }
    }    
Also, I have one other method in this class which affects the rotation of the player, so I will post that here as well:
public void collision()
    {
        if(isTouching(Walls.class))
        {
            if(Greenfoot.isKeyDown("w"))
            {
                setRotation(90);
                move(4);
            }
            if(Greenfoot.isKeyDown("a"))
            {
                setRotation(0);
                move(4);
            }
            if(Greenfoot.isKeyDown("s"))
            {
                setRotation(270);
                move(4);
            }
            if(Greenfoot.isKeyDown("d"))
            {
                setRotation(180);
                move(4);
            }            
        }
    }
This method is for collision with walls. Again, thank you!
danpost danpost

2015/12/13

#
Okay, instead of the 'moving' field, use a class GreenfootImage field to hold the standing image and create an image from it with the same rotation as the moving images:
private static GreenfootImage standingImage = createStandingImage();;

private static GreenfootImage createStandingImage()
{
    GreenfootImage original = new GreenfootImage("Player.png");
    int n = original.getWidth();
    if (original.getHeight() > original.getWidth()) n = original.getHeight();
    GreenfootImage enlarged = new GreenfootImage(n*2, n*2);
    enlarged.drawImage(original, ((enlarged.getWidth()-original.getWidth())/2, (enlarged.getHeight()-original.getHeight())/2);
    enlarged.rotate(90);
    GreenfootImage rotated = new GreenfootImage(original.getHeight(), original.getWidth());
    rotated.drawImage(enlarged, (rotated.getWidth()-enlarged.getWidth())/2, (rotated.getHeight()-enlarged.getHeight())/2);
    return rotated;
}
Then, replace lines 78 through 83 with:
setImage(standingImage);
and remove lines 31 through 35.
SonofArith SonofArith

2015/12/13

#
danpost wrote...
Okay, instead of the 'moving' field, use a class GreenfootImage field to hold the standing image and create an image from it with the same rotation as the moving images:
private static GreenfootImage standingImage = createStandingImage();;

private static GreenfootImage createStandingImage()
{
    GreenfootImage original = new GreenfootImage("Player.png");
    int n = original.getWidth();
    if (original.getHeight() > original.getWidth()) n = original.getHeight();
    GreenfootImage enlarged = new GreenfootImage(n*2, n*2);
    enlarged.drawImage(original, ((enlarged.getWidth()-original.getWidth())/2, (enlarged.getHeight()-original.getHeight())/2);
    enlarged.rotate(90);
    GreenfootImage rotated = new GreenfootImage(original.getHeight(), original.getWidth());
    rotated.drawImage(enlarged, (rotated.getWidth()-enlarged.getWidth())/2, (rotated.getHeight()-enlarged.getHeight())/2);
    return rotated;
}
Then, replace lines 78 through 83 with:
setImage(standingImage);
and remove lines 31 through 35.
Yes, it worked flawlessly! Thank you very much fo your help Danpost!
You need to login to post a reply.