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