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

2016/3/22

I thought I had my actor animated, but now it isn't. I'm not sure what to do now.

tn13673 tn13673

2016/3/22

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Survivor1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor1 extends Actor
{
    public int frame = 1;
    GreenfootImage Survivor1 = new GreenfootImage("Survivor-anim1.png");
    GreenfootImage Survivor2 = new GreenfootImage("Survivor-anim2.png");
    public int speed = 5;
    /**
     * Act - do whatever the Survivor1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    if(Greenfoot.isKeyDown ("up"))
        move (speed); 
    if(Greenfoot.isKeyDown ("down"))
        move (-speed); 
    if(Greenfoot.isKeyDown ("right"))
        turn (5); 
    if(Greenfoot.isKeyDown ("left"))
        turn (-5); 
    }    
   
public void animation()
{
    
    if(frame == 1)
    {
        setImage(Survivor1);
        frame = 2;
    }
    
    else if(frame == 2)
    {
        setImage(Survivor1);
        frame = 1;
       
    }
    
    else if(frame == 3)
    {
        setImage(Survivor1);
        frame = 3;
        
    }
}

}
tn13673 tn13673

2016/3/22

#
Do I just have to add the other images as actors to make this work?
Super_Hippo Super_Hippo

2016/3/22

#
The animation-method isn't called from the act-method, so it is never executed. The animation-method would only change 'frame' between 1 and 2, never to 3. Every if-block sets the image to 'Survivor1', so even if the method would execute, the image wouldn't change.
tn13673 tn13673

2016/3/22

#
I got it fixed. The actor animates too fast though and it's all the time, I only want it to animate when I have it moving. How do I slow it down and have it only animate when I move it?
Super_Hippo Super_Hippo

2016/3/22

#
Only call the animation-method when the actor moves. That will be after the 'move(±speed)'. To slow it down, use an int variable as a timer. Increase it and when it reaches a certain value, execute the animation.
You need to login to post a reply.