Results 1 to 5 of 5
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default Switch Case and Key Events

    Hey. I had a small project for school not too long ago. I developed pong to the best of my abilities, but there was one major flaw. When someone tried to hold down w/s at the same time as up/down (to control the padels) they wouldn't be able to move both at the same time. This really interfered with game play. I had them in a switch case, but I'm not sure if this was the best way to go about it. So, tell me what you think. Here's my code for that portion:

    Java Code:
    	public void keyPressed(KeyEvent e)
    	{
    		switch (e.getKeyCode())
    		{
    		case KeyEvent.VK_W:
    			left.setY(left.getY()-10);
    				break;
    
    		case KeyEvent.VK_S:
    			left.setY(left.getY()+10);
    				break;
    				
    		case KeyEvent.VK_UP:
    			right.setY(right.getY()-10);
    				break;
    
    		case KeyEvent.VK_DOWN:
    			right.setY(right.getY()+10);
    				break;	
    
    		}
    	}
    left and right are padel objects. getX, getY, setX, and setY are pretty obvious.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Perhaps you shouldn't use keylisteners here but instead use Swing with key bindings. There's a decent tutorial on this to get you started in the Sun Swing tutorial.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default

    Ok thanks. I'm teaching myself Swing now anyway, so I should come across that section soon.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Your code isn't very sophisticated for the purpose. If the player presses and hold a key, that generates one keyPressed event. Now, you should start moving the paddle, until you get a keyReleased event for the same key, at which point you stop moving the paddle.

    As it is, the player must continuously press and release the key to make the paddle move. I'm not sure what happens when one key is pressed and held, and another key is released.

    So. First, test the behavior of holding one key and then pressing another. See if the second key generates a keyPressed event. If it doesn't, you will have to prohibit holding down a key.

    It it does, look at the javax.swing.Timer class (not the other two). When a keyPress event happens, start a Timer that moves the paddle every so often (30 millis is a good value). When a keyReleased event happens, stop the timer.

  5. #5
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    261
    Rep Power
    6

    Default

    Interesting idea for that, I never would have thought to do it like that. I'll give it a try, thanks!

Similar Threads

  1. String Title case
    By bugger in forum New To Java
    Replies: 6
    Last Post: 01-31-2012, 01:21 PM
  2. How to convert a String to upper case
    By Valeriano in forum New To Java
    Replies: 16
    Last Post: 03-01-2010, 12:39 PM
  3. Case Based Reasoning
    By kbyrne in forum Advanced Java
    Replies: 4
    Last Post: 04-12-2008, 08:51 PM
  4. problem with operator in case
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 03-21-2008, 08:22 PM
  5. Can I set a range in case statement?
    By christina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:41 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
  •