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

2012/5/13

Greenfoot, Animation

Matt Matt

2012/5/13

#
public class LampPost  extends Actor
{

     int currImage; //0 will represent LampPost off
        
        public LampPost()
        {
            currImage = 0;
            setImage("LampPost.png");
            setRotation(90); //set rotation to north
        }

        public void act() 
    {
        changeImage();
    }    
    
    private void changeImage()
    {
        if(currImage==0)
        {
            setImage("LampPost.png");
            currImage = 1;
            
        }
        else if(currImage==1)
        {
            setImage("LampPostLIT.png");
            currImage = 0; 
        }
    }
   
}
I have written coding for images to change, so they are "Animating", but I essentially want them to change after about a 5-10 second delay. I have tried using 'Gennfoot.delay(15)' but that obviously effects the entire game speed
SPower SPower

2012/5/13

#
Create an instance variable:
public class LampPost  extends Actor  
{  
  
     int currImage; //0 will represent LampPost off
     private int imageDelay = 0;
Change changeImage() to this:
    private void changeImage()
    {
        //add 1 to our delay variable:
        imageDelay++;
        //if our delay isn't big enough, we don't want to do anything anymore:
        if (imageDelay < 100) return;

        //do the stuff you know:
        if(currImage==0)
        {
            setImage("LampPost.png");
            currImage = 1;
        }
        else if(currImage==1)
        {
            setImage("LampPostLIT.png");
            currImage = 0;
        }
    }
The 100 in 'if (imageDelay < 100)' is just something I guessed: you have to test which number you like and use that. There are more complex ways, but this is quite simple.
Matt Matt

2012/5/13

#
Thankyou for your help :) If i wanted for their to be a delay every single time the image changes, how would I go about that
SPower SPower

2012/5/13

#
Forgot to tell: set imageDelay to 0 at the end of your method
SPower SPower

2012/5/13

#
With the method, I mean the changeImage method :)
Matt Matt

2012/5/13

#
THANKS! everything works exactly how I wanted it to
You need to login to post a reply.