Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  [Solved] How would you know if the key was being held? For LD23!  (Read 1229 times)
0 Members and 1 Guest are viewing this topic.
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Posted 2012-04-22 05:43:43 »

In the Ludum Dare right now, I am working on a game. I call this code to move the player's position.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
package src.longarmx.ld23;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Input implements KeyListener{
   
   public static int playerUpKey = KeyEvent.VK_W;
   public static int playerDownKey = KeyEvent.VK_S;
   public static int playerRightKey = KeyEvent.VK_D;
   public static int playerLeftKey = KeyEvent.VK_A;
   
   public void keyPressed(KeyEvent e) {
      if(e.getKeyCode() == playerUpKey){
         if(Player.playerY >= 0)
         Player.playerY -= Player.playerSpeed;
      }
      if(e.getKeyCode() == playerDownKey){
         if(Player.playerY <= World.worldHeight * World.blockSize - World.blockSize*2)
         Player.playerY += Player.playerSpeed;
      }
      if(e.getKeyCode() == playerRightKey){
         if(Player.playerX <= World.worldWidth * World.blockSize - World.blockSize)
         Player.playerX += Player.playerSpeed;
      }
      if(e.getKeyCode() == playerLeftKey){
         if(Player.playerX >= World.blockSize)
         Player.playerX -= Player.playerSpeed;
      }
   }

   public void keyReleased(KeyEvent e) {   }


   public void keyTyped(KeyEvent e) {   }
}


The bad thing about this is that when you hold down the key, the player's position doesn't keep updating. You have to keep pressing the key to get the player to move. I want to know how to keep the player moving while only having to hold down the key.

Thank you,
Longarmx

Thread.sleep(∞);

Color Game
Offline ReBirth
« Reply #1 - Posted 2012-04-22 05:53:16 »

Use booleans. When dX boolean is true then give value to dX speed, otherwise dX speed is zero. Do it for Y too.

Offline Rorkien
« Reply #2 - Posted 2012-04-22 06:13:55 »

I would also rely on a update() method, instead of using the events to handle logic

Little example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
public class Input implements KeyListener {
   public Game game;

   public boolean keys[] = new boolean[65535];
   public boolean w, s, a, d;
     
   public Input(Game game) {
      this.game = game;
      game.addKeyListener(this);
   }
   
   public void update() {  
      w = keys[KeyEvent.VK_W];
      s = keys[KeyEvent.VK_S];
      a = keys[KeyEvent.VK_A];
      d = keys[KeyEvent.VK_D];
   }

   public void keyPressed(KeyEvent e) {
      int key = e.getKeyCode();
      if (key > 0 || key < keys.length) {
         keys[key] = true;
      }
   }

   public void keyReleased(KeyEvent e) {
      int key = e.getKeyCode();
      if (key > 0 || key < keys.length) {
         keys[key] = false;
      }
   }

   public void keyTyped(KeyEvent e) {
   }

}


And then call the update() from your main loop
Good luck!
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #3 - Posted 2012-04-22 06:23:11 »

Thank you do much! I am so tired right now I don't know why I couldn't figure this out by myself. Smiley It works great now and hopefully I can get this game done in time.  Grin

Thank You,
Longarmx

Thread.sleep(∞);

Color Game
Offline ReBirth
« Reply #4 - Posted 2012-04-22 06:29:02 »

I just starting to create my entry now. Let's see which one better Smiley

Offline Rorkien
« Reply #5 - Posted 2012-04-22 06:33:05 »

I usually watch people code when LD is on

but there are only 240p streams, i couldnt even see what program the last one was using Sad
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #6 - Posted 2012-04-22 06:45:50 »

Quote
I just starting to create my entry now. Let's see which one better  Smiley

Haha, I started creating mine 3 hours ago. You have no chance! Yawn (I meant I have no chance  Grin)

PS: I'm going to burn my graphics!

Thread.sleep(∞);

Color Game
Offline ReBirth
« Reply #7 - Posted 2012-04-22 07:48:36 »

3 hours?! then I guess you already done a RPG game with complete self drawn sprite and all battle system :p

Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #8 - Posted 2012-04-22 15:36:13 »

No, I'm still a noob Wink I've only gotten the world, player, coins, and customizeable maps semi-decent. I know, I'm going to be typing 100 wpm the hour before the competition is finished.  Grin

Thread.sleep(∞);

Color Game
Offline ReBirth
« Reply #9 - Posted 2012-04-23 02:15:49 »

Noob? there's no way even a pro can do what I said. Oh wait, they use RPG maker. Ofc they can. Wink

Mine is finished. The title is Narrow

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #10 - Posted 2012-04-23 04:20:25 »

What? No! I made that and more in an hour. I had only been learning java for 4 months.

Thread.sleep(∞);

Color Game
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (83 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.265 seconds with 21 queries.