Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 Keyboard, what's going on?  (Read 471 times)
0 Members and 1 Guest are viewing this topic.
Offline PaidGEEK

Junior Member


Medals: 2
Projects: 1



« Posted 2013-01-28 17:22:36 »

Hello!
Quick question! I have player input setup like this:
1  
2  
3  
4  
5  
boolean keyUp = Keyboard.isKeyDown(Keyboard.KEY_UP)
...
if(keyUp){
    //move code
}

And a bit later:
1  
2  
3  
4  
5  
while(Mouse.next()){
   if(Mouse.isButtonDown(0)){
      //fire button (boom boom)
  }
}

So what happens is, if player holds UP key to run and presses mouse button to fire, movement stops. In order to run again, player must press up key again, running while shooting is impossible.
Why do keys get reset that way?

Improvisational programmer.
Offline DazKins

Junior Member


Medals: 1
Projects: 1



« Reply #1 - Posted 2013-01-28 17:48:27 »

It's todo with the way how when you call Mouse.next() or Keyboard.next() it polls the event so it cannot be used again that tick.

The way i Would solve that particular issue is to create a seperate input class like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public class InputHandler {
   
   public boolean keysStream[] = new boolean[65536];
   
   public void tickInput(){
      while(Keyboard.next()){
         if(Keyboard.getEventKeyState()){
            keysStream[Keyboard.getEventKey()]=true;
         }else{
            keysStream[Keyboard.getEventKey()]=false;
         }
      }
      while(Mouse.next()){
                       //code here
               }

   }
}

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline PaidGEEK

Junior Member


Medals: 2
Projects: 1



« Reply #2 - Posted 2013-01-28 18:25:53 »

Ah I see. Gonna implement it straight away. Thanks!

Improvisational programmer.
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline Sickan

Senior Member


Medals: 7



« Reply #3 - Posted 2013-01-28 18:29:08 »

1  
new boolean[65536];

This basically allocates 64 kilobytes of memory. I can't help but feel like it's wasteful.

I'm just here to learn.
Offline PaidGEEK

Junior Member


Medals: 2
Projects: 1



« Reply #4 - Posted 2013-01-28 18:30:34 »

So I could just use the keys I need.

Improvisational programmer.
Offline DazKins

Junior Member


Medals: 1
Projects: 1



« Reply #5 - Posted 2013-01-28 18:36:55 »

Yes you can, I only put that as that will be able to get button presses for all keyboards, e.g chinese and swedish ones as the different characters have different codes

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Sickan

Senior Member


Medals: 7



« Reply #6 - Posted 2013-01-28 18:39:23 »

There's probably some great container for these kinds of things, but I have no idea.

I'm just here to learn.
Offline sproingie
« Reply #7 - Posted 2013-01-28 19:36:03 »

I think you're looking for this class.  

First person who complains about the overhead is going to get smacked.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #8 - Posted 2013-01-28 19:41:54 »

I think you're looking for this class.
I'll preemptively smack you for the broken link.

First person who complains about the overhead is going to get smacked.
Why is CPU overhead (Set<Integer> + autoboxed Integer instances) better than memory overhead (boolean[])?

Let him use his boolean[], and trade off memory for performance, or convenience for that matter.

And why do we have this discussion again? Clueless

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Sickan

Senior Member


Medals: 7



« Reply #9 - Posted 2013-01-28 19:44:42 »

I wish they would have included variations of the collections classes that are tailored towards the primitives as well. Or just not failed so badly with generics.

I'm just here to learn.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie
« Reply #10 - Posted 2013-01-28 20:58:02 »

I fixed the link.  Smack SMF for freaking out when you put the URL in quotes.  Freakin BBCode.

Got an actual performance issue with a Set, do we?  If you really need primitive collections, try Trove.  Beyond that, screw it, I too am tired of having this argument.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #11 - Posted 2013-01-29 02:45:58 »

No please do have this argument, it sounds quite educational.

*Grabs popcorn*

Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (84 views)
2013-05-17 21:29:12

alaslipknot (92 views)
2013-05-16 21:24:48

gouessej (123 views)
2013-05-16 00:53:38

gouessej (117 views)
2013-05-16 00:17:58

theagentd (127 views)
2013-05-15 15:01:13

theagentd (114 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.111 seconds with 21 queries.