Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Top Down Shooter Movement  (Read 2833 times)
0 Members and 1 Guest are viewing this topic.
Offline Vladiedoo
« Posted 2012-04-11 03:04:40 »

-Sorry for the lengthty post
Hi I got source code from the Articles & tutorials section from the first post "Basic Game." I implemented WASD control over a square that is being drawn on the screen but I'm having a small issue. If I'm moving right and click "A" without letting go of "D" I will eventually stop moving once I let go of "D" and then continue moving left again because I'm still holding down "A".

   private double x = 0;
   private double y = 0;
   private int velX, velY = 0;
   private int movementSpeed = 4;

   protected void update(int deltaTime) {
      if (y > 700 - 30) { //Make sure square doesn't leave the screen
         y = 700 - 30;
      } else if (y < 0) {
         y = 0;
      } else {
         y += velY;
      }
      if (x > 1000 - 30) { //Make sure square doesn't leave the screen
         x = 1000 - 30;
      } else if (x < 0) {
         x = 0;
      } else {
         x += velX;
      }
        }

   protected void render(Graphics2D g) {
                g.fillRect((int) x, (int) y, 30, 30);
        }

   @Override
   public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_W) {
         velY = -movementSpeed;
      }
      if (e.getKeyCode() == KeyEvent.VK_A) {
         velX = -movementSpeed;
      }
      if (e.getKeyCode() == KeyEvent.VK_S) {
         velY = movementSpeed;
      }
      if (e.getKeyCode() == KeyEvent.VK_D) {
         velX = movementSpeed;
      }
         }

   @Override
   public void keyReleased(KeyEvent e) {

      if (e.getKeyCode() == KeyEvent.VK_W) {
         velY = 0;
      }
      if (e.getKeyCode() == KeyEvent.VK_A) {
         velX = 0;
      }
      if (e.getKeyCode() == KeyEvent.VK_S) {
         velY = 0;
      }
      if (e.getKeyCode() == KeyEvent.VK_D) {
         velX = 0;
      }
   }

Thank you SO much for any help you can offer.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-04-11 03:58:11 »

So what exactly is the problem? This is normal and expected behavior from the code. Smiley

Offline roland
« Reply #2 - Posted 2012-04-11 03:59:07 »

I would make a boolean array of keys down, eg.

1  
2  
3  
4  
5  
6  
7  
8  
9  
private boolean keydown[255]; //make this a field

public void keyPressed(KeyEvent e) {
    keydown[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
    keydown[e.getKeyCode()] = false;
}



then in the update method go

1  
2  
3  
4  
5  
if (keydown[KeyEvent.VK_W])
{
    velY = -movementSpeed;
}
//do the same for the rest



What this does is it stores all keypresses, you are no longer hindered by only changing velocity when the keydown or key up events are trigged. You ALWAYS know what key is down and which is not Smiley

you can then go
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
if (keydown[KeyEvent.VK_A] && keydown[KeyEvent.VK_D]) //both keys are down
{
    //don't change the velocity because both keys are down
   //or you could set velX to 0, it depends how you want the game to work.
}
else if (keydown[KeyEvent.VK_A])
{
    velX = -movementSpeed;
} else if (keydown[KeyEvent.VK_D])
{
    velX = movementSpeed;
}





Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Vladiedoo
« Reply #3 - Posted 2012-04-11 04:01:14 »

So what exactly is the problem? This is normal and expected behavior from the code. Smiley

I'm wondering if anyone knows a way to implement WASD movement with you being able to move smoothly, so no un-wanted stops.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-04-11 04:02:58 »

Ah, then either go for roland's idea, or just simply 4 booleans: upPressed, downPressed, leftPressed, and rightPressed; which you would set to true/false based on whether they're pressed or not Smiley

Offline roland
« Reply #5 - Posted 2012-04-11 04:06:47 »

also: don't just go VelX = movementSpeed
GO velX += movementSpeed or velX -= movementSpeed

this will be smooth as long as movementSpeed isnt too big.
Otherwise you can instantly switch between positive and negative velocity (bad)


movementSpeed will probably have to be about ~0.1 for this
Offline Vladiedoo
« Reply #6 - Posted 2012-04-11 04:26:57 »

Delayed response because I was trying to get it to work, and now it works as intended!

roland and ra4king thank you so much!!! : D
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!