Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: right and up action problem

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default right and up action problem

     public void buttons(){
    	 int c = WHEN_IN_FOCUSED_WINDOW;
     
    		Action right = new AbstractAction() {
    		    public void actionPerformed(ActionEvent e) {
    		    	player.setVX(2);
    		    }
    		};
    		Action stop = new AbstractAction() {
    		    public void actionPerformed(ActionEvent e) {
    		    	player.setVX(0);
    		    	player.setVY(0);
    		    }
    		};
     
    		Action up = new AbstractAction() {
    		    public void actionPerformed(ActionEvent e) {
    		    	player.setVY(-2);
    		    }
    		};
    		   getInputMap(c).put(KeyStroke.getKeyStroke("D"), "pressed");
    		   getActionMap().put("pressed", right);
    		   getInputMap(c).put(KeyStroke.getKeyStroke("released D"), "released");
    		   getActionMap().put("released", stop);
    		   getInputMap(c).put(KeyStroke.getKeyStroke("W"), "pressed");
    		   getActionMap().put("pressed", up);
    		   getInputMap(c).put(KeyStroke.getKeyStroke("released W"), "released");
    		   getActionMap().put("released", stop);
     
     }

    Why is that when I press W or D it goes up...

    What is the problem?

    D should go right


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: right and up action problem

    How does the program know the difference between "pressed" and "pressed"
    Try using different Strings.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Action Center problem
    By lf2killer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 23rd, 2012, 05:31 AM
  2. Problem with Action Event
    By jayzee98 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: August 29th, 2012, 10:04 AM
  3. Problem with Animation and Button Action
    By ygeva in forum AWT / Java Swing
    Replies: 8
    Last Post: December 3rd, 2011, 01:57 PM
  4. button action problem
    By macko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 10:51 AM
  5. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM