Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyBindings for a Game

    Ok. You guys aren't understanding the issue. I appreciate your effort, but it hasn't helped.

    I just wanted to make sure I was using KeyBindings correctly. I've already come to the conclusion that KeyBindongs are better than KeyListeners based on a few other sites I looked at and the fact that I can't get the shift key to work with KeyListeners.

    Thank you all for your help. I am no longer in need of assistance.
    Last edited by Prime624; 01-28-2014 at 01:41 AM.

  2. #22
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: KeyBindings for a Game

    Perhaps I don't understand the issue, but I have been listening. And I did my own test. Key Bindings are not working any better than the KeyListener but they did take more code to get it working. Maybe they're better for some things. But not for what I did. And I got the movement to work with the shift key. So pressing the shift key down speeds up the movement. I am moving a painted ball back and forth or up and down. I still want to help so please explain again.

    And to answer your question. yes you are using them correctly. Add the key to the input map. Then map the keyname you supplied to the ActionMap. At least, that is how I do it.

    Regards,
    Jim
    Last edited by jim829; 01-28-2014 at 01:35 AM. Reason: removed extraneous info
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: KeyBindings for a Game

    One big problem with trying to solve this problem is that there was no code posted that could be compiled and executed to show the problem. There are so many ways for code to be wrong that there is no way to guess what the problem is without code to test. A SSCCE would have helped.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyBindings for a Game

    From when I found out Key Bindings existed, I wanted to know which I should be using for an RPG. I found a few pages swing - Key bindings vs. key listeners in Java - Stack Overflow java - Comparing functionality between KeyListeners and Key Bindings - Stack Overflow java - KeyListeners vs Keybindings? - Stack Overflow that all recomend Key Bindings saying that KeyListeners are old, outdated, and low-level. Additionally, I had a problem with KeyListeners in that holding down three keys, then letting go of one and pressing another, then letting go of that then pressing the first again doesn't work. It sounds arbitrary, but in a game where I hold down left and up to move that way, and space bar to clear the path, not being able to swtich between going up left and up right is a bit of an inconvenience. I thought (based on what I got from the sites and tutorials about Key Bindings) that Key Bindings would solve this problem. On top of the fact that it was recommended anyways, I decided I would switch, but Key Bindings are so complicated to learn that it took me a while before I could muster enough patience to read a Java tutorial. By that time, I tried to implement the use of shift to run, without luck, but I didn't want to learn how if I was just gonna switch to Bindings anyway.

    Now that I've learned more about KeyBindings (and that I just now tested the multiple keys pressed scenario described above, and that using Key Bindings didn't help), I guess that maybe they aren't any better than KeyListeners. I still would prefer to use them, just because they are newer, but IDK and I don't think it matters much in my case.

    EDIT: I don't know what a SSCCE is, and I have posted most of the relevant code. Any more specific pieces you want, and I can, but the entirety of the code is in many separate class files, a custom library, and a few text files.
    Last edited by Prime624; 01-28-2014 at 02:01 AM.

  5. #25
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: KeyBindings for a Game

    Check out the SSCCE link in my signature. All you have to do is post some code that moves the object around and demonstrates the observed lag and the speed up slow down mechanism. Not the whole game.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #26
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: KeyBindings for a Game

    I can't really do an SSCCE because the program currently only works in Netbeans (probably some pathing error).

    The relevant portions of the keyPressed and keyReleased methods:
    Java Code:
    public void keyPressed(KeyEvent e) {
            if (consoleOn) {
                ...
            } else {
                if (e.getKeyChar()=='`') {
                    ...
                } else if (e.getKeyCode()==KeyEvent.VK_UP) {
                    UP_ARROW_KEY=true;
                } else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
                    DOWN_ARROW_KEY=true;
                } else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
                    LEFT_ARROW_KEY=true;
                } else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
                    RIGHT_ARROW_KEY=true;
                }  else if (e.getKeyCode()==KeyEvent.SHIFT_MASK) {//not work
                    SHIFT_KEY=true;
                } else if (e.getKeyCode()==KeyEvent.VK_W) {
                    W_KEY=true;
                } else if (e.getKeyCode()==KeyEvent.VK_A) {
                    A_KEY=true;
                } else if (e.getKeyCode()==KeyEvent.VK_S) {
                    if (!e.isControlDown()) {
                        S_KEY=true;
                    } else {
                        saveGame();
                    }
                } else if (e.getKeyCode()==KeyEvent.VK_D) {
                    D_KEY=true;
                } ... else if (e.getKeyCode()==KeyEvent.VK_SPACE) {
                    if (!t.isRunning()&&!waitText.equals("")) {
                        t.start();
                        waitText="";
                    } else {
                        SPACE_BAR=true;
                    }
                } ...
            }
        }
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode()==KeyEvent.VK_UP) {
                UP_ARROW_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
                DOWN_ARROW_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
                LEFT_ARROW_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
                RIGHT_ARROW_KEY=false;
            }  else if (e.getKeyCode()==KeyEvent.SHIFT_MASK) {//not work
                SHIFT_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_W) {
                W_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_A) {
                A_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_S) {
                S_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_D) {
                D_KEY=false;
            }else if (e.getKeyCode()==KeyEvent.VK_Q) {
                Q_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_E) {
                E_KEY=false;
            } else if (e.getKeyCode()==KeyEvent.VK_SPACE) {
                SPACE_BAR=false;
            }
        }
    The relevant portions of the actionPerformed method:
    Java Code:
    public void actionPerformed(ActionEvent e) {
            if (e.getSource()==t) {
                boolean hasMoved=false;
                if (aniFrame>10000) {
                    aniFrame=1;
                }
                try {
                    if (UP_ARROW_KEY || W_KEY) {
                        man.setDir(0);
                        if (manCanMoveHere("up")) {
                            if (!superMan) {
                                if (!SHIFT_KEY) {
                                    man.changeY(-map.getMovs()[0]);
                                } else {
                                    man.changeY(-map.getMovs()[1]);
                                }
                            } else {
                                man.changeY(-map.getMovs()[2]);
                            }
                            aniFrame++;
                            hasMoved=true;
                        } else {
                            man.roundY();
                        }
                    } else if (DOWN_ARROW_KEY || S_KEY) {
                        man.setDir(4);
                        if (manCanMoveHere("down")) {
                            if (!superMan) {
                                if (!SHIFT_KEY) {
                                    man.changeY(map.getMovs()[0]);
                                } else {
                                    man.changeY(map.getMovs()[1]);
                                }
                            } else {
                                man.changeY(map.getMovs()[2]);
                            }
                            aniFrame++;
                            hasMoved=true;
                        } else {
                            man.roundY();
                        }
                    }
                } catch (ArrayIndexOutOfBoundsException aiobe) {
                    man.roundY();
                }
                try {
                    if (RIGHT_ARROW_KEY || D_KEY) {
                        man.setDir(2);
                        if (manCanMoveHere("right")) {
                            if (!superMan) {
                                if (!SHIFT_KEY) {
                                    man.changeX(map.getMovs()[0]);
                                } else {
                                    man.changeX(map.getMovs()[1]);
                                }
                            } else {
                                man.changeX(map.getMovs()[2]);
                            }
                            aniFrame++;
                            hasMoved=true;
                        } else {
                            man.roundX();
                        }
                    } else if (LEFT_ARROW_KEY || A_KEY) {
                        man.setDir(6);
                        if (manCanMoveHere("left")) {
                            if (!superMan) {
                                if (!SHIFT_KEY) {
                                    man.changeX(-map.getMovs()[0]);
                                } else {
                                    man.changeX(-map.getMovs()[1]);
                                }
                            } else {
                                man.changeX(-map.getMovs()[2]);
                            }
                            aniFrame++;
                            hasMoved=true;
                        } else {
                            man.roundX();
                        }
                    }
                } catch (ArrayIndexOutOfBoundsException aiobe) {
                    man.roundX();
                }
                if (!hasMoved) {
                    aniFrame=0;
                }
                if (SPACE_BAR&&map.m().getSubMap(1)[man.getYInt()+man.getYDir()][man.getXInt()+man.getXDir()]==1) {
                    chopTree(man.getYInt()+man.getYDir(),man.getXInt()+man.getXDir());
                } else if (SPACE_BAR&&map.m().getSubMap(1)[man.getYInt()+man.getYDir()][man.getXInt()+man.getXDir()]==9) {
                    pickFruit(man.getYInt()+man.getYDir(),man.getXInt()+man.getXDir());
                }
               ...
                repaint();
            }
        }
    And the relevant portion of the paintComponent:
    Java Code:
    public void paintComponent(Graphics g) {
            ...
            g2.drawImage(man.pic(aniFrame),Basic.round(c.tileWidth()*man.getX()-c.leftCut()),Basic.round(c.tileHeight()*man.getY()-c.topCut()),c.tileWidth(),c.tileHeight(),this);
            ...
        }
    Last edited by Prime624; 01-28-2014 at 05:08 AM. Reason: Removed excess code

Page 2 of 2 FirstFirst 12

Similar Threads

  1. SWING keybindings
    By JavaJimme in forum AWT / Swing
    Replies: 6
    Last Post: 11-26-2013, 09:03 AM
  2. Unresponsive keybindings
    By Insomniac.. in forum AWT / Swing
    Replies: 5
    Last Post: 07-21-2013, 06:50 PM
  3. keybindings
    By jsobel in forum New To Java
    Replies: 8
    Last Post: 04-03-2012, 04:41 PM
  4. KeyBindings Empty Key?
    By Unome in forum Java Applets
    Replies: 1
    Last Post: 10-24-2008, 07:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •