Welcome back.
This episode is about dealing with walls that are moving horizontally.  In the
previous lesson, we got these walls to move, but the player cannot interact with
them, so, right now, they are just a moving part of the background.
Hopefully, that will all be overwith... because in this lesson, we are going to
get the player to stand on these walls and ride them.
First off, getting the player to land on the wall should be fairly simple: all
we do is mimic what we have done for the existing walls.
The second part is getting the player to ride the wall.  In doing this part, we'd
have to move the character every frame according to how much the wall moves.  This
could be done by having an automovement vector which indicates how much the player
will move during every frame in order to stay on the board.  The problem arises
when the wall changes direction.  So, in order to do this, we'd have to have a
reference to the actual moving board.  Since the moving boards are structures,
our reference will have to be the index of the board in the array.  If the player
is not on a moving platform, this value can be set to -1, to indicate that there
is no reference now.  So, with this number, we move the character according to the
change in the moving wall.  Fortunately, we can access this value through the
moving walls' DX and DY properties.  This value is automatically changed to
reflect how much the wall is going to move in the next frame.  So, our character
should ideally move before this change has taken place, which means we need to
move the character before we move the wall.
Now that we've got the wall and player movement synchronized, we also need to
determine how far the player should walk before he falls off the edge.  Remember
that we are currently determining where the player falls off of the end by the
LeftEnd and RightEnd integers, which are set when the player lands on something.
So, when the player lands on a moving platform, these falling values need to be
updated as well.  They can simply be changed by the same amount that the player
changes, though.
Those three criteria are the only main things that we need to implement to get our
character riding safely on the walls.
So, let's get it started.
First, we get the player to interact with the moving walls by mimicking all of our
wall collision detections in the CharacterMovement sub to support the moving wall.
Let's declare our moving wall reference:
 Dim MobileIndex As Integer 'Index of the mobile wall that the player is on.
Don't forget to set it to -1 in the form's Load event procedure. We have this in the GoLeft block:
 For Each Wall In Walls 'Check with each wall. If PlayerLoc.IntersectsWith(Wall) Then PlayerLoc.Offset(Wall.Right - PlayerLoc.Left, 0) 'Move the player to the edge of this wall. End If Next For Each MobileWall In MobileWalls 'Check with each moving wall this time. If PlayerLoc.IntersectsWith(MobileWall.Loc) Then PlayerLoc.Offset(MobileWall.Loc.Right - PlayerLoc.Left, 0) 'Move the player to the edge of this wall. End If Next
And this in the GoRight block:
 For Each Wall In Walls If PlayerLoc.IntersectsWith(Wall) Then PlayerLoc.Offset(Wall.Left - PlayerLoc.Right, 0) 'Move the player to the edge of this wall. End If Next For Each MobileWall In MobileWalls If PlayerLoc.IntersectsWith(MobileWall.Loc) Then PlayerLoc.Offset(MobileWall.Loc.Left - PlayerLoc.Right, 0) 'Move the player to the edge of this wall. End If Next
In the IsJumping block, we have this after the Wall For Each block
 For Each MobileWall In MobileWalls If PlayerLoc.IntersectsWith(MobileWall.Loc) Then If IsJumping = False Then 'If the player has landed on a platform. IsJumping = True 'Knock the player off the platform and make the player PlayerLoc.Offset(0, MobileWall.Loc.Bottom - PlayerLoc.Top + GRAVITYTH) 'Move the player to under the wall. We need GRAVITYTH so that the player climb up to the platform. PlayerVeloc = 0 'Make the player start falling. ElseIf PlayerVeloc > 0 Then 'If player is falling, then the player will land on the wall. IsJumping = False 'Stop the jump. LeftEnd = MobileWall.Loc.Left : RightEnd = MobileWall.Loc.Right 'Set the endpoints. MobileIndex = MobileWalls.IndexOf(MobileWall) 'Set the index of this mobile wall in the array. AnimCycler = 0 'Reset the cycler. PlayerLoc.Offset(0, MobileWall.Loc.Top - PlayerLoc.Bottom) 'Position the player to stand on this wall. Else 'The player has jumped up into the wall. PlayerLoc.Offset(0, MobileWall.Loc.Bottom - PlayerLoc.Top) 'Move the player to under the wall. PlayerVeloc = 0 'Make the player start falling down. End If End If Next
Notice the line in the PlayerVeloc > 0 block that sets the value of mobile index. We also need to unset this value when the player begins jumping again. So, we add this:
 MobileIndex = -1 'We are not standing on anything now, because we are jumping.
into the Keydown event where the keycode is Keys.Space, and into the CharacterMovement subroutine's GoLeft and GoRight blocks when Isjumping becomes True. Now, we should have the player able to fall onto the platform at this point, but we need the player to ride the platform. So, we go back to the one MobileWall block in the CharacterMovement sub that I skipped: the one that occurs before the IsJumping block. Here is where we update the values for the Player location and the LeftEnd/RightEnd variables. So, before we call .MoveWall, we update these values.
 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. If R = MobileIndex Then 'R will never be -1 because of our For Loop. 'We need to move the Player, the left end, and the right end. LeftEnd += MobileWall.dX RightEnd += MobileWall.dX PlayerLoc.Offset(MobileWall.dX, MobileWall.dY) End If MobileWall.MoveWall() 'This does all of the movement for the wall. MobileWalls(R) = MobileWall Next
Now, the player should be able to ride the moving walls. At this point, you can download the current state of the project.