XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

keyboardState.getPressedKeys() problem

Last post 11/14/2009 2:18 PM by jadephoenix1988. 3 replies.
  • 11/14/2009 4:38 AM

    keyboardState.getPressedKeys() problem

    Hi guys,

    I want the user to be able to change the keys for moving and other actions.

    It's work... a little bit :P. If I just try it once, the control is changed. But if I try it twice, I get an IndexOutOfBounds Error.

    I've programmed nearly 12h today, so I'm a little tired and maybe don't find a simple bug.

    Here the Code Fragments, where the fault should be:

    1. in this code the compiler declares the Error:

            public void UpdateKeys() 
            { 
                players[0].jump[0] = keysPlayer1.jump[0]; 
                players[0].right[0] = keysPlayer1.right[0]; 
                players[0].left[0] = keysPlayer1.left[0]; 
                players[0].down[0] = keysPlayer1.down[0]; 
                players[1].jump[0] = keysPlayer2.jump[0]; 
                players[1].right[0] = keysPlayer2.right[0]; 
                players[1].left[0] = keysPlayer2.left[0]; 
                players[1].down[0] = keysPlayer2.down[0];   
            } 


    And this is the Code, where the fault should be:
        public class KeyBinding 
        { 
            KeyboardState keyboardstate; 
            public Keys[] jump = new Keys[3]; 
            public Keys[] right = new Keys[3]; 
            public Keys[] left = new Keys[3]; 
            public Keys[] down = new Keys[3]; 
     
            public bool isClickedPlayer1Jump = false
            public bool isClickedPlayer1Right = false
            public bool isClickedPlayer1Left = false
            public bool isClickedPlayer1Down = false
            public bool isClickedPlayer2Jump = false
            public bool isClickedPlayer2Right = false
            public bool isClickedPlayer2Left = false
            public bool isClickedPlayer2Down = false
     
            public KeyBinding() 
            { 
                jump[0] = Keys.Up; 
                right[0] = Keys.Right; 
                left[0] = Keys.Left; 
                down[0] = Keys.Down; 
            } 
     
            public void setupKey(int action) 
            { 
     
                    KeyboardState previousKeyboardstate = keyboardstate; 
                    keyboardstate = Keyboard.GetState(); 
                    if (previousKeyboardstate != keyboardstate) 
                    { 
                        switch (action) 
                        { 
                            case 1: 
                                jump = keyboardstate.GetPressedKeys(); 
                                isClickedPlayer1Jump = false
                                isClickedPlayer2Jump = false
                                break
                            case 2: 
                                right = keyboardstate.GetPressedKeys(); 
                                isClickedPlayer1Right = false
                                isClickedPlayer2Right = false
                                break
                            case 3: 
                                left = keyboardstate.GetPressedKeys(); 
                                isClickedPlayer1Left = false
                                isClickedPlayer2Left = false
                                break
                            case 4: 
                                down = keyboardstate.GetPressedKeys(); 
                                isClickedPlayer1Down = false
                                isClickedPlayer2Down = false
                                break
                        } 
                    } 
     
            } 
        } 

    Thank you very much for helping! :)
  • 11/14/2009 5:03 AM In reply to

    Re: keyboardState.getPressedKeys() problem

    If no keys are pressed, GetPressedKeys() should be returning a 0-length array. You are repeatedly trying to access index 0, which requires an array of length 1 or greater.
  • 11/14/2009 5:06 AM In reply to

    Re: keyboardState.getPressedKeys() problem

    I dont see enough information to be honest where are u setting data into keyPlayers.
    Also possible that because your setting things equal tosomething in a switch, that the problem is that in your update keys method while the first case triggered and set your varaibles to data, the very next case is still set on null.  I see the keybinding function but I dont see where you set each and every variable by callign keybinding with it.
  • 11/14/2009 2:18 PM In reply to

    Re: keyboardState.getPressedKeys() problem

    Yeah, you were right Bshields!!!

    Thank you very much..damn.. thats cool :D!
Page 1 of 1 (4 items) Previous Next