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

2016/4/22

Animation method

orangutan orangutan

2016/4/22

#
Hi, I have a problem here. And it is a frustrating one. I try to make a method called animate(), which takes two arguments. With the arguments, the animate method would look like this: animate(GreenfootImage array, int someNumber). I have found a method to animate an array from Greenfoot blog, thanks to danpost. danpost method, the animation() works. But when I try to modify so that it would take an arguments it does not work. Please help my animate() method thanks
import greenfoot.*; 

public class Kanye extends Actor
{
    GreenfootImage[] KanyeIdle = new GreenfootImage[10];
    int numOfImgs_KanyeIdle;
    
    //constructor to initialize Kanye
    public Kanye(GreenfootImage[] init) {
        setImage( KanyeIdle[0] );
    }
    
    public Kanye() {
        for ( int i = 0; i < KanyeIdle.length; i++ ) {
            KanyeIdle[i] = new GreenfootImage("KanyeIdle" + i);
        }
    }
    
    /**
     * Act - do whatever the Kanye wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        animate(KanyeIdle, numOfImgs_KanyeIdle);
    }
    
    public void animate(GreenfootImage[] spriteImgs, int numOfImgs) {
        numOfImgs = (numOfImgs + 1) % spriteImgs.length;
        setImage(spriteImgs[numOfImgs]);
    }
    
    public void animation() {
        numOfImgs_KanyeIdle = (numOfImgs_KanyeIdle + 1) % KanyeIdle.length;
        setImage(KanyeIdle[numOfImgs_KanyeIdle]);
    }
    
}
orangutan orangutan

2016/4/22

#
When I have animation(), Kanye gets animated
orangutan orangutan

2016/4/22

#
public void act() {
    animation(); //this method works
    animate( arraysName, arraysLength ); //this method doesnt work
}
danpost danpost

2016/4/22

#
The problem is that when using the 'animate' method, you do not have the value of 'numOfImgs_KanyeIdle' being incremented through the range of index values of the array of images. In fact, its value is constant (no change at all is being made to its value). So, 'numOfImgs' will always be initially the constant value of 'numOfImgs_KanyeIdle' and always be iterated to the same index value.
orangutan orangutan

2016/4/22

#
Thanks for this, thanks a lot. And so... is there a way to make an animation method that can animate any GreenfootImage arrays?
danpost danpost

2016/4/23

#
orangutan wrote...
... is there a way to make an animation method that can animate any GreenfootImage arrays?
My Animation Support Class scenario has an Animation class that you can exchange one set of images for another to be used for the animation. It can be imported into any project and used, as is, to help create animation for your objects (Actor and World alike). I believe the 'setAnimation' method is what you are referring to.
valdes valdes

2016/4/23

#
If you say that animation() works, then I think the error is this: In the code you show,
numOfImgs_KanyeIdle
is not initialized. In the constructor
Kanye()
add
numOfImgs_KanyeIdle = KanyeIdle.length 
or change the call to
animate(KanyeIdle, KanyeIdle.length);
and delete the variable numOfImgs_KanyeIdle
You need to login to post a reply.