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

2013/3/2

Trying to animate something

Vincent_Puyat Vincent_Puyat

2013/3/2

#
So I've been tinkering around with Greenfoot. More specifically, I've been trying to grasp the concept of how I can animate stuff by image shifting. Here is a code for one animation I've been trying to do. When I run it, the images don't seem to change an is stuck at the first image only.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lyn here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lyn extends Actor
{
GreenfootImage image0,image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11,image12,image13,image14,image15,image16,image17,image18,image19,image20,image21,image22,image23,image24,image25;
    /**
     * Act - do whatever the Lyn wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Lyn()
    {
        image0=new GreenfootImage("0.png");
        image1=new GreenfootImage("1.png");
        image2=new GreenfootImage("2.png");
        image3=new GreenfootImage("3.png");
        image4=new GreenfootImage("4.png");
        image5=new GreenfootImage("5.png");
        image6=new GreenfootImage("6.png");
        image7=new GreenfootImage("7.png");
        image8=new GreenfootImage("8.png");
        image9=new GreenfootImage("9.png");
        image10=new GreenfootImage("10.png");
        image11=new GreenfootImage("11.png");
        image12=new GreenfootImage("12.png");
        image13=new GreenfootImage("13.png");
        image14=new GreenfootImage("14.png");
        image15=new GreenfootImage("15.png");
        image16=new GreenfootImage("16.png");
        image17=new GreenfootImage("17.png");
        image18=new GreenfootImage("18.png");
        image19=new GreenfootImage("19.png");
        image20=new GreenfootImage("20.png");
        image21=new GreenfootImage("21.png");
        image22=new GreenfootImage("22.png");
        image23=new GreenfootImage("23.png");
        image24=new GreenfootImage("24.png");
        image25=new GreenfootImage("25.png");
        setImage(image0);
    }
 public void act() 
    {
        animation();
    }    
    public void animation()
    {
            if(getImage() == image0)
            setImage(image1);
            if(getImage() == image1)
            setImage(image2);
            if(getImage() == image2)
            setImage(image3);
            if(getImage() == image3)
            setImage(image4);
            if(getImage() == image4)
            setImage(image5);
            if(getImage() == image5)
            setImage(image6);
            if(getImage() == image6)
            setImage(image7);
            if(getImage() == image7)
            setImage(image8);
            if(getImage() == image8)
            setImage(image9);
            if(getImage() == image9)
            setImage(image10);
            if(getImage() == image10)
            setImage(image11);
            if(getImage() == image11)
            setImage(image12);
            if(getImage() == image12)
            setImage(image13);
            if(getImage() == image13)
            setImage(image14);
            if(getImage() == image14)
            setImage(image15);
            if(getImage() == image15)
            setImage(image16);
            if(getImage() == image16)
            setImage(image17);
            if(getImage() == image17)
            setImage(image18);
            if(getImage() == image18)
            setImage(image19);
            if(getImage() == image19)
            setImage(image20);
            if(getImage() == image20)
            setImage(image21);
            if(getImage() == image21)
            setImage(image22);
            if(getImage() == image22)
            setImage(image23);
            if(getImage() == image23)
            setImage(image24);
            if(getImage() == image24)
            setImage(image25);
            if(getImage() == image25)
            setImage(image0);
    }
            
}
Also, I've taken java before in Highschool but the IDE we used was Netbeans so Greenfoot is kind of a haze to me atm.
Vincent_Puyat Vincent_Puyat

2013/3/2

#
Nvm. I just realized I didn't even put else statements after each if statement. No wonder the code after the first if never got executed.
danpost danpost

2013/3/2

#
Using an array of images would simplify the code greatly:
import greenfoot.*; 

public class Lyn extends Actor
{
    GreenfootImage[] images = new GreenfootImage[26];
    int imageNumber;

    public Lyn()
    {
        for( int i=0; i<images.length; i++ ) images[i] = new GreenfootImage( "" + i + ".png" );
        setImage( images[imageNumber] );
    }

    public void act()
    {
        animation();
    }

    public void animation()
    {
        imageNumber = ( imageNumber + 1 ) % images.length;
        setImage( images[imageNumber] );
    }
}
Vincent_Puyat Vincent_Puyat

2013/3/2

#
Thanks a bunch! I was really hoping someone would simplify the code for me because I'm new to arrays so I couldn't get my head around into doing it your way. Will fully go through your code when I reach the library.
Vincent_Puyat Vincent_Puyat

2013/3/2

#
Would it be possible to get an explanation on the working of the animation method in your code?
danpost danpost

2013/3/2

#
The following is a brief run-down of the main statements in the code. Refer to the Java tutorial trail Language Basics for more details on Variables (Arrays) and Operators (Arithmetic). Another trail deals with Numbers and Strings. FIELDS Line 5: Creates an image array with 26 elements Line 6: Initializes a counter field to track the current element (the default value is zero -- implicitly initialized to default) CONSTRUCTOR Line 10: Loads the images into the array Line 11: Sets the initial image of the actor ANIMATION Line 21: Cycles the counter through to the number of images (counts from zero to twenty-five, then repeats) Line 22: Sets the current image of the actor
You need to login to post a reply.