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

2015/2/28

Need help with slowing down animation

Cathy3210 Cathy3210

2015/2/28

#
Hello, I have only recently begun to learn about coding and was wondering if someone could help me with something. I would like to be able to slow down my Mario animation. I believe that I am supposed to use an animation counter..? If someone could help me that would be greatly appreciated! :)
import greenfoot.*;

/**
 * Write a description of class Mario here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Mario extends Actor
{
    private GreenfootImage seq0;
    private GreenfootImage seq1;
    private GreenfootImage seq2;
    
    private int speed_;
    public Mario()
    {
        super();
        speed_ = 5;
        
        seq0 = new GreenfootImage("mario1.gif");
        seq1 = new GreenfootImage("mario2.gif");
        seq2 = new GreenfootImage("mario3.gif");
        
        setImage(seq0);
    }
    
    public void setSpeed(int s)
    {
        speed_ = s;
    }
    
    public int getSpeed()
    {
        return speed_;
    }
    
    /**
     * 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() 
    {
        // Add your action code here.
        animate();
        swim();
    } 
    
    public void animate()
    {
        if(getImage() == seq0 )
        {
            setImage(seq1);
        }
        else if(getImage() == seq1)
        {
            setImage(seq2);
        }
        else
        {
            setImage(seq0);
        }
    }
    
    public void swim()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - speed_ , getY());
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + speed_ , getY());
        }
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX() , getY() - speed_);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX() , getY() + speed_);
        }
        if(Greenfoot.isKeyDown("p"))
        {
            Greenfoot.stop();
        }
    }
    
}
danpost danpost

2015/2/28

#
An animation counter is just an int field that is used to count act cycles (incremented every act cycle). You will need another int value, which may be made another field or just hard-coded, for the number of act cycles between each image change. The product of the two values will be the number of act cycles that will occur before the same image is again set (one complete cycle of images). So, if you had the fields in the first few lines below, a change in the image will occur when the condition on the last is met:
private int imageCount = 3; // number of images in the animation
private int actsPerImage = 10; // act cycles per image
private int animationActCounter = 0; // act counter for the animation

animationActCounter = (animationActCounter + 1) % (imageCount * actsPerImage); // act counter that cycles for each complete cycle of images
if (animationActCounter % actsPerImage == 0) // if time to change image
The following value can be used to determine which image to set (the value will be between 0 and one less than the number of images, inclusive):
int imageNumber = animationActCounter / actsPerImage;
Cathy3210 Cathy3210

2015/2/28

#
Thank you very much! :)
You need to login to post a reply.