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

2016/11/3

Key Release

BGallino3 BGallino3

2016/11/3

#
Hey, I need some help with my Game. I would like it to run a method, but only when my key is released. public void startgame() { cloudy myWorld = (cloudy)getWorld(); if (Greenfoot.isKeyDown("space")) { myWorld.prepare(); myWorld.fixtwo2(); } } This is what I have so far.
danpost danpost

2016/11/3

#
To detect, not the state of, but a change in the state of a key, whether it be the pressing or the releasing, you will need an instance boolean field to track the state of the key:
// with instance field
private boolean spaceDown;

// to check change in state
if (spaceDown != Greenfoot.isKeyDown("space"))
{
    spaceDown = ! spaceDown; // record new state
    if ( ! spaceDown) // new released state
    {
        // etc.
You need to login to post a reply.