Game Development Reference
In-Depth Information
The player can now fire bullets, but there is no code to detect input and call the
Fire method. All the input for the player is handled in the Level class in
the Update method. It's a bit messy to have the input code in the root of the
Update method, so I've extracted a new function called UpdateInput ; this
helps keep things a bit tidier.
public void Update(double elapsedTime)
{
UpdateCollisions();
_bulletManager.Update(elapsedTime);
_background.Update((float)elapsedTime);
_backgroundLayer.Update((float)elapsedTime);
_enemyList.ForEach(x = > x.Update(elapsedTime));
// Input code has been moved into this method
UpdateInput(elapsedTime);
}
private void UpdateInput(double elapsedTime)
{
if (_input.Keyboard.IsKeyPressed(Keys.Space) || _input.Controller.
ButtonA.Pressed)
{
_playerCharacter.Fire();
}
// Pre-existing input code omitted.
Take all the input code out of the Update loop and put it at the end of the new
UpdateInput method. This UpdateInput method is then called from the
Update method. Some new code has also been added to handle the player firing.
If the space bar on the keyboard or the A button on the gamepad is pressed, then
the player fires a bullet. Run the program and try the spaceship's new firing
abilities.
The new bullets can be seen in Figure 10.8. The bullets are created every time the
player hits the fire button. For gameplay reasons, it's probably best to slow this
down a little and give the spaceship a small recovery time between shots. Modify
the PlayerCharacter class as follows.
 
Search WWH ::




Custom Search