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

2019/3/15

Animation help?

MadameMinuit MadameMinuit

2019/3/15

#
Hello world! I'm trying to animate a sprite with three images making four frames. I want the variable "cycle" to cycle 0, 1, 2, 1, 0, 1... and so on. However, it seems to go 0, 1, 2, 1, 2, 1, 2, 1 and so on, and I can't figure out why for the life of me! I believe I've posted all the relevant code below--apologies that it's a bit messy. I'd greatly appreciate any help I could get. Thank-you!
private void animate()..
    {   
        if  (Greenfoot.isKeyDown("left")){
            setImage (new GreenfootImage("PicklesLeft"+(cycle+1)+".png"));
        
         if (cycle == 0){
             if (delay < 20){
                 delay ++;
             }
             else {
                 step = true;
                 cycle = 1;
                 delay = 0;
                }
        
         }
         else if ((cycle == 1) & (step = true)){
            if (delay < 20){
                 delay ++;
            }
            else {
                delay = 0;
                cycle = 2;
            }
         }
         else if ((cycle == 1) & (step = false)){
            if (delay < 20){
                 delay ++;
            }
            else{
                delay = 0; 
                cycle = 0;
            }
         }
         else if (cycle == 2){
            if (delay < 20){
                 delay ++;
             }
             else{
                 delay = 0;
                 step = false;
                 cycle = 1;
             }
         }
        }

        }
    public void act() 
    {
        move();
        animate();
   
    } 
danpost danpost

2019/3/16

#
I would make things a bit easier by using an array for the animation image set. It is capable of referencing the same image twice, so that would not be an issue. Also, I would use only one int field for running the animation. Let it run to the delay amount times the number of images in the animation image set. If more than one animation set is being used, add an extra array field to reference the current set being used.
danpost danpost

2019/3/17

#
MadameMinuit wrote...
I'm afraid I have never used an array before. I don't even know what an array is.
See here.
MadameMinuit MadameMinuit

2019/3/18

#
Thank you so much! I don't think I did quite what you intended, but this new code does what it's supposed to do.
    private void animate()
    {   
        int[] cycle = {
            2, 3, 2, 1
        };
        if  (Greenfoot.isKeyDown("left")){
            if (delay <= 39){
                delay ++;
            }
            else{
                delay = 0;
            }
            if (delay >= 0 && delay < 10){
                setImage (new GreenfootImage("PicklesLeft"+cycle[0]+".png"));
            }
            else if (delay >= 10 && delay < 20){
                setImage (new GreenfootImage("PicklesLeft"+cycle[1]+".png"));
            }
            else if (delay >= 20 && delay < 30){
                setImage (new GreenfootImage("PicklesLeft"+cycle[2]+".png"));
            }
            else if (delay >= 30 && delay <= 39){
                setImage (new GreenfootImage("PicklesLeft"+cycle[3]+".png"));
            }
        }

        }
You need to login to post a reply.