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

2021/4/19

How to change pictures back to back by clicking?

lonkness lonkness

2021/4/19

#
private void change()
    {
        if (Greenfoot.mouseClicked(this)){ 
            this.setBackground("Story2.png");
            if (Greenfoot.mouseClicked(this)){
                this.setBackground("Story3.png");
                if (Greenfoot.mouseClicked(this)){
                    Greenfoot.setWorld(new AreaFiftyFifty());
                }
            }
        }
    }
I want to tell a little story before my game and would like to do it by clicking on the screen which makes the background change. With the code I have written so far, it obviously skipps to the game immediately. I tried to add a timer, which didnt really work. Can anyone improve my code please?
danpost danpost

2021/4/19

#
You need to have some way to tell how far along you are with the mouse click actions. A simple int counter field should suffice:
private int clickCount;
Then,
if (Greenfoot.mouseClicked(this))
{
    if (clickCount == 2) Greenfoot.setWorld(new AreaFifttyFifty());
    else setImage("Story"+(2+clickCount)+".png");
    clickCount++;
}
You need to login to post a reply.