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

2022/1/26

A really simple question

Avenderl Avenderl

2022/1/26

#
I'm making a game that has the option to click on Info. When I click it, it creates a new world and displays that world. I used the Greenfoot.isKeyDown method to make a back button to the main menu but it doesn't seem to be working. Maybe the answer is really simple but I'm not sure what went wrong. Here's is the code for the new InfoPage world:
public class InfoPage extends World
{
    public InfoPage()
    {    
        super(400, 350, 1); 
        
        if (Greenfoot.isKeyDown("M")){
            Greenfoot.setWorld(new Menu());
        }
    }
}
Here's the code for the info button in the main menu:
public class Info extends Actor
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new InfoPage());
        }
    }
}
I can provide more of the code if needed!
Spock47 Spock47

2022/1/26

#
You have the check for the key press in the constructor of InfoPage, but you need to have it in the act method (of InfoPage). The constructor is only executed once (when the world gets created), but you need to check for the keypress continuously while the info page is shown (therefore, you have to put it into the act method that will be called repeatedly as long as info page is the currently shown world). Live long and prosper, Spock47
Avenderl Avenderl

2022/1/26

#
Awesome! Thanks for helping :)
You need to login to post a reply.