Lesson 48: Improving Platform Behavior
Now that you can add platforms, you will want to allow the player to jump from platform to platform. In this lesson, you will do just that; and fix some unwanted platform behaviors.
1. Add the ability to jump using when the space bar is pressed.
Declare upArrowDown as a global variable.
Set upArrowDown to true when the up arrow key is pressed; and set the upArrowDown to false when the up arrow key is released.
The player should only be able to jump when the pc is on a platform. Therefore, the code that initializes a jump should be at the same moment when a collision occurs. At this point, when the player has the up arrow key pressed, set the fallSpeed to -16 (a strong upward force).
Now, only when the pc is on a platform, you should be able to jump.
2. Add some more platforms to test out the functionality more thouroughly.
An issue should become apparent. When the pc collides with a platform as it falls, the pc should be moved directly on top of that platform. This issue should be apparent when you jump from underneath a platform, and when you run into the side of a platform.
3. Modify your code to distinguish between collisions that occur when you are falling downward and collisions that occur when you are jumping upward.
If the pc is moving upward (fallSpeed<0) when a collision occurs, you know the pc hit the platform from the bottom. When this happens, the pc is moved directly underneath the platform so it no longer is colliding with the platform; and the fallSpeed is changed from negative to positive (by multiplying it by -1). This will cause the pc to begin falling at the same speed it had previously been rising.
If the pc is moving upward (fallSpeed>0) when a collision occurs, you know the pc landed on the platform from the top; and you already have the code for this!
Note, it is useful to add the comments not only for your own reference when you look back at your code, but also for other programmers who you share your code with.
Now jumping upward into a platform should send you falling back down.
Differentiating between the up and down VERTICAL MOVEMENTcollisions solves one of the issues. However, you still need to deal with instances in which you run into a platform from the side.
4. Modify your code further to recognize side collisions that occur from HORIZONTAL MOVEMENT.
Immediately after the line of code that moves the pc to the left, loop through the platforms to detect if there is a collision. By checking for a collision immediately moving the pc to the left, you know the collision came from moving horizontally. When this type of collision occurs, you simply move the pc back to his original position. Basically, you just do not allow the horizontal motion to occur because the pc is up against the wall, literally.
Note that if( sideHit ) is equivalent to if(sideHit == true), because a Boolean variable is already true or false, which are the same values that a conditional operator (such as ==, <, and >) returns.
Of course you need to do the same thing when you move to the right.
Now you should be able to collide with platforms in any way without and strange behavior. The only issue now is that you can just run right out of the game.
5. Modify the horizontal motion so that the platforms move instead of the pc.
One approach would be to move all the platforms horizontally (instead of the pc), and then if there is a collision, move them all back. However, this would require much unneeded processing, which may eventually be a problem as more and more platforms are added.
Instead, move only the pc to determine if a collision would occur, and then move the pc back to it's original position. In this way, you can then loop through and move the platforms only if there will be no collision (sideHit is not true). Pretty clever.
6. Add some more platforms to test out your improved platform functionality.
Now, the horizontal motion should move the platforms (not the pc) while the vertical motion should still move only the pc.
Of course, you could keep the pc in one place and have the platforms move horizontally and vertically, but if that is the behavior you want, you should now be able to handle that on your own.
7. After all the platforms for the game are showing up correctly, hide all elements of the game that are not within the gameWindow by setting the overflow to hidden.
8. Since your pc will likely not be a ball, resize the pc in the init() function.
The platform functionality is now complete!