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

2013/12/2

isKeyDown isn't working

thekidj thekidj

2013/12/2

#
I'm trying to get my game to switch to an instructions screen when I press a key.
public class Castle extends World
{

    /**
     * Constructor for objects of class Castle.
     * 
     */
    public Castle()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 500, 1); 
        setBackground("Castle-stock3197.jpg");
        goToInstructions();
    }
    
    public void goToInstructions()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            Greenfoot.setWorld(new Instructions());
        }
    }
}


to this...

public class Instructions extends World
{

    /**
     * Constructor for objects of class Instructions.
     * 
     */
    public Instructions()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 500, 1); 
        setBackground("Castle.jpg");
    }
}
SPower SPower

2013/12/2

#
the goToInstructions method is only called in the constructor of your world, it should be in your act method:
public void act()
{
    goToInstructions();
}
which will cause the goToInstructions method to be executed when you probably expected :)
bourne bourne

2013/12/2

#
The problem is, the method goToInstructions is only being called once. Move goToInstructions(); from the Castle constructor to its act method like so:
public void act()
{
    goToInstructions();
}
SPower SPower

2013/12/2

#
@bourne :)
thekidj thekidj

2013/12/2

#
SPower wrote...
the goToInstructions method is only called in the constructor of your world, it should be in your act method:
public void act()
{
    goToInstructions();
}
which will cause the goToInstructions method to be executed when you probably expected :)
Silly mistake. Thanks.
You need to login to post a reply.