> | | | | | | | | | | | > Leaping From and Landing On Slopes

Leaping From and Landing On Slopes

Posted on Thursday, August 23, 2012 | 2 Comments

I just applied the other day for what would be an amazing job and I am perfectly qualified for it!  I am supposed to hear back by tomorrow at the latest if they want to interview me, so if I don't get a phone call I'm going to be pretty disappointed.  I won't be surprised, even though I feel like my qualifications align more closely with this job than any other I've applied to in the past few months, because I've only had one employer call me back since February.  It's just so frustrating trying to find a job in this economy... Well anyway, on to the topic at hand -- leaping from and landing onto sloped tiles!

Ironically, implementing jumping from and landing onto sloped tiles in my most recent solution to sloped tiles is a lot easier than before.  I've still not gotten around to fixing the slope code, but as I mentioned previously this certain aspect of it works perfectly due to how it's implemented.  The code is basically identical to the old method, but what makes it work correctly is the new pixel perfect collision method in the Power Tools.

First, we've got our variables onSlope and jumpingOnSlope.  These help us to keep track of Otto's position and current action so that we can determine what code to run.  Like before, in addition to our normal jump code, we have a section that handles jumping if we're currently standing on a sloped tile.  We only want to run this code if Otto is currently on a slope (onSlope = true) and not currently jumping from a slope (!jumpingOnSlope).  In addition, we won't run the code if stopJump or holdingSpace are active.

(FlxG.keys.SPACE && onSlope && !jumpingOnSlope && !stopJump && !holdingSpace)
If we run the slope jump code, we need to be sure to first set the player's maxVelocity.y to the default.  Then, do the normal jump routine with the addition of setting jumpingOnSlope to true.  I probably should rename that variable to jumpingFromSlope.  It always helps to be as specific as possible with your variable names, but you don't want them to be too long or your code will just get cluttered.
player.maxVelocity.y = 454;
player.velocity.y = -player.maxVelocity.y / 2;
jumpingOnSlope = true;
holdingSpace = true;
isJumping = true;
onSlope = false;
Already, the player should be able to jump from a sloped tile.  However, we need to add code for a few other situations that could occur.  You always have to check your boundaries when changing a variable and make sure that it gets changed back to the appropriate value whenever necessary.  Let's start with jumpingOnSlope.

We should set jumpingOnSlope to false whenever the player has stopped the process of jumping from a slope.  So, we should set it in the chunk of code that runs when the player lets go of space mid-air and was still moving upward.  We also want to set it to false any time the player's y velocity is greater than zero, since that indicates that he is falling.  Also, as always, set it to false any time the slope code itself is skipped over and we run our normal collision code.
 if (!FlxG.keys.SPACE && !player.isTouching(FlxObject.FLOOR) && !stopJump && player.velocity.y < 0) {
  player.velocity.y = 0;
  stopJump = true;
  jumpingOnSlope = false;
  isFalling = true;
if (player.velocity.y > 0) {
  jumpingOnSlope = false;
  isFalling = true;
As for onSlope, it's only set to false when we run either the normal jump code or the slope jump code or if we've skipped over the slope code.  Otherwise, we set it to true any time the parser runs the slope code.

This is all I needed to do in order to get jumping from and landing on slopes working with this current method.  It works because once the player's collision point for slopes overlaps a sloped tile, we simply run the pixel perfect collision check and adjust the player's position accordingly.  Since the collision point usually won't immediately intersect the actual slope, we let "gravity" do its job until it does intersect.  If it does intersect immediately, it doesn't matter since we already have code in place to handle that situation as well.

Now all I need to do is figure out why the positioning of my sloped tile sprite doesn't work correctly and then I can put all this sloped tile business behind me!  I am hoping to find the time and motivation to do so sometime soon, but my main priority at the moment is finding a job.  As always, thanks for reading!

See you later!


Powered by Blogger.