官术网_书友最值得收藏!

Time for action – falling pieces

  1. Add a new class to the Flood Control project called FallingPiece.
  2. Add the Inherits line after the class declaration as follows:
    Inherits GamePiece
  3. Add the following declarations to the FallingPiece class:
    Public VerticalOffset As Integer
    Public Shared FallRate As Integer = 5
  4. Add a constructor for the FallingPiece class:
    Public Sub New(type As String, verticalOffset As Integer)
        MyBase.New(type)
        Me.VerticalOffset = verticalOffset
    End Sub
  5. Add a method to update the piece:
    Public Sub UpdatePiece()
        VerticalOffset =
            CInt(MathHelper.Max(0, VerticalOffset - FallRate))
    End Sub

What just happened?

Simpler than a RotatingPiece, a FallingPiece is also a child of the GamePiece class. A FallingPiece 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 type parameter to its base class constructor, and uses the verticalOffset parameter to set the VerticalOffset member. Again, we use the Me. notation to differentiate the two identifiers of the same name.

Lastly, the UpdatePiece() method subtracts FallRate from VerticalOffset, again using the MathHelper.Max() method to ensure that the offset does not fall below zero.

主站蜘蛛池模板: 宜宾县| 龙门县| 辉南县| 桑植县| 丰台区| 雷山县| 广德县| 台州市| 贵南县| 崇州市| 宁陕县| 积石山| 临海市| 诸暨市| 惠来县| 榆中县| 鸡泽县| 抚远县| 马鞍山市| 界首市| 磐石市| 贵溪市| 芜湖市| 益阳市| 梨树县| 崇信县| 将乐县| 丹东市| 年辖:市辖区| 湘西| 云林县| 察哈| 竹溪县| 江阴市| 新河县| 越西县| 岳西县| 中宁县| 鹤山市| 苏尼特右旗| 曲麻莱县|