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

2016/1/10

Class AnimatedActor

TomazVDSN TomazVDSN

2016/1/10

#
Hi, Friends Is there a way to control the time between images in this class?
* @author mik
 * @version 1.0
 */
public class AnimatedActor extends Actor
{
    private GreenfootImage[] images;
    private int currentImage = 0;
    
public AnimatedActor(String basename, String suffix, int noOfImages)
    {
        images = new GreenfootImage[noOfImages];
        for(int i=0; i < noOfImages; i++) {
            images[i] = new GreenfootImage(basename + i + suffix);
        }
        setImage(images[currentImage]);
    }
    
    /**
     * Act - do whatever the AnimatedActor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        currentImage = (currentImage + 1) % images.length;
        setImage(images[currentImage]);
    }
}
TomazVDSN TomazVDSN

2016/1/10

#
Guys, no worries I put a <Thread.sleep(1000); //1000 milliseconds is one second.> under the Subclass Actor:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class FlamingoAct1 extends AnimatedActor
{

    public FlamingoAct1()
    {
        super("Fla", ".png", 7);
    }

    public void act() 
    {
        super.act();  // this takes care of the image animation
        try {
            Thread.sleep(1000);                 //1000 milliseconds is one second.
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }     

    }
}    

TomazVDSN TomazVDSN

2016/1/10

#
It seems it is not the best solution, when I try to put a new object into the World, the World Flickers and the system doesn't let me manually put the object easily. Please if anyone have any idea I would appreciate any comment on this regard.
danpost danpost

2016/1/10

#
Where did you find the AnimatedActor class? I would like to see the code as it was originally. Are you showing the entire class except for the 'import' statements? As an alternative you can download my Animation class and use it as I am already familiar with it and help would be so much easier with it. It can be found here.. Download it and import the Animation class into your scenario. Create an Animation object and keep to reference to it within the class of any object you with to animate. Control the object from the class of the animated actor. The images are passed to the Animation object with the object being animated. Once created, the images can be changed as needed. It even has a timer to be used as a regulator in the speed. It is set to the number of act cycles needed to complete one rotation of the images. This is better than using the time to display one image as many in-between speeds are realize this way.\ As a side note: You should never use Thread.sleep (or any Thread operations unless they are threads you create yourself) -- and using the 'delay' method will halt all operations for the waiting time; so, it is not recommended you use it either.
TomazVDSN TomazVDSN

2016/1/10

#
Danpost, I took the AnimatedActor from the old website Tech-ICT three years ago, I downloaded many articles and now I am trying to learn something. I have bits and peaces which I tried to recreate. I don't have the original maybe Michael Kolling was the original creator maybe he still have it in his archives. You can download what I have here. I will download your Animation Class and study it and try to use it. Thank you for pointing out on the Thread usage, I still have a long way to go. Regards, Tomaz
TomazVDSN TomazVDSN

2016/1/10

#
.
You need to login to post a reply.