Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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. */
- // From my input class:
- public Input()
- {
- // Get input state
- CurrentKeyboardState = new KeyboardState[MaxInputs];
- CurrentGamePadState = new GamePadState[MaxInputs];
- // Preserving last states to check for isKeyUp events
- LastKeyboardState = new KeyboardState[MaxInputs];
- LastGamePadState = new GamePadState[MaxInputs];
- }
- /// <summary>
- /// Helper for checking if a button was newly pressed during this update.
- /// The controllingPlayer parameter specifies which player to read input for.
- /// If this is null, it will accept input from any player. When a button press
- /// is detected, the output playerIndex reports which player pressed it.
- /// </summary>
- public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
- {
- if (controllingPlayer.HasValue)
- {
- // Read input from the specified player.
- playerIndex = controllingPlayer.Value;
- var i = (int)playerIndex;
- return CurrentGamePadState[i].IsButtonDown(button) && LastGamePadState[i].IsButtonUp(button);
- }
- else
- {
- // Accept input from any player.
- return IsNewButtonPress(button, PlayerIndex.One, out playerIndex)
- || IsNewButtonPress(button, PlayerIndex.Two, out playerIndex)
- || IsNewButtonPress(button, PlayerIndex.Three, out playerIndex)
- || IsNewButtonPress(button, PlayerIndex.Four, out playerIndex);
- }
- }
- /// <summary>
- /// Helper for checking if a key was newly pressed during this update. The
- /// controllingPlayer parameter specifies which player to read input for.
- /// If this is null, it will accept input from any player. When a keypress
- /// is detected, the output playerIndex reports which player pressed it.
- /// </summary>
- public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
- {
- if (controllingPlayer.HasValue)
- {
- // Read input from the specified player.
- playerIndex = controllingPlayer.Value;
- var i = (int)playerIndex;
- return CurrentKeyboardState[i].IsKeyDown(key) && LastKeyboardState[i].IsKeyUp(key);
- }
- else
- {
- // Accept input from any player.
- return IsNewKeyPress(key, PlayerIndex.One, out playerIndex)
- || IsNewKeyPress(key, PlayerIndex.Two, out playerIndex)
- || IsNewKeyPress(key, PlayerIndex.Three, out playerIndex)
- || IsNewKeyPress(key, PlayerIndex.Four, out playerIndex);
- }
- }
- // Tells the left paddle to move down
- public bool LeftDown(PlayerIndex? controllingPlayer)
- {
- PlayerIndex playerIndex;
- return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex)
- || IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex)
- || IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
- }
- // Tells the left paddle to move up
- public bool LeftUp(PlayerIndex? controllingPlayer)
- {
- PlayerIndex playerIndex;
- return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex)
- || IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex)
- || IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
- }
- //////////////////////////////////////////////////////////////////////////////////////////
- // And now from my Gameplay class:
- public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
- ........
- input.Update();
- // Controls movement of the bats
- if (rightBat.GetType() != typeof(AIBat))
- {
- if (input.LeftDown(ControllingPlayer))
- {
- leftBat.MoveDown();
- }
- else if (input.LeftUp(ControllingPlayer))
- {
- leftBat.MoveUp();
- }
- if (input.RightDown(ControllingPlayer))
- {
- rightBat.MoveDown();
- }
- else if (input.RightUp(ControllingPlayer))
- {
- rightBat.MoveUp();
- }
- /////////////////////////////////////////////////////////
- // From my bat class:
- public void MoveUp()
- {
- SetPosition(position + new Vector2(0, -moveSpeed));
- }
- public void MoveDown()
- {
- SetPosition(position + new Vector2(0, moveSpeed));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement