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

2025/6/5

This is spawned when an enemy is killed. This code lags in HTML5 for 0.5 seconds but not when run locally. Gifs do the exact same thing. Is it possible to fix this online?

Zacy0 Zacy0

2025/6/5

#
import greenfoot.*;

public class EnemyDeathEffect extends Actor
{
    private GreenfootImage[] frames;
    private int frameInterval = 5;
    private int currentFrame = 0;
    private int frameCounter = 0;
    private boolean fadingOut = false;
    private int fadeSteps = 20;
    private int fadeCounter = 0;
    private GreenfootImage originalImage;

    public EnemyDeathEffect()
    {
        frames = new GreenfootImage[] 
        {
            new GreenfootImage("splat1.png"),
            new GreenfootImage("splat2.png"),
            new GreenfootImage("splat3.png"),
            new GreenfootImage("splat4.png")
        };
        setImage(frames[0]);
    }

    public void act()
    {
        if (!fadingOut)
        {
            frameCounter++;
            if (frameCounter >= frameInterval)
            {
                frameCounter = 0;
                currentFrame++;
                if (currentFrame >= frames.length)
                {
                    //start fading out
                    fadingOut = true;
                    currentFrame = frames.length - 1;
                    originalImage = new GreenfootImage(frames[currentFrame]); //clone once
                    setImage(new GreenfootImage(originalImage)); //set a fresh copy
                }
                else
                {
                    setImage(frames[currentFrame]);
                }
            }
        }
        else
        {
            fadeCounter++;
            float alphaRatio = 1.0f - ((float)fadeCounter / fadeSteps);
            if (alphaRatio < 0) 
            {
                alphaRatio = 0;
            }

            GreenfootImage img = new GreenfootImage(originalImage); //reuse original
            img.setTransparency((int)(255 * alphaRatio));
            setImage(img);

            if (fadeCounter >= fadeSteps)
            {
                if (getWorld() != null)
                {
                    getWorld().removeObject(this);
                }
            }
        }
    }
}
danpost danpost

2025/6/6

#
This is spawned when an enemy is killed. This code lags in HTML5 for 0.5 seconds but not when run locally. Gifs do the exact same thing. Is it possible to fix this online? Try having the images pre-loaded instead of loading them when the actor is added into the world.
You need to login to post a reply.