Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (429)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (467)
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  
  Key Buffer!  (Read 2134 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2010-09-16 07:57:45 »

ever wanted to have time-based commands in a game? Like if you were to hit 236A you'd throw a fireball if they were input fast enough and no other keys were between them? Well this is for you than!

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  
/*Created by William Starkovich*/
public class KeyBuf {
   int key[];
   int length = 10;
   int ticks = 0;
   int maxTicks = 15;
   int num = 0;
   
   public KeyBuf(int l, int mt){
      length = l;
      maxTicks = mt;
   }
   
   public void init(){
      key = new int[length];
      for(int i = 0; i < length; i++){
         key[i] = -1;
         
      }
   }
   
   public void update(){
      if(ticks >= maxTicks){
         for(int i = 0; i < length; i++){
            key[i] = -1;
           
         }
         
         num = 0;
         ticks = 0;
      }
     
      ticks++;
     
   }
   
   public void keyPressed(int keyNum){
      key[num] = keyNum;
      num++;
      ticks = 0;
      if(num >= length){
         for(int i = 1; i < length; i++){
            key[i-1] = key[i];
           
         }
         
         num--;
      }
   }
}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #1 - Posted 2010-09-16 08:47:01 »

Why are you storing the length? Why not just use the length field?

Also, I tend to just store a list of the buttons that are pressed, and add to the list when a button is pressed and remove from the list when the button is released. It depends on what system you're using to get keyboard input, though. If you're using KeyEvent to do it then doing as you are is pretty hokey. If you're just directly polling keyboard states using something like JInput then I can see the advantage to your method, but still you can poll the events one by one...

This honestly isn't the best way of doing things in my opinion. If for some reason you try to update something after calling update() on KeyBuf you're going to end up with incorrect states. Not to mention you're iterating over an array every timestep, which isn't a massive deal but is still wasted CPU time.

And on another note, why abbreviate your class name? I guess you've been too recently working with C?

See my work:
OTC Software
Offline Nate

JGO Kernel


Medals: 129
Projects: 3
Exp: 14 years


Esoteric Software


« Reply #2 - Posted 2010-09-16 11:29:05 »

I guess you've been too recently working with C?
int key[]; Tongue

You probably want your class time based rather than based on ticks, but maybe not. It would be better to use a circular buffer rather than shifting all the keys. init could be in the constructor. You will forget to call it. Tongue You could use zero for "no key" and get rid of the loop in init. The class has no external way to get at the key array. You can use key.length rather than a separate field. You shouldn't use pithy field and parameter names, or abbreviate your class name. The "l" constructor parameter or the "keyNum" keyPressed parameter will crash if negative.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CyanPrime
« Reply #3 - Posted 2010-09-16 13:18:19 »

@Eli I got a feeling I didn't explain what this really is enough. When I said 236A I didn't mean (2 + 3 + 6 + the A Key) I meant QFC + Weak Punch, or D -> DF -> F -> A. Umm Think of a Hadoken. You don't hit down (2) + down-forward (3) + forward(6) +Weak Punch (A) you hit 2 than 3 than 6 than A, so if I removed a key every time it was released I would end up with only A on my buffer when I got to the last part of the special attack and it wouldn't go off, or would go off any time I hit A depending on how I code it.

You guys were right. I can still have a user-length selected array length without a separate length variable.

@Nate "It would be better to use a circular buffer rather than shifting all the keys." I don't quite understand this. what do you mean "circular buffer?"

@Nate "You could use zero for "no key" and get rid of the loop in init." I'm a little paranoid about Null stuff, so that's why I set the value myself.


updated src:
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  
/*Created by William Starkovich*/
public class KeyBuf {
   int key[];
   int ticks = 0;
   int maxTicks = 15;
   int num = 0;
   
   public KeyBuf(int l, int mt){
      if(l <= 0)
         l = 1;

      maxTicks = mt;
     
      key = new int[l];
      for(int i = 0; i < key.length; i++){
         key[i] = -1;
         
      }
   }

   public void update(){
      if(ticks >= maxTicks){
         for(int i = 0; i < key.length; i++){
            key[i] = -1;
           
         }
         
         num = 0;
         ticks = 0;
      }
     
      ticks++;
     
   }
   
   public void keyPressed(int keyNum){
      key[num] = keyNum;
      num++;
      ticks = 0;
      if(num >= key.length){
         for(int i = 1; i < key.length; i++){
            key[i-1] = key[i];
           
         }
         
         num--;
      }
   }
}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #4 - Posted 2010-09-16 17:30:15 »

@Eli I got a feeling I didn't explain what this really is enough. When I said 236A I didn't mean (2 + 3 + 6 + the A Key) I meant QFC + Weak Punch, or D -> DF -> F -> A. Umm Think of a Hadoken.
Ah, I see. In that case if you're looking purely for combinations then I guess that's a good approach. But I agree with Nate that as long as you're holding ints you might as well just place each key in its location in the array (for a very fast lookup) and then increment the value at that location to represent how long it has been since it was pressed.

See my work:
OTC Software
Offline Wildern

Junior Member





« Reply #5 - Posted 2010-09-17 01:49:11 »

@Nate "It would be better to use a circular buffer rather than shifting all the keys." I don't quite understand this. what do you mean "circular buffer?"

1  
2  
3  
4  
5  
6  
7  
8  
public void keyPressed(int keyNum){
      key[num] = keyNum;
      num++;
      ticks = 0;
      if(num >= key.length){
                    num = 0;
      }
}


Your buffer would start at num and circle around the end of the array back to num-1.
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.

xsi3rr4x (81 views)
2014-04-15 18:08:23

BurntPizza (73 views)
2014-04-15 03:46:01

UprightPath (84 views)
2014-04-14 17:39:50

UprightPath (67 views)
2014-04-14 17:35:47

Porlus (84 views)
2014-04-14 15:48:38

tom_mai78101 (107 views)
2014-04-10 04:04:31

BurntPizza (167 views)
2014-04-08 23:06:04

tom_mai78101 (263 views)
2014-04-05 13:34:39

trollwarrior1 (214 views)
2014-04-04 12:06:45

CJLetsGame (223 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!