> | | | | | | | | | | | | | > A Shorter Slope

A Shorter Slope

Posted on Wednesday, August 8, 2012 | No Comments

Sorry for the delay in posting this time around, folks!  The past several days have been very busy and, to top it off, my monitor died in front of my eyes.  I was just sitting there doing my thing and my monitor faded to black like it was going into sleep mode, except the power light went off as well.  I was really hoping it was just a bad power cable, but, alas, it was not.  To make it even more aggravating, my three-year warranty for it expired literally two days prior.  Never again will I purchase an Acer product!  It's too bad I don't know anything about monitor/television repair, because I did some research and it seems like whatever went bad might be something replaceable.  Oh well!  Maybe when I have the time and the money I can learn how to diagnose and repair things like this.

I should also mention that I am going to be posting a little less frequently for awhile, perhaps only once a week.  Once I finish up the series of posts on sloped tiles, I won't really have anything to post in regards to development on the game because I haven't yet gotten that far.  I've unfortunately not had the time lately to do any work on the game and until I get my new monitor it's going to be a hassle doing so because this spare monitor is a lot smaller and I can't have all the things on screen that I'm used to having on screen!  I do have some good things planned for after I finish up the new slope code.  For starters, I'm going to implement Otto's gun so we can finally shoot and I'd like to start putting some enemies in the game as well so we have something at which to shoot!  I also need to figure out what elements are going to be included in the GUI.  All in good time.

Anyway, back to our discussion on sloped tiles.  The 45° slopes we made last time are wonderful, yes, but as the old saying goes, "variety is the spice of life!"  This sentiment of course holds true for video games as well and this tile-based game is no exception.  It would get boring if every sloped tile in our game was the same, always moving us up or down exactly one tile's height.

45° slopes, to me, seem to be more like stairs whose sole purpose is to help you ascend or descend big distances.  22.5° slopes, on the other hand, seem to be more for aesthetics, but are still just as important.  In fact, I think 22.5° slopes are better for breaking up the monotony of a tile-based game because they make it harder to tell that the level is made from a bunch of small tiles.  Unlike 45° slopes, 22.5° slopes require two tiles for the complete slope and as a result help mask the fact that your game's levels are built out of individual tiles.

So how do we implement these shorter slopes?  Well, with the method we discussed last time, it's really simple.  In addition to checking for our normal slopes, we also need to check for shorter slopes.  Keep in mind, since the shorter slopes consist of two tiles, you'll have to check for both values.  So, for example, instead of just checking:

(tileBelow == 17 || tileUnderneath == 17)

you would need to check:

(tileBelow == 20 || tileBelow == 21 || tileUnderneath == 20 || tileUnderneath == 21)

The rest of the code is exactly the same, except when we increment or decrement the player's y value, we do so by half as much as before.  This ends up working perfectly because the game keeps track of the player's position attributes as floating-point data types, even though we're calculating the player's new y position as an integer.  Here's what my code looks like for a 22.5° slope:

Actionscript 3 Flixel 22.5 degree slope code.

Pretty simple, right?  There are some problems with this approach, however, which is why I'm working on a new version right now (and it's nearly complete!).  For starters, once you have all the possible directions and types of slopes, the code gets very bloated and there's a lot of repetition.  Also, since we've disabled normal y movement (gravity), implementing jumping and landing on slopes is a little tricky.  I'll show you how I went about enabling jumping from slopes with this approach, but I gave up on landing/falling on slopes because the code was getting too messy -- it's much, much easier in my new version!

Jumping while on a slope is different than jumping normally since we've disabled gravity for the time being.  Instead of running our normal jump code, we need a slightly modified version that only runs when the player is on a slope.  This is where our onSlope variable comes into play.  Directly underneath the normal jump code is where we'll add our "jumping while on a slope" code.  I've also added a new variable called jumpingOnSlope that simply keeps track of whether or not the player just jumped from a slope.

The main differences between our normal jump code and the slope jump code are small.  First, the conditional is modified slightly so that instead of checking whether or not the player is on the ground, we check to see if he's on a slope and if he's not currently jumping from a slope.  Mine ended up looking like this:

(FlxG.keys.SPACE && onSlope && !jumpingOnSlope && !stopJump && !holdingSpace)

Since gravity is disabled, we need to re-enable it at this point.  Actually, it technically wasn't disabled, but since we changed the player's maximum y velocity to 0, gravity doesn't have an effect.  The first line of code after the conditional should reset the player's maximum y velocity to whatever the default is (or if the player has gotten an item that increases it, whatever that value is).  Mine looks like this: player.maxVelocity.y = 454;

We also want to make sure to set jumpingOnSlope to true now that we've determined the player is jumping while on a slope.  In addition, we need to check for situations in which this variable should be set to false, such as when the player is in mid-air and let go of the space bar or any time the player's y velocity is greater than zero.  We've already got code for the mid-air situation, so add the line of code there.  We need to add another conditional in this area that checks if the player's y velocity is greater than zero and, if so, we'll set jumpingOnSlope = false;

If you recall, last time I mentioned we'd need to modify the conditional that checks whether or not we run slope code.  All we need to do is check to make sure the player is currently NOT jumping on a slope.  The modified conditional should look something like this: ((tileBelow > 15 || tileUnderneath > 15) && !jumpingOnSlope).

You should be able to jump from slopes now.  However, I just noticed that I've also got two variables I haven't yet mentioned and I'm not what exactly I was using them for (perhaps to help with falling/landing on slopes; I'll mention them anyway).  They are isJumping and isFalling.  I've added a conditional directly underneath the first slope code conditional that looks like this: (!isJumping && !isFalling).  So basically, we only run this portion of the slope code if we're not currently jumping or falling.  Both variables initially are false and are set to true or false in different areas of the jump code.

Any time the normal or jump slope code is run, isJumping is set to true.  Any time the player is touching the floor (player.isTouching(FlxObject.FLOOR)) or the player's y velocity is 0, we set it AND isFalling to false.  isFalling is only set to true in the section of code that checks if the player let go of space bar in mid-air and in the new section we added that checks if the player's y velocity is greater than zero.

As you can see, things are getting very convoluted with this approach and I'm having trouble remembering just exactly what certain variables are used for.  Thankfully, my new approach is a lot better and I'll start discussing it next time I post.  The new approach takes advantage of an awesome free library called Flixel Power Tools by Photon Storm.  The Power Tools have a method for testing pixel-perfect collisions and we'll be taking advantage of that.  I'll show you how next time!

Anyway, I hope you've been able to garner some knowledge from these past few posts about sloped tiles in a tile-based game.  As always, feel free to leave comments if you have questions or just want to say hi!  I'll leave you this time with a link to the version of my game that contains 22.5° slopes and jumping from slopes.  You can find it here.

Thanks for reading!

Leave a Reply

Powered by Blogger.