Arcade
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Step 5: Upward Bullets Too
  • Let's make bullets fly upward from the bottom of the screen as well.
  • Add a _isMovingDownward boolean variable to the Bullet class that designates whether it moves up or down, and update CheckBounds accordingly:
void CheckBounds()
{
Vector2f gameSize = (Vector2f)Stage.Game.Graphics.Size;
Vector2f mySize = (Vector2f)_sprite.Size;
// tell our containing scene to get rid of us when we are fully out of bounds
if(_isMovingDownward && Y < -mySize.Y * 0.5f)
Stage.Remove(this);
else if(!_isMovingDownward && Y > gameSize.Y + mySize.Y * 0.5f)
Stage.Remove(this);
}
  • Add a new bool parameter to Bullet's constructor so that GameStage can choose to create either a downward-moving or upward-moving bullet:
// constructor
public Bullet(bool movingDownward)
{
_moveSpeed = Mathf.Random(30.0f, 40.0f);
_isMovingDownward = movingDownward;
}
  • Flip the bullet's sprite vertically in OnLoadGraphics if it's moving upwards:
if(!_isMovingDownward)
_sprite.FlipY = true;
  • In GameStage.cs we'll now create 2 bullets each time instead of one:
// create a bullet moving downward
Vector2f bulletPos = new Vector2f(Mathf.Random(PADDING, Graphics.Width - PADDING), Graphics.Height + PADDING);
Bullet bullet = Add(new Bullet(true), 1);
bullet.Position = bulletPos;
// create a bullet moving upward
bulletPos = new Vector2f(Mathf.Random(PADDING, Graphics.Width - PADDING), -PADDING);
bullet = Add(new Bullet(false), 1);
bullet.Position = bulletPos;

Now the player's caught in a harmless crossfire.

part5-1.gif

Prev Page :: Back to Index :: Next Page