Step 2

Moving the player up


Next, let’s create the code to make the player go up a lane.

In order for our player to go up, we need to go to the previous y coordinate in the array; however, remember if we try to go to a value that doesn’t exist we will get an error.

To avoid this happening we need to make sure we aren’t on the lower edge or our array 0 before we do any other code.

Moving the player down


Next, let’s create the code to make the player go down a lane.

Similarly, in order for our player to go up, we need to go to the next y coordinate in the array. Again, we need to make sure that we select a value that exists.

To avoid this happening we need to make sure we aren’t on the upper edge or our array 2 (an even better thing to check for is length of array – 1 why is that?)

Testing the code


Now let’s test our code to make sure that it works.

If coded correctly, our player should now be able to go in all three lanes and stop at the edges!