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

2015/11/24

Problems with animation

Widgy Widgy

2015/11/24

#
I am attempting to have multiple animation commands on a single actor all on different keystrokes such as a walk forward animation, A walk back animation, and an attack animation. However I can only get a single one to work at a time when I need all 3 working at once. Any ideas on how to fix this?
public class Johnny extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    public static int score = 50;
    private boolean flag = false;
    private int waitCycle;
    /**
     * Act - do whatever the Johnny wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        animate();
        checkforSol();
    }   
    public Johnny()
    {
        image1 = new GreenfootImage("Johnny3.png");
        image2 = new GreenfootImage("JohnnyWalk.png");
        image3 = new GreenfootImage("Jattack.png");
        image4 = new GreenfootImage("Johnnywalkback.png");
    }
   public void move()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-2,getY());
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+2,getY());
        }
    }
   public void checkforSol()
   {
       Sol theSol = null;
       theSol = (Sol)getOneIntersectingObject(Sol.class);
       if(theSol != null)
       {
           if (flag == false)
           {
               Sol.score--;
               flag = true;
           }    
           else if (Greenfoot.isKeyDown("a"))
           {
               Johnny.score++;
               flag = true;
           }
       }
   }    
   public void animate()
   {
              if (Greenfoot.isKeyDown("left"))
       {
           setImage(image2);     
       }
        else
       {
            setImage(image1);
       }
       if (Greenfoot.isKeyDown("right"))
        {
            setImage(image4);     
        }
        else
        {
            setImage(image1);
        }
       if (Greenfoot.isKeyDown("a"))
       {
            setImage(image3);     
       }
       else
       {
            setImage(image1);
       }
   }    
    }
danpost danpost

2015/11/24

#
The last 'if-else' (lines 74 through 81) do not care what code comes before it; so, the image will always be 'image3' or 'image1' depending on the state of the "a" key. Use 'else if' through to the end, when you can execute the final 'else' (setting the image to 'image1').
You need to login to post a reply.