Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (540)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 2599 times)
0 Members and 1 Guest are viewing this topic.
Offline Longarmx
« Posted 2012-04-22 03: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

Offline ReBirth
« Reply #1 - Posted 2012-04-22 03: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 04: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!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2012-04-22 04: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

Offline ReBirth
« Reply #4 - Posted 2012-04-22 04: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 04: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
« Reply #6 - Posted 2012-04-22 04: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!

Offline ReBirth
« Reply #7 - Posted 2012-04-22 05: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
« Reply #8 - Posted 2012-04-22 13: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

Offline ReBirth
« Reply #9 - Posted 2012-04-23 00: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
« Reply #10 - Posted 2012-04-23 02:20:25 »

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

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.

Mr.CodeIt (23 views)
2014-12-23 03:34:11

rwatson462 (53 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (78 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (95 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (151 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!