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

2014/8/28

getKey() not working!

K_wow K_wow

2014/8/28

#
Here's my code:
    public void act()
    {
        if (!loadingScreenAdded)
        {
            addObject(new LoadingScreen(), getWidth() / 2, getHeight() / 2);
            loadingScreenAdded = true;
        }
        if (playing)
        {
            if (Greenfoot.isKeyDown("e") || Greenfoot.isKeyDown("["))
            {
                paused = true;
                playing = false;
                Actor pauseMenu = new Actor() {};
                pauseMenu.setImage(new GreenfootImage(600, 300));
                pauseMenu.getImage().setColor(Color.BLACK);
                pauseMenu.getImage().fillRect(0, 0, 600, 300);
                pauseMenu.getImage().setColor(Color.GRAY);
                pauseMenu.getImage().fillRect(20, 20, 560, 260);
                pauseMenu.getImage().setColor(Color.WHITE);
                pauseMenu.getImage().setFont(pauseMenu.getImage().getFont().deriveFont(Font.BOLD));
                pauseMenu.getImage().setFont(pauseMenu.getImage().getFont().deriveFont(30.0F));
                pauseMenu.getImage().drawString("The game is paused.", 20, 50);
                pauseMenu.getImage().drawString("Press E or [ to continue playing.", 20, 100);
                pauseMenu.getImage().drawString("Press enter to return to the title screen.", 20, 150);
                addObject(pauseMenu, getWidth() / 2, getHeight() / 3);
            }

            if (!playerBlueAlive && !playerRedAlive)
            {
                //Greenfoot.stop();
                playing = false;
                Actor scoreDisplay = new Actor() {};
                scoreDisplay.setImage(new GreenfootImage(600, 300));
                scoreDisplay.getImage().setColor(Color.BLACK);
                scoreDisplay.getImage().fillRect(0, 0, 600, 300);
                scoreDisplay.getImage().setColor(Color.GRAY);
                scoreDisplay.getImage().fillRect(20, 20, 560, 260);
                scoreDisplay.getImage().setColor(Color.WHITE);
                scoreDisplay.getImage().setFont(scoreDisplay.getImage().getFont().deriveFont(Font.BOLD));
                scoreDisplay.getImage().setFont(scoreDisplay.getImage().getFont().deriveFont(30.0F));
                scoreDisplay.getImage().drawString("You were mauled by zombies!", 20, 50);
                scoreDisplay.getImage().drawString("Time: " + time / 62 + " seconds.", 20, 100);
                scoreDisplay.getImage().drawString("Press enter to return to the title screen.", 20, 150);
                addObject(scoreDisplay, getWidth() / 2, getHeight() / 3);
            }

            time++;
        }
        else
        {
            if (!playerBlueAlive && !playerRedAlive && "enter".equals(Greenfoot.getKey()) || paused && "enter".equals(Greenfoot.getKey()))
            {
                removeObjects(getObjects(null));
                
                playerBlueX = 0;
                playerBlueY = 0;
                playerRedX = 0;
                playerRedY = 0;
                playerBlueWeapon = 0;
                playerRedWeapon = 0;
                playerBlueAlive = true;
                playerRedAlive = false;
                playing = false;
                players = 1;
                
                setPaintOrder(LoadingScreen.class, Button_Singleplayer.class, Button_Multiplayer.class, TitleScreen.class, Timer.class, ComboDisplay.class, BackItem.class, Player_Blue.class, Player_Red.class, Player_Arms.class, Sword.class, Trail.class, Explosion.class, ExplosionSmall.class, Zombie.class, Grenade.class, Bullet.class, Shield.class, Weapon.class, Bomb.class, Medpack.class);

                Player_Blue playerBlue = new Player_Blue();
                addObject(playerBlue, getWidth() / 2 - 20, getHeight() / 2);
                playerBlue.setRotation(180);

                addObject(new Lifebar(playerBlue), getWidth() / 2 - 20, getHeight() / 2);

                addObject(new TitleScreen(), getWidth() / 2, getHeight() / 2);
                addObject(new Button_Singleplayer(), 550, 300);
                addObject(new Button_Multiplayer(), 550, 400);

                addObject(new Timer(), getWidth() - 120, 40);
            }
            if (paused && "e".equals(Greenfoot.getKey()) || paused && "[".equals(Greenfoot.getKey()))
            {
                removeObjects(getObjects(Actor.class));
                playing = true;
                paused = false;
            }
        }
    }
danpost danpost

2014/8/28

#
You are calling 'getKey' four times in this act method -- twice on line 52 and twice again on line 81. The only one that could possibly return a value that is not 'null' is the first one on line 52. Once a key is returned by 'getKey', the key is removed from the list of keys to return and the next key, if any, will be returned on the second call to 'getKey' (the second one on line 52). What you need to do is add the following line before line 52 (inserted at line 52):
String key = Greenfoot.getKey();
and compare it ( 'key' ) to each possible as needed. Please note that if you are using 'getKey' somewhere else (in another class perhaps) that the same problem may occur (if both are being used within the same act cycles) and you will need to find a more general location to get the key and work with it there.
K_wow K_wow

2014/8/28

#
Thanks! It worked, to some extent. I managed to sort out the rest of the problems.
You need to login to post a reply.