0

My question is, how can I get my move() methods to work using KeyEvents i.e. KeyEvent.VK_DOWN? I'm currently trying to use the import java.awt.event.KeyEvent; in which I'll be using the arrow keys NOT numpad keys to move a player in a 2 dimensional grid. I have my actions moveUp(); moveRight(); moveDown(); and moveLeft(); in my super class User and the class Player extends User and contains the key event method. When I use the arrow keys the actor simply does not move, however when I manually click on the actor in the grid and select a method it will move. Therefore my move methods work, so I'm assuming my KeyEvent setup is broken. Pictures showing me manually controlling the methods are supplied.

User which contains the move methods

package info.gridworld.actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

public class User extends Actor {
    private boolean isStopped = false;

    public User()
    {
        setColor(null);
    }

    public void moveUp(){
        moveTo(getLocation().getAdjacentLocation(Location.NORTH));
    }

    public void moveDown(){
        moveTo(getLocation().getAdjacentLocation(Location.SOUTH));
    }

    public void moveLeft(){
        moveTo(getLocation().getAdjacentLocation(Location.WEST));
    }

    public void moveRight(){
        moveTo(getLocation().getAdjacentLocation(Location.EAST));
    }
}  

Player class contains KeyEvents

package game.classes;
import info.gridworld.actor.User;
import java.awt.event.KeyEvent;


public class Player extends User{

    public Player(){
    }

    public void keyPressed(KeyEvent e){
        int keys = e.getKeyCode();
        if((keys == KeyEvent.VK_UP)){
            moveUp();
        }
        else if((keys == KeyEvent.VK_DOWN)){
            moveDown();
        }
        else if((keys == KeyEvent.VK_LEFT)){
            moveLeft();
        }
        else if((keys == KeyEvent.VK_RIGHT)){
            moveRight();
        }
    }


}  

Main class

package game.classes;
import info.gridworld.grid.*;

public class PlayerRunner{


    private static GameGrid world = new GameGrid();

    public static void main(String[] args)
    {
        Player player = new Player();
        world.add(new Location(0, 0), player);
        world.show();
  }
}  

PreMove

moveDown() Selected

Adamc23
  • 157
  • 1
  • 11
  • I don't think that's how it works, doesn't `World` receive key information? – MadProgrammer Jun 10 '14 at 00:38
  • 1
    Possible duplicate of [How to move a Bug in GridWorld with arrow keys](http://stackoverflow.com/questions/15671466/how-to-move-a-bug-in-gridworld-with-arrow-keys) – MadProgrammer Jun 10 '14 at 00:39
  • I would recommend to use keyBinding for this – Rod_Algonquin Jun 10 '14 at 00:40
  • @MadProgrammer I will try that example I also did not see the keyPressed method in the ActorWorld class – Adamc23 Jun 10 '14 at 00:42
  • @Rod_Algonquin I thought that too, but the OP is using the `gridworld` API, which works slightly differently...not saying it's not possible, but if we can find something that works with the API it will produce better results, if that fails...hack, hack, hack ;) – MadProgrammer Jun 10 '14 at 00:45
  • @MadProgrammer the problem with creating a new class that extends ActorWorld is that I won't have access to my move() methods.... If I delete the stock method keyPressed in World class will my methods work? – Adamc23 Jun 10 '14 at 00:53
  • You should be able to access the `Actor` via `World#getGrid#get`...I think...not used the API before ;) – MadProgrammer Jun 10 '14 at 00:55
  • @MadProgrammer Sorry I edited my comment right after you answered but if I deleted the KeyPressed method could I keep my original design? – Adamc23 Jun 10 '14 at 01:00
  • No idea, what's the original design? – MadProgrammer Jun 10 '14 at 01:03
  • What I have in my question the User/Player setup I'm not really understanding the CharacterWorld setup you mentioned in the duplicate question – Adamc23 Jun 10 '14 at 01:04

2 Answers2

1

You're extending Actor, which has nothing to do with the GUI, and KeyEvents and related are a swing thing. You need to actually add a KeyListener to a JPanel. From what I can tell, right now you just have the extra methods in the Actor class.

The GUI isn't actually on the AP test, so there's not a lot out there on it, but it looks like you can extend info.gridworld.gui.GridPanel. So override the constructor as:

public GridPanel(DisplayMap map, ResourceBundle res)
{
    super(map, res);
    addKeyListener(new KeyListener()
    {
        // Put the KeyListener methods here. 
        // (All of them: KeyListener is an interface
    }
}

This is kind of rough, but I think it should work.

I assume you're going to have a reference to the Actor that you can move with the arrow keys anyway, so you can just call its move methods.

Mar Johnson
  • 215
  • 1
  • 7
  • @user3709396 super(map, res); doesn't meet arguments in JPanel method and requires me to remove map and res.........` The constructor JPanel(DisplayMap, ResourceBundle) is undefined Remove arguments to match 'JPanel()'` – Adamc23 Jun 10 '14 at 20:12
  • Also where would I put my move() methods? they're not accessible from the gridPanel and moving them to the grid panel doesn't work because I don't have access to location getters – Adamc23 Jun 10 '14 at 21:24
  • Are you extending GridPanel or JPanel? – Mar Johnson Jun 11 '14 at 01:29
  • The whole thing is I don't understand what you're doing, are you modifying the constructor in GridPanel or are you saying create a separate class that does all of this? Put this in the player class the user class? I can create all of the KeyEvents and whatnot I just am confused on where to add this for it to work correctly? – Adamc23 Jun 11 '14 at 02:05
  • Sorry but just realized this is a BAD way of going about this. Posted a better answer... – Mar Johnson Jun 11 '14 at 04:59
0

The World class actually has a keyPressed() method. You can just extend World and override this method. It will give you a string parameter to work with instead of a KeyEvent. Use javax.swing.KeyStroke.getKeyStroke(description).getKeyCode() to figure out which key was pressed. The source for the World class is her: https://github.com/parkr/GridWorld/blob https://github.com/parkr/GridWorld/blob/master/framework/info/gridworld/world/World.java aster/framework/info/gridworld/world/World.java

As for getting access to your Actor's methods, I would put a reference to an Actor in the World class. Then when you add the Actor you want to move to the world, you can just set it there. Then when you're dealing with the keystrokes you have access to the Actor you want to manipulate.

Mar Johnson
  • 215
  • 1
  • 7
  • Okay that should work but what's the reason for the location parameter? Is that the location of the actor? (getLocation();)? – Adamc23 Jun 11 '14 at 19:24
  • Also I believe one of your links is broken, I couldn't edit my previous comment which is why I created this new one. – Adamc23 Jun 11 '14 at 19:33
  • I think it's the Actor that's selected within the World gui. You shouldn't need it for what you're doing though – Mar Johnson Jun 12 '14 at 00:16