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

2019/6/7

Help, cant change the Image of a class!

LplayerJohnny LplayerJohnny

2019/6/7

#
Hello there, my problem is, that I want to have a working actor sprite that changes after 3 seconds. Pictures are already drawn and loaded but it just doesnt want to change them.
public class Player extends Actor
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    boolean DashIsReady=true;
    long lastAdded = System.currentTimeMillis();
    long DashTimer = System.currentTimeMillis();
    int Framestage=1;
    int Timer1=0;
    public void act() 
    {

        if (Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(),getY()-5);
            Timer1=Timer1-1;
            if(Timer1<=0)
            {
                if(Framestage==1)
                {
                    setImage("PlayerUpFrame1.png");
                    Framestage=2;
                    Timer1=3;
                }
                if(Framestage==2)
                {
                    setImage("PlayerUpFrame2.png");
                    Framestage=3;
                    Timer1=3;
                }
                if(Framestage==3)
                {
                    setImage("PlayerUpFrame3.png");
                    Framestage=4;
                    Timer1=3;
                }
                if(Framestage==4)
                {
                    setImage("PlayerUpFrame4.png");
                    Framestage=1;
                    Timer1=3;
                }
            }
        }
can someone please tell my what im doing wrong and fix it please?
danpost danpost

2019/6/7

#
From what I can tell, your player's image should be going frantic when the key is down. There is a much better way to handle the animations, however:
public class Player extends Actor
{
    GreenfootImage[][] animations = { null, null, null, null };
    int imgTimer;
    int lastDir;
    
    public Player()
    {
        String[] dirs = { "Right", "Down", "Left", "Up" };
        for (int d=0; d<4; d++)
        {
            GreenfootImage[] imgs = new GreenfootImage[4];
            for (int i=0; i<4; i++) imgs[i] = new GreenfootImage("Player"+dirs[d+1]+"Frame"+i+".png");
            animations[d] = imgs;
        }
    }
    
    public void act()
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("d")) dx++;
        if (Greenfoot.isKeyDown("s")) dy++;
        if (Greenfoot.isKeyDown("a")) dx--;
        if (Greenfoot.isKeyDown("w")) dy--;
        if (dx*dy != 0 || dx+dy == 0) return; // no keys or key conflict
        setLocation(getX()+dx*5, getY()+dy*5); // move
        int frameRate = 30; // adjust as needed; higher value >> slower animation
        imgTimer = (imgTimer+1)%(4*frameRate); 
        int d = 1-dx; if (d == 1) d = 2-dy; // direction/90 degrees
        if (d != lastDir) imgTimer = 0;  // change in direction
        lastDir = d; // save latest direction
        if (imgTimer%frameRate == 0) setImage(animations[d][imgTimer/frameRate]); // set image
    }
}
One timer, one setImage command
LplayerJohnny LplayerJohnny

2019/6/16

#
Thanks
LplayerJohnny LplayerJohnny

2019/6/21

#
How exactly do I use the animation
danpost danpost

2019/6/21

#
LplayerJohnny wrote...
How exactly do I use the animation
I am not sure what you mean. It is being used at line 32.
You need to login to post a reply.