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

2024/4/11

Animate Delay

risebille risebille

2024/4/11

#
i loaded 8 images to make it look moving but it's too fast can you help me add a delay between the images...i tried using Greenfoot.delay() but that makes the whole world and other actors slow down too import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class E_Bringer here. * * @author (your name) * @version (a version number or a date) */ public class E_Bringer extends Actor { GreenfootImage walkRight = new GreenfootImage; GreenfootImage walkLeft = new GreenfootImage; public int animationCount=0; public E_Bringer() { initAnimationSprites(); } /** * Act - do whatever the E_Bringer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { runLeft(); move(-1); checkEdge(); // Add your action code here. } public void initAnimationSprites(){ for(int i = 0; i < 8; i++){ walkLeft = new GreenfootImage("Bringer-of-Death_Walk_"+ (i+1) +".png"); //System.out.println(walkRight); } for(int i = 0; i < 8; i++){ walkRight = new GreenfootImage("Bringer-of-Death_Walk_"+ (i+1) +".png"); walkRight.mirrorHorizontally(); } } public void runRight(){ setImage(walkRight); } public void runLeft(){ setImage(walkLeft); } public void checkEdge(){ World w = getWorld(); if(isAtEdge()){ w.removeObject(this); } } }
danpost danpost

2024/4/12

#
Your code, more completely (without unnecessaries):
import greenfoot.*;

public class E_Bringer extends Actor
{
    GreenfootImage[] walkRight = new GreenfootImage[9];
    GreenfootImage[] walkLeft = new GreenfootImage[9];
    public int animationCount = 0;
    
    public E_Bringer() {
        initAnimationSprites();
    }
    
    public void act() {
        runLeft();
        move(-1);
        checkEdge();
    }
    
    public void initAnimationSprites() {
        for (int i=0; i<8; i++) {
            walkLeft[i] = new GreenfootImage("Bringer-of-Death_Walk_"+(i+1)+".png");
        }
         for (int i=0; i<8; i++) {
            walkRight[i] = new GreenfootImage("Bringer-of-Death_Walk_"+(i+1)+".png");
            walkRight[i].mirrorHorizontally();
        }
    }
    
    public void runRight() {
        setImage(walkRight[animationCount++ %8]);
    }
    
     public void runLeft() {
        setImage(walkLeft[animationCount++ %8]);
    }
    
    public void checkEdge() {
        if (isAtEdge()) {
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2024/4/12

#
In lines 5 and 6, use:
<array name> = new GreenfootImage[8];
You can remove lines 22 and 23 (making only one loop) without changing what it does. Plus, then, you can make line 24 to be:
walkRight[i] = new GreenfootImage(walkLeft[i]);
To slow down the animation, you must allow time between image changes; more specifically, act frames where the image does not change. You can accomplish this by multiplying the number of images by the number of frames for each image. That is the maximum value of the animationCount field. The modulus 8 of the value acquired by dividing the field value by the frames per image value will give the value of the image to show. The field must be zeroed when hitting its maximum value. So, add the following field (insert as new) at line 5:
private static final int ANIM_MAX = 8 * 6; // the 6 can be adjusted
Then, as the last lines in act, insert the following:
if (++animationCount == ANIM_MAX) {
    animationCount = 0;
}
Now, you can use the following when calling runLeft or runRight (one example given here):
if (animationCount%(ANIM_MAX/8) == 0) {
    runLeft();
}
With the setImage line in the called method being:
setImage(walkLeft[animationCount/(ANIM_MAX/8)]);
You need to login to post a reply.