Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  Learning Class : Input Recorder  (Read 1924 times)
0 Members and 1 Guest are viewing this topic.
Offline Gudradain
« Posted 2010-12-22 00:32:46 »

A new entry in the learning class!!

Subject : What method do you use for your input in a game?

Description

I use 2 differents way to handle input in a game, the first one is the traditionnal input listener and the second one is an input that keep track of every key pressed, key released, mouse down, mouse up and the mouse position. For now, I will only explain how to do the second option.

So, what do you need to make a custom input container? :

1. First, you need to have input listener that will catch the event for you
2. Then, you send theses event to the InputRecorder.
3. Finally, you can access those datas through the Input interface.

Code

Here is a part of the code that does that.

The listener that receive input from the player and send them to the InputRecorder
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private class KeyControl implements KeyListener{

   @Override
   public void keyPressed(KeyEvent e) {
      inputRecorder.setKeyPressed(e.getKeyCode(), true);
   }

   @Override
   public void keyReleased(KeyEvent e) {
      inputRecorder.setKeyPressed(e.getKeyCode(), false);
   }

   @Override
   public void keyTyped(KeyEvent e) {
     
   }  
}


The interface that the programmer use to get the information about the player input
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public interface Input {
   
   public boolean isKeyPressed(int code);
   
   public boolean isMouseDown(int code);
   
   public int getMouseX();
   
   public int getMouseY();

}


The class the record every input from the player
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  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
public class InputRecorder implements Input{
   
   private ArrayList<Boolean> keyPressed = new ArrayList<Boolean>();
   private ArrayList<Boolean> mouseDown = new ArrayList<Boolean>();
   private int mouseX = -1;
   private int mouseY = -1;
   
        @Override
   public synchronized boolean isKeyPressed(int code){
      if(code >= keyPressed.size()){
         increaseSize(keyPressed, code+1);
      }
      return keyPressed.get(code);
   }
   
   public void setKeyPressed(int code, boolean value){
      if(code >= keyPressed.size()){
         increaseSize(keyPressed, code+1);
      }
      keyPressed.set(code, value);
   }

        @Override
   public synchronized boolean isMouseDown(int code){
      if(code >= mouseDown.size()){
         increaseSize(mouseDown, code+1);
      }
      return mouseDown.get(code);
   }
   
   public synchronized void setMouseDown(int code, boolean value){
      if(code >= mouseDown.size()){
         increaseSize(mouseDown, code+1);
      }
      mouseDown.set(code, value);
   }
   
        @Override
   public synchronized int getMouseX(){
      return mouseX;
   }
   
        @Override
   public synchronized int getMouseY(){
      return mouseY;
   }
   
   public synchronized void setMouseX(int pos){
      mouseX = pos;
   }
   
   public synchronized void setMouseY(int pos){
      mouseY = pos;
   }
   
   public synchronized void reset(){
      mouseX = -1;
      mouseY = -1;
      for(int i=0; i<keyPressed.size(); i++){
         keyPressed.set(i, false);
      }
      for(int i=0; i<mouseDown.size(); i++){
         mouseDown.set(i, false);
      }
   }
   
   
   //****************************** HELPING METHODS ********************
  private void increaseSize(ArrayList<Boolean> list, int newSize) {
      while(list.size() < newSize){
         list.add(false);
      }
   }

}


Finally, here is an example of how to use it
1  
2  
3  
if(input.isKeyPressed(KeyEvent.VK_D){
      //execute code
}


Notes

The advantage of using this kind of input is that you can put the logic that depends on input anywhere in your code. You don't have to put everything in the listener subclass or creating a lot of variable for keeping track of the changes.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-12-22 01:25:16 »

1  
2  
3  
4  
5  
   //@Override
  public void keyReleased(KeyEvent e) {
      inputRecorder.setKeyPressed(e.getKeyCode(), false);
      inputManagerRecorder.keyReleased(e);
   }


*cough* bug *cough* Wink

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Gudradain
« Reply #2 - Posted 2010-12-22 01:33:37 »

*cough* bug *cough* Wink

Ty for pointing out (always happen when I copy paste...)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fireside

Senior Newbie





« Reply #3 - Posted 2010-12-22 17:37:06 »

Thanks for this post.  It's something I need to think about.  My games tend to end up a mess because of input.  I'm not sure I completely agree with saving every input, if that's what it's doing, but I need something more thought out that's available from the main loop.
Offline cylab

JGO Ninja


Medals: 43



« Reply #4 - Posted 2010-12-22 21:12:11 »

You jave to store which keys are down to be able to poll them in the main loop and the inputrecorder does exactly that (confusing name though).

The implementation is ok,but needs syncronization.

Mathias - I Know What [you] Did Last Summer!
Offline Gudradain
« Reply #5 - Posted 2010-12-23 01:20:49 »

The implementation is ok,but needs syncronization.

I'm really not an expert on synchronization but I guess that putting every method in the InputRecorder as synchronized should do the trick right?

confusing name though

Really? Why? Suggestion
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #6 - Posted 2010-12-23 12:13:31 »

1  
2  
3  
4  
5  
private void increaseSize(ArrayList<Boolean> list, int newSize) {
      while(list.size() < newSize){
         list.add(false);
      }
   }


Why not simply replace that with:

1  
2  
3  
private void increaseSize(ArrayList<Boolean> list, int newSize) {
   list.ensureCapacity(newSize);
}


 Smiley
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-12-23 17:39:58 »

1  
2  
3  
4  
5  
private void increaseSize(ArrayList<Boolean> list, int newSize) {
      while(list.size() < newSize){
         list.add(false);
      }
   }


Why not simply replace that with:

1  
2  
3  
private void increaseSize(ArrayList<Boolean> list, int newSize) {
   list.ensureCapacity(newSize);
}


 Smiley

Because that has significantly different behavior...

ensureCapacity(cap) only makes sure the backing array's length is at least 'cap'. It has no impact on the size() of the collection.

The increaseSize() method posted here adds items to the list, which obviously has an effect on the size of the collection.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #8 - Posted 2010-12-23 17:51:48 »

1  
2  
3  
4  
5  
private void increaseSize(ArrayList<Boolean> list, int newSize) {
      while(list.size() < newSize){
         list.add(false);
      }
   }


Why not simply replace that with:

1  
2  
3  
private void increaseSize(ArrayList<Boolean> list, int newSize) {
   list.ensureCapacity(newSize);
}


 Smiley

Because that has significantly different behavior...

ensureCapacity(cap) only makes sure the backing array's length is at least 'cap'. It has no impact on the size() of the collection.

The increaseSize() method posted here adds items to the list, which obviously has an effect on the size of the collection.

Oh I see
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.

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

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

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

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

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

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

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

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

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
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!