SHOW:
|
|
- or go back to the newest paste.
1 | /* My paddles no longer continue to move after holding down the left thumbstick or dpad down. I think it is because I'm not checking for a LastKeyPressed anymore now that I've begun to use a new input class. */ | |
2 | ||
3 | // From my input class: | |
4 | ||
5 | public Input() | |
6 | { | |
7 | // Get input state | |
8 | CurrentKeyboardState = new KeyboardState[MaxInputs]; | |
9 | CurrentGamePadState = new GamePadState[MaxInputs]; | |
10 | ||
11 | // Preserving last states to check for isKeyUp events | |
12 | LastKeyboardState = new KeyboardState[MaxInputs]; | |
13 | LastGamePadState = new GamePadState[MaxInputs]; | |
14 | } | |
15 | ||
16 | /// <summary> | |
17 | /// Helper for checking if a button was newly pressed during this update. | |
18 | /// The controllingPlayer parameter specifies which player to read input for. | |
19 | /// If this is null, it will accept input from any player. When a button press | |
20 | /// is detected, the output playerIndex reports which player pressed it. | |
21 | /// </summary> | |
22 | public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex) | |
23 | { | |
24 | if (controllingPlayer.HasValue) | |
25 | { | |
26 | // Read input from the specified player. | |
27 | playerIndex = controllingPlayer.Value; | |
28 | ||
29 | var i = (int)playerIndex; | |
30 | ||
31 | return CurrentGamePadState[i].IsButtonDown(button) && LastGamePadState[i].IsButtonUp(button); | |
32 | } | |
33 | else | |
34 | { | |
35 | // Accept input from any player. | |
36 | return IsNewButtonPress(button, PlayerIndex.One, out playerIndex) | |
37 | || IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) | |
38 | || IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) | |
39 | || IsNewButtonPress(button, PlayerIndex.Four, out playerIndex); | |
40 | } | |
41 | } | |
42 | ||
43 | /// <summary> | |
44 | /// Helper for checking if a key was newly pressed during this update. The | |
45 | /// controllingPlayer parameter specifies which player to read input for. | |
46 | /// If this is null, it will accept input from any player. When a keypress | |
47 | /// is detected, the output playerIndex reports which player pressed it. | |
48 | /// </summary> | |
49 | public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex) | |
50 | { | |
51 | if (controllingPlayer.HasValue) | |
52 | { | |
53 | // Read input from the specified player. | |
54 | playerIndex = controllingPlayer.Value; | |
55 | ||
56 | var i = (int)playerIndex; | |
57 | ||
58 | return CurrentKeyboardState[i].IsKeyDown(key) && LastKeyboardState[i].IsKeyUp(key); | |
59 | } | |
60 | else | |
61 | { | |
62 | // Accept input from any player. | |
63 | return IsNewKeyPress(key, PlayerIndex.One, out playerIndex) | |
64 | || IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) | |
65 | || IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) | |
66 | || IsNewKeyPress(key, PlayerIndex.Four, out playerIndex); | |
67 | } | |
68 | } | |
69 | ||
70 | // Tells the left paddle to move down | |
71 | public bool LeftDown(PlayerIndex? controllingPlayer) | |
72 | { | |
73 | PlayerIndex playerIndex; | |
74 | ||
75 | return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) | |
76 | || IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) | |
77 | || IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex); | |
78 | } | |
79 | ||
80 | // Tells the left paddle to move up | |
81 | public bool LeftUp(PlayerIndex? controllingPlayer) | |
82 | { | |
83 | PlayerIndex playerIndex; | |
84 | ||
85 | return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) | |
86 | || IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex) | |
87 | || IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex); | |
88 | } | |
89 | ||
90 | ////////////////////////////////////////////////////////////////////////////////////////// | |
91 | // And now from my Gameplay class: | |
92 | ||
93 | public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) | |
94 | ........ | |
95 | input.Update(); | |
96 | // Controls movement of the bats | |
97 | if (rightBat.GetType() != typeof(AIBat)) | |
98 | { | |
99 | if (input.LeftDown(ControllingPlayer)) | |
100 | { | |
101 | leftBat.MoveDown(); | |
102 | } | |
103 | else if (input.LeftUp(ControllingPlayer)) | |
104 | { | |
105 | leftBat.MoveUp(); | |
106 | } | |
107 | ||
108 | if (input.RightDown(ControllingPlayer)) | |
109 | { | |
110 | rightBat.MoveDown(); | |
111 | } | |
112 | else if (input.RightUp(ControllingPlayer)) | |
113 | { | |
114 | rightBat.MoveUp(); | |
115 | } | |
116 | ||
117 | - | //////////////////////////////////////////////////////// |
117 | + | ///////////////////////////////////////////////////////// |
118 | // From my bat class: | |
119 | ||
120 | - | public Vector2 GetPosition() |
120 | + | |
121 | { | |
122 | - | return position; |
122 | + | |
123 | } | |
124 | ||
125 | public void MoveDown() | |
126 | { | |
127 | SetPosition(position + new Vector2(0, moveSpeed)); | |
128 | } |