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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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:
1
2
3
4
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:
1
2
3
4
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:
1
2
3
4
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.