Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Chat in Slick  (Read 1218 times)
0 Members and 1 Guest are viewing this topic.
Offline Reck

Senior Newbie





« Posted 2012-12-04 09:44:18 »

I'm going to add chat functionality to a game i'm making, and i would probably be able to do it in a not-so-great way.

How would one register each keypress and write it to the chat, without having to check each letter?
i.e:
1  
2  
3  
        if (input.isKeyPressed(input.KEY_A)) {
            this.text += "a";
        }
Offline Phased
« Reply #1 - Posted 2012-12-04 09:56:54 »

For my first one attempt at a chat box, I created a ChatBox class, when enter is pressed, the input is disabled in the game, and only will only work in the ChatBox class.

using org.lwjgl.input.Keyboard class I have a method
1  
2  
3  
   public char getChatKey(){
      return Keyboard.getEventCharacter();
   }


which is inside my InputHandler class.

in my ChatBox class, I have an array of char's (because i get random chars in the way if I don't), I loop through the allowed keys if the key matches one of the chars in the array, it is then applied to the message string.


1  
2  
3  
4  
5  
6  
7  
char key = input.getChatKey();

         for (int i = 0; i < allowedKeys.length; i++) {
            if (key == allowedKeys[i]) {
               message += key;
            }
         }

You may also want to put a delay in their so 5000 of the same keys don't instantly get put in. or some other way of checking to make sure unwanted amounts of the same key is entered.

EDIT: added more
Offline Reck

Senior Newbie





« Reply #2 - Posted 2012-12-04 10:09:31 »

For my first one attempt at a chat box, I created a ChatBox class, when enter is pressed, the input is disabled in the game, and only will only work in the ChatBox class.

using org.lwjgl.input.Keyboard class I have a method
1  
2  
3  
   public char getChatKey(){
      return Keyboard.getEventCharacter();
   }


which is inside my InputHandler class.

in my ChatBox class, I have an array of char's (because i get random chars in the way if I don't), I loop through the allowed keys if the key matches one of the chars in the array, it is then applied to the message string.

Thank you for the answer! :-) The chat function is however a core functionality of my game, and should be available at all times without deactivating other movement (only moves by mouse).

Where did you put that method?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Phased
« Reply #3 - Posted 2012-12-04 10:13:41 »

Well, It is inside my InputHandler Class, I'm not to sure about slicks use of input, but the getChatKey() method is inside my input handler class.

If you want to be still be able to talk, just ignore disabling the input.

basically you just went to get the char value of what key is pressed. this will save you having to do checks for 26 letters, 26 capital letters, 10 numbers and what 20 symbols?
Offline Reck

Senior Newbie





« Reply #4 - Posted 2012-12-04 12:08:25 »

I can't figure how to make use of this.... :-(
Offline sproingie

JGO Kernel


Medals: 202



« Reply #5 - Posted 2012-12-04 16:39:17 »

Perhaps you'd be better off using a GUI library of some sort like TWL or LibGDX+Stage2D.  Both of them have steep learning curves, but it beats writing a GUI from scratch.
Offline Cero
« Reply #6 - Posted 2012-12-04 16:49:18 »

slick has actually a button and a textfield by itself - nothing else, but they do work

so if you only need those 2, and you want quick results, check em out

TWL and stage2d were mentioned, although the latter is for libgdx
there is also NiftyGui

I just wrote my own... but it really depends on what you need

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.

Elsealabs (11 views)
2014-12-28 10:39:27

CopyableCougar4 (17 views)
2014-12-28 02:10:29

BurntPizza (21 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (57 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (94 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!