0

Here is my code and I am trying to get this cat to move with a, s, w, d and this box to move with the arrow keys. I was using switch case statements and the box works, but not the cat. I am new to programming and I really need help. Do I need to use key bindings instead?:

public class Other extends Applet implements ActionListener, KeyListener {

    public static final int MOVE_STOP = 0;
    public static final int MOVE_UP = 1;
    public static final int MOVE_DOWN = 2;
    public static final int MOVE_RIGHT = 3;
    public static final int MOVE_LEFT = 4;
    public static final int MOVE_W = 5;
    public static final int MOVE_S = 6;
    public static final int MOVE_D = 7;
    public static final int MOVE_A = 8;

    int CursorDirection = MOVE_STOP;
    int cursordirection = MOVE_STOP;

    private ArrayList<Integer> keysDown;

    public void init()
    {
        this.addKeyListener(this);   

        keysDown = new ArrayList<Integer>();
        rect = new Rectangle(1000, 715, 35, 35);
    }

    public void paint(Graphics g){
        setSize(2000, 2000);
        this.CalculateMappyPosition();
        //i edited the background out
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (!keysDown.contains(e.getKeyCode()))
            keysDown.add(new Integer(e.getKeyCode()));

        moveRect();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        keysDown.remove(new Integer(e.getKeyCode()));
    }

    public void CalculateMappyPosition() {

        switch (CursorDirection) {
        case MOVE_UP:
            rect.y-= 100;
            break;

        case MOVE_DOWN:
            rect.y+= 100;
            break;

        case MOVE_LEFT:
            rect.x-= 5;
            break;

        case MOVE_RIGHT:
            rect.x+= 5;
            break;

        case MOVE_A:
            cat.x1-= 5;
            break;

        case MOVE_S:
            cat.y1+= 100;
            break;

        case MOVE_D:
            cat.x1+= 5;
            break;

        case MOVE_W:
            cat.y1-= 100;
            break;            
        default:
            break;
        }
    }

    public void moveRect()
    {
        repaint();

        if(keysDown.contains(KeyEvent.VK_UP))
            up();

        }

        if (keysDown.contains(KeyEvent.VK_DOWN))
            down();
    }

    if(keysDown.contains(KeyEvent.VK_LEFT))
        left();
    }

    if(keysDown.contains(KeyEvent.VK_RIGHT))
        right();
    }

    if(keysDown.contains(KeyEvent.VK_A)){
        a();

    }
    if(keysDown.contains(KeyEvent.VK_S)){
        s();
    }
    if(keysDown.contains(KeyEvent.VK_D)){

    }
    if(keysDown.contains(KeyEvent.VK_W)){
        w();
    }

    @Override 
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }
    public void up(){
        CursorDirection = MOVE_UP;
    }
    public void down(){
        CursorDirection = MOVE_DOWN;
    }
    public void left(){
        CursorDirection = MOVE_LEFT;
    }
    public void right(){
        CursorDirection = MOVE_RIGHT;
    }
    public void a(){
        CursorDirection = MOVE_A;
    }
    public void s(){
        CursorDirection = MOVE_S;
    }
    public void d(){
        CursorDirection = MOVE_D;
    }
    public void w(){
        CursorDirection = MOVE_W;
    }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Nikki
  • 21
  • 1
  • Check this http://stackoverflow.com/questions/10876491/how-to-use-keylistener – Stefan Falk Dec 14 '14 at 23:20
  • 1
    Don't call `setSize` within the `paint` method, apart from been inappropriate for painting (`paint` should paint the current state, not try to change it), it's inappropriate for applets, as the size of the applet is defined by the html code which loads it. – MadProgrammer Dec 14 '14 at 23:21
  • Can you show the code that creates the cat? And the code that declares and sets the `cat` variable? I suspect you may have a duplicate variable name, or something of that kind. – Dawood ibn Kareem Dec 14 '14 at 23:28
  • Your movement methods (which set the `CursorDirection`) seem to be overriding the state of what is actually pressed (so if you press "UP" and "A", "A" seems to override "UP") – MadProgrammer Dec 14 '14 at 23:31

0 Answers0