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

2016/7/14

looping

olliequirk olliequirk

2016/7/14

#
I want to repeat the code that is in the do while loop until I stop holding down the down key, however, I am unsure how to do this, some help would be great, thanks.
if(Greenfoot.isKeyDown("down"))
        {
            do
            {

                setLocation(getX(), getY()+1);

                setImage(image1);
                int span = 10;
                int imgCt = 4;

                imageTimer++;
                if (imageTimer == span) 
                {
                    setImage(image2);
                }
                if (imageTimer == 2*span) 
                {
                    setImage(image3);
                }
                if (imageTimer == 3*span) 
                {
                    setImage(image2);
                }
                if (imageTimer == 2*span) 
                {
                    setImage(image3);
                }
                if (imageTimer == 3*span) 
                {
                    setImage(image2);
                }

            }while (Greenfoot.isKeyDown("down"));
        }
danpost danpost

2016/7/14

#
For what you are trying to do as far as looping, you could, instead of a 'do-while' loop, just use a 'while' loop:
while(Greenfoot.isKeyDown("down"))
{
    // code to repeat while 'down' key is held down
}
However, you are missing a basic concept here. Any loop you create using 'for', 'while', or 'do', will be executed to its completion during each act cycle. As a result, the scenario will freeze until you release the "down" key and whatever image is finally set during the loop will be what is displayed on the next frame (you will not see the intermediate changes while the loop is executing). The loop you are trying to create is already created by greenfoot, which calls the act method on all active objects each frame. Therefore, the code placed in your act method should be what you want the object to do each instance (each frame), not throughout the running of the scenario. Therefore, you will be using 'if' instead of any type of loop.
Super_Hippo Super_Hippo

2016/7/14

#
I don't see an error there. What I notice is that lines 17 through 24 are the same as 25 through 32. If I remember correctly, the actors are only repainted in the world between the act-cycles (or if the method was called "manually"). So you can't see the character moving down when you hold down the down-key. When you release the key, the actor will be somewhere below the last position, but changing the image while falling in the do-while-loop doesn't really make sense because you can't see it. Why do you want to use a do-while-loop? Maybe it would work like you wish if you just use line 1 as the condition and remove 'do' and 'while'. Edit: I should refresh the page before posting, so I could see that someone was faster. :)
You need to login to post a reply.