In my Programming class we're working with GridWorld. For fun, I'm recreating Pacman in GridWorld down to the details. I've already programmed all 4 ghosts to work with their personalities and chase Pacman efficiently. The last piece of the puzzle is to actually program Pacman to move, or respond to keys.
In GridWorld there's a "KeyPressed" method. My question is, if anyone here knows, how do you use this method correctly in your code, and where would you use it?
Does the actor need to reference it in it's "act()" method?
GridWorld KeyPressed Method
Page 1 of 12 Replies - 9264 Views - Last Post: 22 March 2013 - 05:56 PM
Replies To: GridWorld KeyPressed Method
#2
Re: GridWorld KeyPressed Method
Posted 13 February 2013 - 06:58 PM
It looks like Dr. Horstmann is pretty explicit. You would want to use the World class you created rather than the standard World class.
http://www.horstmann...-gridworld.html
Quote
Override the keyPressed method so that something happens when the user hits a key. Your method should return true if you consume the key press. Don't consume standard keys such as the arrow keys or Enter. (Shifted arrows are ok to consume.)
http://www.horstmann...-gridworld.html
#3
Re: GridWorld KeyPressed Method
Posted 22 March 2013 - 05:56 PM
I put this code in the main method of my driver of the game I wrote. Enjoy!
/>
java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new java.awt.KeyEventDispatcher() {
public boolean dispatchKeyEvent(java.awt.event.KeyEvent event) {
String key = javax.swing.KeyStroke.getKeyStrokeForEvent(event).toString();
//Listens for specific key
if (key.equals("pressed UP")) {
thing.doStuff();
}
}
});
Page 1 of 1

New Topic/Question
Reply


MultiQuote






|