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

2015/4/5

How to cycle through a String of images ! Help ASAP!

Hades Hades

2015/4/5

#
So I'm trying to cycle through a string of images to make an object look like its rotating constantly, but I can't seem to get it working. This is my current code:
 private String[] img3 = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"};

...........

  public void switchImage()
   {
     if (touchingBody && !isDown)
     {
         
       
         setImage("Block2.fw.png");
         isDown = true;
         
         play();
     }
     if (!touchingBody && isDown)
     {
         isDown = false;
         for (int i = 0; i < 30; i++)
         {
             setImage(img3[i]+".png");
             
             
         }
     }
     
I am keeping the Block2 image file the same for now but i want to make a string for it as well as soon as i get the first image working. Also I have question on how to access certain string values. For example, if an Object1 Intersects an object2 during the time a certain image is used in Object2, how do you access the same string value of image assuming Object 2 had a color change during the intersection and contains 2 string sets: one for normal color and one for change in color.
danpost danpost

2015/4/6

#
You cannot use a 'for' loop to run an animation like that. Only the last image set will be visible. You need to add a delay counter to track act cycles and change the image as time passes (act cycles pass).
Hades Hades

2015/4/6

#
I know I have to use Greenfoot.delay(); but I am not sure how i can call on all the Objects that are of the Border Actor and make them all rotate simaltaneously without disrupting other methods. From what I understand the "for" loop excutes the image rotation command for each object then moves on to the next as it is currently doing for me as well as preventing me from executing any other methods. So im lost at this point
danpost danpost

2015/4/6

#
Hades wrote...
I know I have to use Greenfoot.delay();
That is precisely what you cannot do. Using that will disrupt the smooth actions of your other actors as 'delay' will basically stop execution for the allotted amount of time (stuck in the method at that point). You need to add an instance int field to count act cycles and use it to determine when to have the image change:
private int timer;
 
public void switchImage()
{
    if (touchingBody != isDown) // detect state change
    {
        isDown = !isDown; // change state
        timer = -1; // reset timer
        if (isDown) play(); // play if down
    }
    // determine number of images in array that is to be used
    int imgCount = img3.length;
    if (isDown) imgCount = border3.length; // whatever name you actually give it
    // run timer
    timer = (timer+1)%(20*imgCount);
    if (timer%20 == 0) // time to change image
    {
        // set appropriate image
        if (isDown) setImage(border3[timer/20]); 
        else setImage(img3[timer/20]);
    }
}
Hades Hades

2015/4/6

#
Oh, thanks for the code but I got it working a different way after spending some time on it...unfortunately my computer just crashed and my project got wiped so this is going to be fun spending another 16 hours doing this. I appreciate all the help though.
You need to login to post a reply.