I am a new learner in Greenfoot. Can anyone help me with how I can add a counter so that my object changes images every n times the Act method is invoked?
public void act()
{
if (counter == n)
{
setImage(the image you want to change to);
counter = 0;
}
else
{
counter++;
}
}public int counter = 0;
private int counter = 0;
private int counter = 0;
public void act()
{
if (counter == n)
{
setImage(the image you want to change to);
counter = 0;
}
else
{
counter++;
}
}// with instance field
private int counter=0;
// and in act
counter=(counter+1)%4;
if(counter==0)setImage("image1.png");
if(counter==1)setImage("image2.png");
if(counter==2)setImage("image3.png");
if(counter==3)setImage("image4.png");counter=(counter+1)-4*((counter+1)/4)