Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Keeping menus visible in-game after KeyReleased?

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    Norway
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Keeping menus visible in-game after KeyReleased?

    Hey guys, this is probably a noobish question, but I'm just totally mindblank right now:

    So, I've made a boolean array keys[] of size 120 (to include all keys I'm using for my Keyboard class), and each index returns true when KeyPressed and false when KeyReleased. So if I press down a key, an effect will happen (like directional movement), and if I release it'll stop the effect. But as you can imagine, this is not good for showing menus in-game. I want the menu to persist even after keyReleased and then, the next time keyPressed happens, it clears the graphics of that menu.

    What is a good, simple way to implement this? I feel like I need to do something with my keyPressed/keyReleased paradigm. E.g. separate it into two arrays, perhaps? One that includes keys that follow this system and another with keys that don't react to KeyReleased? or specify which key that won't react keyReleased happens? Is this the root of my problem?

    Thanks in advance.
    Last edited by Madolite; September 27th, 2014 at 01:35 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Keeping menus visible in-game after KeyReleased?

    Thread moved.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    Norway
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Keeping menus visible in-game after KeyReleased?

    I managed to figure it out:

    // Snipped from my Keyboard class (which implements KeyListener)
     
    private boolean keySwitch = false;
     
    public void keyPressed(KeyEvent e)
    {
    	keys[e.getKeyCode()] = true;
    	keys2[e.getKeyCode()] = true; 
    }
     
    public void keyReleased(KeyEvent e)
    {
    	keys[e.getKeyCode()] = false;
     
    	if (!keySwitch)
    	{
    		keys2[e.getKeyCode()] = true;
    		keySwitch = true;
    	}
     
    	else
    	{
    		keys2[e.getKeyCode()] = false;
    		keySwitch = false;
    	}
    }

    My mistake was that I was messing about in keyPressed(), when I needed to control keyReleased().

    keys[] are keys that turn off when key is released.
    keys2[] are keys that turn off when key is pressed a second time.

    Thanks anyways, though.
    Last edited by Madolite; October 5th, 2014 at 09:17 PM.

Similar Threads

  1. Joining two menus into one?
    By Digital Larry in forum AWT / Java Swing
    Replies: 2
    Last Post: October 24th, 2013, 10:22 PM
  2. Menus will not appear in JMenubar
    By mwardjava92 in forum AWT / Java Swing
    Replies: 8
    Last Post: March 19th, 2013, 04:02 PM
  3. What is these menus and How to build them
    By hno2005 in forum Android Development
    Replies: 1
    Last Post: December 1st, 2012, 02:48 AM
  4. Swing Drop Down Menus
    By x3rubiachica3x in forum AWT / Java Swing
    Replies: 2
    Last Post: November 5th, 2010, 04:58 PM
  5. Menu of Menus Help
    By Strivelli in forum Java Theory & Questions
    Replies: 10
    Last Post: June 5th, 2010, 02:40 PM