1 Replies - 10585 Views - Last Post: 25 May 2012 - 03:09 PM Rate Topic: -----

#1 medingdong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-May 12

How to make an actor that moves when keys are pressed?

Posted 22 May 2012 - 07:15 AM

I am not very good with GridWorld, but for my final project, we were asked to use gridworld in order to create a game. I want to make an subclass of Bug that when you press 'W' for example, it moves up on the grid, when you press 'S' moves down, press 'A' moves left, 'D' moves right. This is the only problem I have with my game. Could someone please help me on how I can go about this.
Is This A Good Question/Topic? 0
  • +

Replies To: How to make an actor that moves when keys are pressed?

#2 wildcatqw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-May 12

Re: How to make an actor that moves when keys are pressed?

Posted 25 May 2012 - 03:09 PM

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();
      if (key.equals("pressed UP"))
        world.getPlayer().setPacDir(0);
      if (key.equals("pressed RIGHT"))
        world.getPlayer().setPacDir(90);
      if (key.equals("pressed DOWN"))
        world.getPlayer().setPacDir(180);
      if (key.equals("pressed LEFT"))
        world.getPlayer().setPacDir(270);
      return true;
    }
    }); 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1