Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Pressing vs. holding down a button  (Read 788 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2011-12-22 03:28:41 »

In my game, I am trying to make the character switch sprites after a certain time. When I simply press and release a button, it switches sprites no problem. However, when I try to hold the button, it take a second or so to start changing the sprite.

Try it out here -

http://www.mediafire.com/?5vjgsqo30y5dmo0

Sprite switching code -

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  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
public void keyPressed(KeyEvent e)
   {
      int key = e.getKeyCode();
     
      if (key == KeyEvent.VK_A)
      {
         if ((key2 != 2) && (key2 != 3) && (key2 != 4))
         {
            if (imgSwitchHoriz) player = leftRunning.getImage();
            if (!imgSwitchHoriz) player = leftStill.getImage();
            key2 = 1;
            dx = negChange;
            if (imgSwitchHoriz) imgSwitchHoriz = false;
            else if (!imgSwitchHoriz) imgSwitchHoriz = true;
         }
      }
     
      if (key == KeyEvent.VK_D)
      {
         if ((key2 != 1) && (key2 != 3) && (key2 != 4))
         {
            if (imgSwitchHoriz) player = rightRunning.getImage();
            if (!imgSwitchHoriz) player = rightStill.getImage();
            key2 = 2;
            dx = posChange;
            if (imgSwitchHoriz) imgSwitchHoriz = false;
            else if (!imgSwitchHoriz) imgSwitchHoriz = true;
         }
      }  
     
      if (key == KeyEvent.VK_W)
      {
         if ((key2 != 1) && (key2 != 2) && (key2 != 4))
         {
            if (imgSwitchVert) player = up.getImage();
            if (!imgSwitchVert) player = up2.getImage();
            key2 = 3;
            dy = negChange;
            if (imgSwitchVert) imgSwitchVert = false;
            else if (!imgSwitchVert) imgSwitchVert = true;
         }
      }
     
      if (key == KeyEvent.VK_S)
      {
         if ((key2 != 1) && (key2 != 2) && (key2 != 3))
         {
            if (imgSwitchVert) player = down.getImage();
            if (!imgSwitchVert) player = down2.getImage();
            key2 = 4;
            dy = posChange;
            if (imgSwitchVert) imgSwitchVert = false;
            else if (!imgSwitchVert) imgSwitchVert = true;
         }
      }
      if (key == KeyEvent.VK_ESCAPE) main.menuInit();
   }

Check out my website!
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-12-22 03:31:43 »

That's a normal keyboard feature where there is an input lag between the first key event and all subsequent key event.

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2011-12-22 03:34:31 »

Poop.

Is there a way around this? I guess my only example is Minicraft (https://s3.amazonaws.com/ld48/ld22/index.html) where they transition flawlessly.

p.s. yes I stole my sprite from there

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

Senior Member


Medals: 5
Projects: 1



« Reply #3 - Posted 2011-12-22 03:37:27 »

In the keyPressed section set a boolean variable that represents your key to true.  and do the opposite in the keyReleased section.  Then in your main loop check to see if the key is true or false and do accordingly.

For 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  
//In your variable declaration section
boolean keyA = false;
///etc...

//In your keyPressed event
if (key == KeyEvent.VK_A)
      {
         keyA = true;
      }

//In your keyReleased event
if (key == KeyEvent.VK_A)
{
    keyA = false;
}

//Somewhere in your main loop
if (keyA)
{
    if ((key2 != 2) && (key2 != 3) && (key2 != 4))
         {
            if (imgSwitchHoriz) player = leftRunning.getImage();
            if (!imgSwitchHoriz) player = leftStill.getImage();
            key2 = 1;
            dx = negChange;
            if (imgSwitchHoriz) imgSwitchHoriz = false;
            else if (!imgSwitchHoriz) imgSwitchHoriz = true;
         }
}
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-12-22 03:40:53 »

The only way around this is to have a boolean variable that you set to true if pressed and false if released. Then in your game loop you would apply the appropriate actions depending on the state of the variables.

EDIT: Aggh gbeebe beat me to it :S

Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #5 - Posted 2011-12-22 03:44:14 »

...and I modified it, adding code to help what we both are trying to say  Smiley
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.

BurntPizza (25 views)
2014-09-21 02:42:18

BurntPizza (16 views)
2014-09-21 01:30:30

moogie (19 views)
2014-09-21 00:26:15

UprightPath (26 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (44 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (100 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
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!