JUMPING AND BOUNDARY COLLISION

Something I was excited to see working was the code I had previously written for jumping. It works by taking note of the starting Y pos. We then calculate how much positive or negative speed to add to the Y pos each frame:

yvector = yvector + gravity;

The Y vector is our jump speed in the above code, we add gravity to it each frame to reduce it. This means after a certain point the Y vector will be come negative and this will bring the player back down again.

The following code updates the Y pos each frame by add the Y vector it, initially this is positive taking the player up, as it turns negative the Y pos starts to return to it’s original position.

ypos+=yvector;

When the Y pos returns to the starting position we then change the player state to idle and the jump is finished.

Boundary collision is fairly simple, after we have calculated all the movement we do a final check to see if we have gone beyond our set boundaries. I call a function to do this, checking both the left and right side of the screen:

if (xpos>277) {xpos=277;}

if (xpos<1) {xpos=1;}

So if the player had moved to X pos 280 on the right side of the screen, the code moves it back to 277. Doing the check after all the movement is calculated ensures we don’t get any weird jerky movement.