- 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;
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:
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:
Vector2f bulletPos = new Vector2f(Mathf.Random(PADDING, Graphics.Width - PADDING), Graphics.Height + PADDING);
Bullet bullet = Add(new Bullet(true), 1);
bullet.Position = bulletPos;
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.
Prev Page :: Back to Index :: Next Page