Results 1 to 4 of 4

Thread: Key Presses

  1. #1
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Key Presses

    Hello! I am writing a program where i am trying to control a rectangle and determine where it moves, everything is working fine it is moving but i am trying to figure out how to change direction using the "WASD" keys. This is what I have and I am not sure why its not working. Any tips ?

    Java Code:
    addKeyListener( new KeyAdapter()
    		{
    			public void keyReleased(KeyEvent event)
    			{
    				for (MoveableShape creature : creatures)
    				{
    					if (((Creature)creature).isPlayer())
    					{
    						if (event.getKeyCode() == KeyEvent.VK_W) //up
    						{
    							((Creature)creature).setDirection(0, -1);
    						}
    						else if (event.getKeyCode() == KeyEvent.VK_A) //left
    						{
    							((Creature)creature).setDirection(-1, 0);
    						}
    						else if (event.getKeyCode() == KeyEvent.VK_S) //down
    						{
    							((Creature)creature).setDirection(0, 1);
    						}
    						else if (event.getKeyCode() == KeyEvent.VK_D) //right
    						{
    							((Creature)creature).setDirection(1, 0);
    						}
    					}
    				}
    			}
    		});

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Key Presses

    You're setting both your horizontal and vertical speed when any key is pressed. That doesn't seem right. You probably only want to set the horizontal speed when A or D are pressed (and released), and you probably only want to set the vertical speed when the W or S are pressed (and released).
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    rang3tastic is offline Member
    Join Date
    Oct 2013
    Posts
    13
    Rep Power
    0

    Default Re: Key Presses

    Although it may not seem right, it is working as expected. I have used the same code for mouse listener, and it is working with mouse clicks. My issue here is not whether the speed or not is set correctly, my issue is that it is unresponsive to my key presses.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: Key Presses

    Can you create an SSCCE that demonstrates exactly the problem? Does you keyReleased() function get triggered at all?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Detecting if the player presses q or e
    By meesterpickles in forum AWT / Swing
    Replies: 3
    Last Post: 12-18-2011, 05:38 PM
  2. How to ERROR if user presses Enter?
    By net2chris in forum New To Java
    Replies: 3
    Last Post: 10-03-2011, 06:30 PM
  3. Why isnt my program detecting any key presses?
    By rajkobie in forum New To Java
    Replies: 6
    Last Post: 05-19-2011, 06:45 PM
  4. Multiple keyboard presses
    By abbeywell in forum Java Gaming
    Replies: 6
    Last Post: 05-02-2011, 05:57 PM
  5. Responding to button presses
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:38 PM

Posting Permissions

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