In this 21st episode, we'll look at horizontally moving walls. This is probably one of the more challenging aspects of platforming, simply because of the fact that, first of all, the wall is moving and our boundaries would have to be moving with the wall. We can solve this by moving the boundaries along with the wall. We'd also have to get the character to ride the wall, instead of doing it as in some games, where you have to walk to stay on the wall. Then, there are other problems with the wall. We'd have to move the character if the wall moves into the character. We then have to kill the character if the wall rams him into another wall or something. So, there's a lot at stake here. The first question: What do we use for these moving walls? Well, we can easily make a structure for our moving wall. This structure would have the location of the wall. It would also hold the speed of the moving wall in the X and Y directions. Also, we have not determined how far the wall will move, so we'd also need to specify some boundaries for our moving wall.
Public Structure MovingWall Private rect As Rectangle 'The location of the wall. Private cx As Integer 'How much the wall moves in the horizontal. Private cy As Integer 'How much the wall moves in the vertical. Private bound As Rectangle 'The wall cannot move outside of this square. Public Property Loc() As Rectangle Get Return rect End Get Set(ByVal Value As Rectangle) rect = Value End Set End Property Public Property dX() As Integer Get Return cx End Get Set(ByVal Value As Integer) cx = Value End Set End Property Public Property dY() As Integer Get Return cy End Get Set(ByVal Value As Integer) cy = Value End Set End Property Public Property Boundary() As Rectangle Get Return bound End Get Set(ByVal Value As Rectangle) bound = Value End Set End Property Public Sub New(ByVal R As Rectangle, ByVal dX As Integer, ByVal dY As Integer, ByVal Boundary As Rectangle) Me.rect = R Me.cx = dX Me.cy = dY Me.bound = Boundary End Sub Public Sub Offset(ByVal dX As Integer, ByVal dY As Integer) rect.Offset(dX, dY) End Sub End Structure |
Public Sub MoveWall() 'This subroutine coordinates the in-game functionality of the moving wall with the rest of the game. 'Just add the collision checks against rect. rect.Offset(cx, cy) 'Move the wall. If rect.Right > bound.Right OrElse rect.Left < bound.Left Then 'When the wall hits the edge of our boundary, then it must begin moving in the other direction. cx = -cx End If If rect.Bottom > bound.Bottom OrElse rect.Top < bound.Top Then 'This is for vertical movement direction switching. cy = -cy End If End Sub |
Dim MobileWalls As ArrayList 'Holds all of the moving walls in the level. Dim MobileWall As MovingWall 'A moving wall structure variable declaration |
MobileWalls = New ArrayList(20) 'Set up the arraylist. MobileWalls.Add(New MovingWall(New Rectangle(640, 80, 32, 32), 4, 0, New Rectangle(500, 0, 300, 600))) 'Add a moving wall. |
Dim R As Integer For R = 0 To MobileWalls.Count - 1 MobileWall = DirectCast(MobileWalls(R), MovingWall) 'We have to use DirectCast since arraylist contains structures and we are changing the property values. MobileWall.MoveWall() 'This does all of the movement for the wall. MobileWalls(R) = MobileWall Next |
For Each MobileWall In MobileWalls MobileWall.Offset(-ScreenLeft, -ScreenTop) GFX.FillRectangle(Brushes.DarkKhaki, MobileWall.Loc) GFX.DrawRectangle(Pens.Sienna, MobileWall.Loc) 'This will draw the wall (not the boundary). MobileWall.Offset(ScreenLeft, ScreenTop) 'We must reset the wall location. Next |