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

2017/6/9

image change on key press

AAA355 AAA355

2017/6/9

#
I want to make my character change his appearance when I move so that he faces where he goes. how do I do this? Player code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class here.
* 
 * @author (your name) 
 * @version (a version number or a date)
*/
public class Player extends characters
{
    /**
     * Act - do whatever the Mario wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
    }public void move()
    {if (Greenfoot.isKeyDown("left")) 
        {
            move(-5);
            if (isTouching(Wall.class))
            {
                move(5);
            }
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            move(5);
            if (isTouching(Wall.class))
            {
                move(-5);
            }
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
            if (isTouching(Wall.class))
            {
                setLocation(getX(), getY()+5);
            }
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
            if (isTouching(Wall.class))
            {
                setLocation(getX(), getY()-5);
            }
        }  
        if ( isTouching(Collectibles.class) )   {
            removeTouching(Collectibles.class);
        }
        if ( isTouching(End.class) )   {
            removeTouching(End.class);
            Greenfoot.setWorld(new Level1());
        }
        if ( isTouching(End2.class) )   {
            removeTouching(End2.class);
            Greenfoot.setWorld(new Level3());
        }
        if ( isTouching(Enemy.class) )   {
            removeTouching(Enemy.class);
            Greenfoot.setWorld(new Level2());
        }
}
}

Super_Hippo Super_Hippo

2017/6/9

#
Try to use the setRotation method and then move(5) for every direction.
You need to login to post a reply.