Results 1 to 3 of 3
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default Key Binding issues in game

    I have some code that should do key binding if it's written correctly, and everything compiles just fine, but now I can't move in my game again, and I don't know what's wrong..

    Here's some code from my Player class:

    Java Code:
      public void moveRight(ActionEvent e)
      {
        movex=1;
        mcounter = 1;
        SIDE = 1;
      }
    
      public void moveLeft(ActionEvent e)
      {
        movex=-1;
        mcounter = -1;
        SIDE = 0;
      }
    
      public void moveUp(ActionEvent e)
      {
        movey = 20;
      }
    
      public void moveDown(ActionEvent e)
      {
        standingUp = false;
      }

    and some from my Inner class:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    
    public class Inner extends JPanel implements ActionListener
    {
    
      public Player playuh;
      public InputMap inputmap;
      public ActionMap actionmap;
    
      Action moveright = new AbstractAction()
      {
        public void actionPerformed(ActionEvent e)
        {
          playuh.moveRight(e);
        }
      };
    
      Action moveleft = new AbstractAction()
      {
        public void actionPerformed(ActionEvent e)
        {
          playuh.moveLeft(e);
        }
      };
    
      Action moveup = new AbstractAction()
      {
        public void actionPerformed(ActionEvent e)
        {
          playuh.moveUp(e);
        }
      };
    
      Action movedown = new AbstractAction()
      {
        public void actionPerformed(ActionEvent e)
        {
          playuh.moveDown(e);
        }
      };
    
      Inner()
      {
        super();                              
        setBackground(Color.BLACK);
        setFocusable(true);
        addMouseListener(new RAdapter());
        playuh = new Player();
        setDoubleBuffered(true);
        setSize(600,465);
        inputmap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        inputmap.put(KeyStroke.getKeyStroke("d"), "one");
        inputmap.put(KeyStroke.getKeyStroke("a"), "two");
        inputmap.put(KeyStroke.getKeyStroke("w"), "three");
        inputmap.put(KeyStroke.getKeyStroke("s"), "four");
        actionmap = getActionMap();
        actionmap.put("one", moveright);
        actionmap.put("two", moveleft);
        actionmap.put("three", moveup);
        actionmap.put("four", movedown);
      }

  2. #2
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Nevermind, I found out that the chars I was using were not the registered chars for W, A, S, D. I changed that and now my code works :D

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Fortu View Post
    Nevermind, I found out that the chars I was using were not the registered chars for W, A, S, D. I changed that and now my code works :D
    I have used the Thread Tools at the top of the thread to mark this thread "solved".

Similar Threads

  1. JSP and javascript binding???
    By paulb in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-02-2011, 09:33 PM
  2. Help: Key Binding
    By mine0926 in forum New To Java
    Replies: 0
    Last Post: 08-06-2010, 05:52 AM
  3. dynamic binding
    By apparao in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 09:41 AM
  4. [SOLVED] Key binding Q
    By playwin2 in forum New To Java
    Replies: 3
    Last Post: 11-04-2008, 07:04 AM
  5. Spring binding in jsp
    By Pierre Javason in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-05-2008, 01:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •