2 Replies - 9264 Views - Last Post: 22 March 2013 - 05:56 PM Rate Topic: -----

#1 Amieva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-February 13

GridWorld KeyPressed Method

Posted 04 February 2013 - 08:43 PM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: GridWorld KeyPressed Method

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

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.

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
Was This Post Helpful? 0
  • +
  • -

#3 Zode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-March 13

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! :bananaman:/>
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();
                      }
         }   
});


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1