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

2014/6/4

Help with getKey() and booleans

PixeledView PixeledView

2014/6/4

#
Hi i'm really new to programming, just doing it for school and its cool :D I just need help making a loop for a pacman game. I am trying to use the getKey() method to recognize the up key to make a boolean true and when its not the most recent key it will become false. there are 4 booleans for 4 directions by the way. This is my current code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Movings
{
    public boolean upDirection;
    public boolean downDirection;
    public boolean leftDirection;
    public boolean rightDirection;
    public Player()
    {
        setImage("pacman1.png");
    }
    public void act() 
    {
        updateDirection();
        moveAndAnimate();
    }    
    public void checkKeys()
    {
        
    }
    public void updateDirection()
    {
        String key = Greenfoot.getKey();
        if ("up".equals(key))
        {
            upDirection = true;
        }
        else
        {
            upDirection = false;
        }
        if (Greenfoot.getKey() == ("down"))
        {
            downDirection = true;
        }
        else
        {
            downDirection = false;
        }
        if (Greenfoot.getKey() == ("left"))
        {
            leftDirection = true;
        }
        else
        {
            leftDirection = false;
        }
        if (Greenfoot.getKey() == ("right"))
        {
            rightDirection = true;
        }
        else
        {
            rightDirection = false;
        }
    }
    public void moveAndAnimate()
    {
        while (upDirection == true)
        {
            setLocation(getX(), getY() - 2);
        }
    }
}
Also when i have while to make a loop it crashes greenfoot so when i click run i cant click pause and when i compile it it disappears basically. Thanks for any help :D
PixeledView PixeledView

2014/6/4

#
I also wanted to know if instead of using 4 booleans could i use a string? Thanks :D
lordhershey lordhershey

2014/6/4

#
It looks like you are getting the keys too many times. Do the first key = getKey() the do the 4 tests: key.equals("up") , key.equals("down") , you would not have to do the get key more than once per act method unless you were doing some kind game that used key combos.
davmac davmac

2014/6/4

#
lordhershey is correct. You could also condense the whole method considerably:
    public void updateDirection()  
    {  
        String key = Greenfoot.getKey();  
        upDirection =  ("up".equals(key));
        downDirection = ("down".equals(key));
        leftDirection = ("left".equals(key));
        rightDirection = ("right".equals(key));
    }  
davmac davmac

2014/6/4

#
Also when i have while to make a loop it crashes greenfoot
It doesn't crash Greenfoot, it's your scenario that is crashing (going into an infinite loop). Just remove the 'while' loop and keep what's inside it. The act() method is called repeatedly anyway, so you don't need to code your own loop here.
You need to login to post a reply.