- Add a new class to the Flood Control project called "FallingPiece".
- Add
using Microsoft.Xna.Framework;
to theusing
area at the top of the class. - Update the declaration of the class to read
class FallingPiece : GamePiece
- Add the following declarations to the FallingPiece class:
public int VerticalOffset; public static int fallRate = 5;
- Add a constructor for the FallingPiece class:
public FallingPiece(string pieceType, int verticalOffset) : base(pieceType) { VerticalOffset = verticalOffset; }
- Add a method to update the piece:
public void UpdatePiece() { VerticalOffset = (int)MathHelper.Max( 0, VerticalOffset - fallRate); }
Simpler than a RotatingPiece, a FallingPiece is also a child of the GamePiece class. A falling piece has an offset (how high above its final destination it is currently located) and a falling speed (the number of pixels it will move per update).
As with a RotatingPiece, the constructor passes the pieceType
parameter to its base class constructor and uses the verticalOffset
parameter to set the VerticalOffset
member. Note that the capitalization on these two items differs. Since VerticalOffset
is declared as public and therefore capitalized by common C# convention, there is no need to use the "this" notation, since the two variables technically have different names.
Lastly, the UpdatePiece()
method subtracts fallRate
from VerticalOffset
, again using the MathHelper.Max()
method to ensure the offset does not fall below zero.