2

I'm making a game for my computer Science class and I am trying to move a character object that extends Bug with the arrow keys. Should I put the code to move with the arrow keys in the Character class or in the World class? And what should the code look like? Right now I've got this code in the Character class and it complies fine, but when I try to to run it in the grid nothing happens when I press the arrow keys.

 public class Character extends Bug
{
Random pokemon;
public Character()
{

}

public void act(KeyEvent e)
{
        move(e);
        pokemon = new Random();
        if(pokemon.nextInt(10) == 5)
            System.out.println("It works!!");
}

public void move(KeyEvent e)
{
    Grid<Actor> gr = getGrid();
    Location loc = getLocation();
    if(gr == null)
        return;
    if( e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        if(!(getDirection() == 90))
            setDirection(90);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        if(!(getDirection() == 270))
            setDirection(270);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_UP)
    {
        if(!(getDirection() == 0))
            setDirection(0);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        if(!(getDirection() == 180))
            setDirection(180);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }

public class Character extends Bug
{
Random pokemon;
public Character()
{

}

public void act(KeyEvent e)
{
        move(e);
        pokemon = new Random();
        if(pokemon.nextInt(10) == 5)
            System.out.println("It works!!");
}

public void move(KeyEvent e)
{
    Grid<Actor> gr = getGrid();
    Location loc = getLocation();
    if(gr == null)
        return;
    if( e.getKeyCode() == KeyEvent.VK_RIGHT)
    {
        if(!(getDirection() == 90))
            setDirection(90);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_LEFT)
    {
        if(!(getDirection() == 270))
            setDirection(270);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_UP)
    {
        if(!(getDirection() == 0))
            setDirection(0);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }
    else if( e.getKeyCode() == KeyEvent.VK_DOWN)
    {
        if(!(getDirection() == 180))
            setDirection(180);
        else
        {

            Location next = loc.getAdjacentLocation(getDirection());
            if (gr.isValid(next))
                moveTo(next);
            else
                removeSelfFromGrid();
        }
    }

Is this code correct for a KeyEvent and how can I call on this code from the World class? Any help would be greatly appreciated!

1 Answers1

1

In the ActorWorld class there is a method boolean keyPressed(String description, Location loc) this method is the for the sole purpose of being overridden in a subclass. description is the KeyStroke in the format found here, and loc is the Location that the cursor was on when the key was pressed. (Although in your case it doesn't matter)

So in short, you should extend KeyPressed in a Custom CharacterWorld extends ActorWorld class.

i .
  • 485
  • 2
  • 10
  • 23
  • Thanks I didn't know there was a keyPressed method, this helped a little bit. I am still stuck on what to put in the keyPressed method so that I can move the Character with the arrow keys. – Dustin Weinmann Apr 24 '13 at 21:37
  • you can use a switch statement to determine whether the String description is "UP", "DOWN", "LEFT", or "RIGHT". In any of those cases, the method should move the character and then return true. if it is none of those keystrokes, return false. – i . Apr 25 '13 at 02:38