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.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
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); } } }