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

2013/5/11

delay issues

Banginwithflavor Banginwithflavor

2013/5/11

#
Hi! Im making a game. in order to make an animation i have linked together a bunch of pictures with delays in between in order to make it look like its moving. However when i do this i didnt realize it pauses greenfoot as a whole. Is there a way to pause just the picture without making the entire game pause. This is my code for setting the pictures. it needs to finish running this before it does anything else. Any help? TY in advance.
public void act() 
    {
        int page =1;
        while(page!= 18)
        {
            Greenfoot.delay(5);
            setImage("exp"+page+".gif");
            page++;
        }
    }  
Gevater_Tod4711 Gevater_Tod4711

2013/5/11

#
If you want your image to change while you are running you mussn't use loops. You have to change your image every act:
    public void act() {  
            int page =1;  
            if (page!= 18) {
                setImage("exp"+page+".gif");  
                page++;  
            }  
            else {
                page = 1; //this will make your animation start again from the beginning;
            }
        }    
GreenGoo GreenGoo

2013/5/11

#
A much longer way is to have a integer delay which is decreased in the act method. However, this requires a long chain of if statements which is very cumbersome.
Gevater_Tod4711 Gevater_Tod4711

2013/5/11

#
Oh I missed something... You must declare the int page as a global one and not in the act method. Otherwhile the value will always stay one.
danpost danpost

2013/5/11

#
@Gevater_Tod4711, I believe that Banginwithflavor needed a way to slow down the rate of animation which requires a few frames between each changing of the image. @GreenGoo, I do not know why you would think this would require a long chain of 'if' statements. @Banginwithflavor, the adjusted code would be:
// with the following instance fields
private int delayTimer;
private int page;
// the 'act' method
public void act()
{
    delayTimer = (delayTimer+1)%5; // the last number is total frame delay
    if (delayTimer == 0) // if time to change image, do so
    {
        page = (page+1)%18; // the last number is total images in animation
        setImage("exp"+(page+1)+".gif");
    }
}
Banginwithflavor Banginwithflavor

2013/5/11

#
@Gevater_Tod4711, Thanks a ton! it works fine now :D @danpost, thanks for showing me how to slow it down it was going a bit fast!
You need to login to post a reply.