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

2015/1/8

isKeyDown issue

Kryptonous Kryptonous

2015/1/8

#
Hello everyone: I got a little issue here with my isKeyDown.
1
2
3
4
5
6
7
8
9
10
11
public Dambord()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 500, 1);
        SchijvenAdd();
        if (Greenfoot.isKeyDown("o"))
        {
            setBackground("game-over.jpg");
            Greenfoot.stop();
        }
    }
It's placed in the World - subclass. I don't know why, there are no errors but whenever I press 'o', the program won't stop/won't display game-over. Either way, I have one piece of this too in my Actor-subclasses, tried it there as well, didn't work. A classmate had the same problem. Anyone find the little problem?
Stoeptegel Stoeptegel

2015/1/8

#
You put code in the world constructor. The constructor only gets initialized (it runs when the world is constructed). So if you want to stop the game, you can't do that in a part which only runs when the world is created. You would need to create a method. Then you call that method in the "act" method. The "act" method executes every frame. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Dambord()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 500, 1);
    }
         
 
    public void act () {
        gameOver();
    }
 
    public void gameOver() {
        if (Greenfoot.isKeyDown("o"))
        {
            setBackground("game-over.jpg");
            Greenfoot.stop();
        }
    }
I removed Schijvenadd(); from this code because i don't know what it's for. I think you can just put it in the "act" method as well. Depends on what it's for.
Kryptonous Kryptonous

2015/1/8

#
Alright, thanks! That did the trick. But true, I forgot about the constructor in world. Now, why doesn't it work in the Actor (as that was my first, actual plan)?
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
{
    gameOver();
}  
public void gameOver()
{
    if (Greenfoot.isKeyDown("s"))
    {
        getWorld().setBackground("game-over.jpg");
        Greenfoot.stop();
    }
}
Thanks!
danpost danpost

2015/1/8

#
Mouse and keyboard detection does not function unless the scenario is in a running state and act methods are being cycled through. You cannot use them in your initial world constructors -- they will not work. They will work, however, in the constructor of a world that is created while the project is already running (which means it will not be your initial world).
Kryptonous Kryptonous

2015/1/8

#
Yes, but the code in the World-subclass is already solved. The other piece of code (my second post) is located in the Actor-subclass. So normally, it should be available right after compiling and running the project, no?
danpost danpost

2015/1/8

#
If an actor of the class you placed the code is in the world and you press the "S" key, it should work as is.
Kryptonous Kryptonous

2015/1/8

#
Didn't think of that (...) is in the world(...)! Thanks, that was the problem, then. Discussion locked!
You need to login to post a reply.