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

2015/5/27

I can't get the images to rotate as they are walking

DietrichaLove DietrichaLove

2015/5/27

#
I tried a for loop in one of them, but the only image it outputs is the one inside the loop and not the one outside of it. I am just trying to make my character have a walking animation. Please help!
import greenfoot.*;

public class Avatar extends Actor
{
       public int i;
    public void act() 
    {
           if(Greenfoot.isKeyDown("left")&&!(Greenfoot.isKeyDown("down"))&&!(Greenfoot.isKeyDown("up"))&&!(Greenfoot.isKeyDown("right"))){
           move(-1);
           setImage("centaur-left-left.gif");
        }
        if(Greenfoot.isKeyDown("right")&&!(Greenfoot.isKeyDown("left"))&&!(Greenfoot.isKeyDown("down"))&&!(Greenfoot.isKeyDown("up"))){
          move(1);
          setImage("Centaur-right-right.gif");
        }
        if(Greenfoot.isKeyDown("up")&&!(Greenfoot.isKeyDown("left"))&&!(Greenfoot.isKeyDown("down"))&&!(Greenfoot.isKeyDown("right"))){
            setLocation(getX(), getY()-1);
            setImage("Centaur-up-right.gif");
        }
        if(Greenfoot.isKeyDown("down")&&!(Greenfoot.isKeyDown("left"))&&!(Greenfoot.isKeyDown("up"))&&!(Greenfoot.isKeyDown("right"))){
            setLocation(getX(), getY()+1);
            setImage("centaur-waling-down-Right.gif");
           
            for(int j=0;j==0;j++){
                setImage("centaur-waling-down-left.gif");
                break;
            }
        }
 
    if(Greenfoot.isKeyDown("left")){
           i=0;
    }else if(Greenfoot.isKeyDown("right")){
           i=1;
    }else if(Greenfoot.isKeyDown("up")){
           i=2;
    }else if(Greenfoot.isKeyDown("down")){
           i=3;
    }else if(!(Greenfoot.isKeyDown("left"))&&!(Greenfoot.isKeyDown("down"))&&!(Greenfoot.isKeyDown("up"))&&!(Greenfoot.isKeyDown("right"))){
           if(i==0){
               setImage("Centaur-left.gif");
           }else if(i==1){
                setImage("Centaur-right.gif");
            }else if(i==2){
                setImage("Centaur-up.gif");
            }else if(i==3){
                setImage("centaur-waling-down.gif");
            }
    }
    
}}
danpost danpost

2015/5/27

#
As written, only one particular image will be set to the actor during any phase in its movement (whether moving left, or right, or up, or down, or standing still). If your ".gif" files contain multiple images each, they need to be extracted from the file and set to the actor individually at set intervals to create the animation. Greenfoot does provide a 'GifImage' class that can be imported into your project that will do most of the dirty work for you. From the menubar, use "Edit>Import Class..." and select the "GifImage" class to import. Once imported, your actors can make use of the class to control the animation of you actor(s). I did a quick look to see if there were any tutorials or documentation on its use; however, I did not find much. I believe that during creation of a GifImage object, the "gif" file given is parsed; then, you can use method calls to get the consecutive images from it as needed.
You need to login to post a reply.