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

2017/12/6

Can I have help flipping my Immage

Bandito Bandito

2017/12/6

#
import greenfoot.*;

/**
 * This is the human, the last survivor on earth
 * 
 * @author Eric Faltermeier 
 * @version 12/6/2017
 */
public class Human extends Actor
{
    private boolean isDown;
    private GreenfootImage rightImage;
    public Human()
    // This makes the human more to scale with the rest of the world.
    {
        //myImage = new GreenfootImage("Human.gif");
        GreenfootImage myImage = getImage();
        int myNewWidth = (int)myImage.getWidth()/8;
        int myNewHeight = (int)myImage.getHeight()/8;
        myImage.scale(myNewWidth,myNewHeight);
        rightImage = myImage;

    }

    public void act() 
    {
        move();
    }

    public void move()
    // This is where the character is controlled it makes it so the character face the right way
    {
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() +2);
        }

        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() -2);
        }

        if (Greenfoot.isKeyDown("a"))
        {
            move(-2);
            if(Greenfoot.isKeyDown("a") && !isDown)
            {
                getImage().mirrorHorizontally();
                isDown = true;
            }
            if(!Greenfoot.isKeyDown("a") && isDown)
            {
                isDown = false;
            }
        }

        if (Greenfoot.isKeyDown("d"))
        {
            setImage("Human2.gif");
            GreenfootImage myImage = getImage();
            int myNewWidth = (int)myImage.getWidth()/8;
            int myNewHeight = (int)myImage.getHeight()/8;
            myImage.scale(myNewWidth,myNewHeight);
            move(2);            
        }
    }
}

Bandito Bandito

2017/12/6

#
It flips to the left when I press "a" like it is suposed to then it fips back with d but it doesnt flip back again with a i need help.
danpost danpost

2017/12/6

#
Instead of mirroring the image repeatedly, keep a reference to images facing both ways and set them to the actor when needed.
Bandito Bandito

4 days ago

#
So would it be something more like this
 private GreenfootImage rightImage;
    private GreenfootImage leftImage;
    public Human()
    // This just difines what rightImage and leftImage are so they can be used later in the code
    {
        rightImage = new GreenfootImage ("HumanRight.gif");
        leftImage = new GreenfootImage ("HumanLeft.gif");
    }

    public void act() 
    {
        move();
    }

    public void move()
    // This is where the character is controlled it makes it so the character face the right way
    {
        if (Greenfoot.isKeyDown("s"))
        {
            setLocation(getX(), getY() +2);
        }

        if (Greenfoot.isKeyDown("w"))
        {
            setLocation(getX(), getY() -2);
        }

        if (Greenfoot.isKeyDown("a"))
        {
            setImage("leftImage");
            move(-2);
        }

        if (Greenfoot.isKeyDown("d"))
        {
            setImage("rightImage");           
            move(2);            
        }
    }
(Sorry it took me so long to reply)
danpost danpost

4 days ago

#
Remove the quotes in lines 30 and 36.
Bandito Bandito

4 days ago

#
That worked. Thanks for your help!
You need to login to post a reply.