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

2022/12/27

Greenfoot.mousePressed() condition to exit while loop behaving unexpectedly

PaperJaxon PaperJaxon

2022/12/27

#
I'm attempting to make a simple roulette sprite (click the actor and it cycles through sprites, until clicked again) but im stuck on even beginning the loop. I can't tell why this isn't working, but from debugging it seems like the while loop is breaking immediately. My best guess is that mousePressed is still returning true at that point? but i dont see why it would be. id appreciate any help
public void act()
    {
        if (Greenfoot.mousePressed(this)) {
            RollNewNumber();
        }
    }
    
    public void RollNewNumber() {
        rolling = true;
        while (rolling) {
            if (Greenfoot.mousePressed(this)) {
                rolling = false;
            }
            //do some cycling code
        }
    }
danpost danpost

2022/12/27

#
PaperJaxon wrote...
I'm attempting to make a simple roulette sprite (click the actor and it cycles through sprites, until clicked again) but im stuck on even beginning the loop. I can't tell why this isn't working, but from debugging it seems like the while loop is breaking immediately. My best guess is that mousePressed is still returning true at that point? but i dont see why it would be
Greenfoot grabs keyboard and mouse information between act steps (once between all actors and the world acting). By using a while loop, you are holding off on having act cycles occur. That is, you are stuck in the act of this rolling actor. So, updating of mouse info does not occur. Also, you said it yourself:
until clicked again
This should suggest to you that mousePressed is the wrong method to be using and that mouseClicked would be what you really want. Try something like this:
if (Greenfoot.mouseClicked(this)) rolling = ! rolling;
if (rolling)
{
    // do some cycling code
}
You may want to add a timer to time the changing of images. You would not want to images to change so fast as to be a blur to the users. Also, have all images loaded and ready to use when creating this actor so that they are not continuously being loaded from the file directory.
PaperJaxon PaperJaxon

2022/12/27

#
oh ok that makes sense! thanks for the reply. ill look into that!
You need to login to post a reply.