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

2015/10/16

changing characters picture when turning

sp_lit sp_lit

2015/10/16

#
Hello! I'm working on a game with a snowman hunting fires. I'm trying to make the image of the snowman change whenever I turn (press the arrow keys), but I can't get it to work. Would be cool if someone could help me figure out what I'm doing wrong.
import greenfoot.*;

/**
 * A little spider named Mary
 * 
 * @author Jonas Hatschek
 * @version 2015-10-16
 */
public class Snowman extends Mover
{
    private int mIceTimeMax = 50;
    private int mProduceIce = 50;

/**
*declaration of image variables
*/
    private GreenfootImage imageRight = new GreenfootImage("snowman_right.png");
    private GreenfootImage imageLeft = new 
GreenfootImage("snowman_left.png");
    private GreenfootImage imageDown = new 
GreenfootImage("snowman_down.png");
    private GreenfootImage imageUp = new 
GreenfootImage("snowman_up.png");

    /**
     * Constructs a default Snowman
     */
    public Snowman()
    {
        super(2, new String[]{"snowman_right.png"});

    }

    /**
     * Act - do whatever the snowman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkInput();  
        mProduceIce++;
    }

    /**
     * Checks user input and acts accordingly
     */
    private void checkInput()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(359);
            move();     
            animate();
            setImage(imageRight);

        }
        else if(Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            move();
            animate();
            setImage(imageLeft);
        }
        else if(Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move();
            animate();
            setImage(imageUp); //this is where I'm trying to use the setImage-method. Same for all the arrows.
        }
        else if(Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move();
            animate();
            setImage(imageDown);
        }

        if(Greenfoot.isKeyDown("space"))
        {
            if(mProduceIce >= mIceTimeMax)
            {
                getWorld().addObject(new Ice(getRotation()), getX(),getY());
                mProduceIce = 0;
            }
        }
    }

}
danpost danpost

2015/10/16

#
I could be wrong, but I do not think that the Mover class is something you may want to use here. Please post the Mover class code so that the proper reply could be formulated.
You need to login to post a reply.