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  
  [LWJGL] isKeyReleased() method?  (Read 1041 times)
0 Members and 1 Guest are viewing this topic.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Posted 2013-03-13 20:21:37 »

First off, sorry if this question has already been asked. I've searched the docs and this site and can't find what I'm looking for!

Right now I have a variable that I would like to increase, or decrease, every time a specific key is released I know how to use
1  
Keyboard.isKeyDown()
and all that, but that doesn't work for what I'm looking for. I need a function that will only increment the variable once every time the key is pressed. Right now, the variable increases constantly while the key is pressed. I need it to only increase by one every time the key is pressed. Is there any function out there that can do this? Or do I just have to make my own(which I can do).
Thanks!

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 77
Projects: 15


★★★★★


« Reply #1 - Posted 2013-03-13 20:41:25 »

Have a read of this tutorial.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-03-13 23:50:27 »

Have a read of this tutorial.
Much appreciated!

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

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #3 - Posted 2013-03-14 01:15:21 »

I seem to be having a little trouble... I tried following what the tutorial said, but it doesn't work! Here's the code currently:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
while (Keyboard.next()) {
         if (Keyboard.getEventKey() == Keyboard.KEY_SPACE) {
            if (Keyboard.getEventKeyState()) {
               if (brushSize <= 4) {
                  System.out.println(brushSize);
                  brushSize += 1;
               } else {
                  brushSize = 4;
               }
            }
         }
      }

Now, the brushSize is increased, but when I try to access it through another class, the variable says it never was! To explain better, I make a new instance of my input class and then access the brushSize variable through that instance by doing this:
1  
      brushSize = input.brushSize;

Which obviously should work. Now, I print out the value of brushSize in the input class and it increases. However, when I print it out in the other class, it stays at 0. Basically, the code works in the input class, but does not work when accessed outside of the class. Any help?

Offline Vladiedoo
« Reply #4 - Posted 2013-03-14 01:49:42 »

Questions:
  • When and how is your input object obtaining user input? To my knowledge (and this is how it is done in my projects) it should be called once in each iteration of the game loop.
  • Do you only have one input object or multiple? To my understanding it would be best if you only have one input object (perhaps making it static, though take my advice lightly as I am not that experienced).
  • Could you show us more source code relevant to your problem?



This isn't a solution to your problem but I would just like to point out that instead of doing
1  
brushSize = input.brushSize;
you should do
1  
brushSize = input.getBrushSize();

Some reasons from http://stackoverflow.com/questions/1568091/why-use-getters-and-setters are
  • "Encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later."
  • "Hiding the internal representation of the property while exposing a property using an alternative representation."
  • "Insulating your public interface from change - allowing the public interface to remain constant while the implementation changes without effecting existing consumers."
  • "Controlling the lifetime and memory management (disposal) semantics of the property - particularly important in non-managed memory environments (like C++ or Objective-C)."
  • "Providing a debugging interception point for when a property changes at runtime - debugging when and where a property changed to a particular value can be quite difficult without this in some languages."
  • "Improved interoperability with libraries that are designed to operate against property getter/setters - Mocking, Serialization, and WPF come to mind."
  • "Allowing inheritors to change the semantics of how the property behaves and is exposed by overriding the getter/setter methods."
  • "Allowing the getter/setter to be passed around as lambda expressions rather than values.
    Getters and setters can allow different access levels - for example the get may be public, but the set could be protected."



EDIT: I plugged your code into one of my projects and I believe you are only tracking pressed input, not released. It my not be the best design but here is how I obtain input, hopefully it is useful to you.

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  
   protected boolean[] keysDown = new boolean[256];
   protected boolean[] keysPressed = new boolean[256];
   protected boolean[] keysReleased = new boolean[256];

   protected void pollInput() {
      for (int i = 0; i < this.keysDown.length; i++) {
         this.keysDown[i] = Keyboard.isKeyDown(i);
      }
      while (Keyboard.next()) {
         if (Keyboard.getEventKeyState()) {
            for (int i = 0; i < this.keysPressed.length; i++) {
               if (Keyboard.getEventKey() == i) {
                  this.keysPressed[i] = true;
               }
            }
         } else {
            for (int i = 0; i < this.keysReleased.length; i++) {
               if (Keyboard.getEventKey() == i) {
                  this.keysPressed[i] = false;
                  this.keysReleased[i] = true;
               }
            }
         }
      }
   }
Offline cubus

Junior Member


Medals: 2



« Reply #5 - Posted 2013-03-14 02:23:30 »

without more code it's hard to say, but i guess what you want is:

1  
2  
3  
4  
if (brushSize < 4) { // < 4 (not <= 4)                          
   brushSize += 1;
    System.out.println(brushSize); // print after the change
} // no else

either you are accessing the wrong instance or you do not set the new instance's brushSize member coz of something like
1  
2  
3  
4  
5  
brushSize = brushSize;
// instead of
this.brushSize = brushSize;
// or
brushSize = other.brushSize;
in a copy constructor or clone method.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #6 - Posted 2013-03-14 20:09:57 »

without more code it's hard to say, but i guess what you want is:

1  
2  
3  
4  
if (brushSize < 4) { // < 4 (not <= 4)                          
   brushSize += 1;
    System.out.println(brushSize); // print after the change
} // no else

either you are accessing the wrong instance or you do not set the new instance's brushSize member coz of something like
1  
2  
3  
4  
5  
brushSize = brushSize;
// instead of
this.brushSize = brushSize;
// or
brushSize = other.brushSize;
in a copy constructor or clone method.

I'm sorry but none of your suggestions are even remotely helpful... First, I do need less than or equal to four, I have my reasons. Second, there can be an else. What makes you think there can't be one? I do not need to print after the change as honestly, it was for debugging purposes and it will only print out the incorrect thing once.

As to your other suggestion, really? I think I know how to use classes and objects by now. The code used to work, but when I changed it over to the new kind of input, it stopped working.

Please, don't just post without thinking...

@Vladiedoo, thank you! I'll see if that helps.

Offline cubus

Junior Member


Medals: 2



« Reply #7 - Posted 2013-03-14 21:10:05 »

Quote
First, I do need less than or equal to four

thats why you have to change the '<=' to '<' otherwise you increment brushSize to 5!

Quote
Second, there can be an else

sure, there can be an else but it makes no sense here.

Quote
I do not need to print after the change as honestly, it was for debugging purposes and it will only print out the incorrect thing once.

well, i just thought that if you print before the change, you could -and obviously you did- not see that brushSize is 5.

Quote
I think I know how to use classes and objects by now.

the code you posted does not look as if you know, sorry.

Quote
Please, don't just post without thinking...

Please, don't just post without thinking...  Wink
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #8 - Posted 2013-03-14 22:01:23 »

Really, are you really going to do this? The brushSize variable does not increase to 5, I have tested it. Seeing as LESS THAN OR EQUAL TO means that you can only increase the number up to 4. Second, you don't know what the rest of my code looks like, don't say I don't know how to code. Third, my else statement does work. I use it for other things to besides the brushSize.

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #9 - Posted 2013-03-14 22:27:47 »

I seem to be having a little trouble... I tried following what the tutorial said, but it doesn't work! Here's the code currently:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
while (Keyboard.next()) {
         if (Keyboard.getEventKey() == Keyboard.KEY_SPACE) {
            if (Keyboard.getEventKeyState()) {
               if (brushSize <= 4) {
                  System.out.println(brushSize);
                  brushSize += 1;
               } else {
                  brushSize = 4;
               }
            }
         }
      }

Now, the brushSize is increased, but when I try to access it through another class, the variable says it never was! To explain better, I make a new instance of my input class and then access the brushSize variable through that instance by doing this:
1  
      brushSize = input.brushSize;

Which obviously should work. Now, I print out the value of brushSize in the input class and it increases. However, when I print it out in the other class, it stays at 0. Basically, the code works in the input class, but does not work when accessed outside of the class. Any help?

Somewhere, you are changing that variable. Do you mind giving a better code example showing all the instances where you are using brushSize? (Paste-bin?) Variables usually stay put on a number unless another part of the code changes them, which might be the case here.

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

Junior Member


Medals: 2



« Reply #10 - Posted 2013-03-14 23:42:26 »

Really, are you really going to do this? The brushSize variable does not increase to 5, I have tested it. Seeing as LESS THAN OR EQUAL TO means that you can only increase the number up to 4. Second, you don't know what the rest of my code looks like, don't say I don't know how to code. Third, my else statement does work. I use it for other things to besides the brushSize.

ok, relax,  i try again...

your code says: if brushSize is less than or equal to 4 then add 1.
so what happens if it is equal to 4? it will be increased to 5!

and you did not see it because you print it before it is increased to 5.

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 (28 views)
2014-09-21 02:42:18

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

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

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

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

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

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

TehJavaDev (101 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!