Robot Foot Games Helping you improve your games as we improve our own. :)

30May/111

Adding Ladders to PSK

Ladders seem to be pretty common for the Platform Starter Kit (PSK) so I figured I'd post my implementation of how I got it working.

First of all I want to thank whoever I got the code from on the XNA forums in the first place (I've combined snippets from a few places in the XNA forums).

This is quite a long post so I tried splitting it up into 3 areas (split up by a horizontal line) with the name of the class the changes take place in. By the end of this you should be able to climb up/down ladders, have an animation for both and have a new key to use when building your levels so you can place ladders throughout.

Here's a video showing off what it should look like after this tutorial:


 

Player.cs

First off, we'll add these to the top of our Player.cs class:

 

Now in the Update(...) method there's a block of code that looks like this:

And we want to change Update(...) to look like this:

If you try to build at this point you'll get errors for ladderUpAnimation and ladderDownAnimation because we haven't added those yet but that's not until later. You'll also get a bunch of errors because we change movement from a float to a Vector2 so we'll address those issues now.

Our GetInput(...) should now look something like this. I also added another method below called IsAlignedToLadder() so don't forget to include that. I just commented out the previous movement lines so you can see what's changed.

 

Now we need to make some changes inside of the ApplyPhysics(...) method to accommodate for more movement changes:

 

Now we move onto HandleCollisions(...). Find this if statement:

And replace it with this:

 

A little further down you'll see this else if statement:

AFTER that you'll need to ADD this one:

 

Now we'll add those missing animations. At the top our Player class add these calls:

And in LoadContent(...) add these below the existing ones. Notice how I'm using the celebrate animation for the ladder up animation and the death animation for the ladder down animation. You'll obviously want to change these for your game! I used them so I didn't have to make this post even longer by adding specific ladder up/down animations. :) If you don't have a celebrate or death animation then temporarily replace it with a animation that you do have.


 

Tile.cs

We only have 2 small additions in here. In enum TileCollision, add the following. If you have others, just increase the highest number by 1. The number after it doesn't really matter but you have to make sure it doesn't overlap another one.

Then add this line under one of the other const calls:

 

And that's it for our Tile class! Level.cs is next.


 

Level.cs

Under our GetCollision(...) method add these 2 methods:

 

Now you'll want to add this next to the other case statements in LoadTile(...).

 

And don't forget to add some 'H' characters to your level so you can try it out! I used 'BlockA0' as my ladder texture so you'll want to add your own ladder texture to the Tiles folder and replace "BlockA0" with name of your new ladder texture.

Everything should work at this point so build it and try it out!

 

Comments (1) Trackbacks (0)
  1. Thank you very helpful and interactive


Leave a comment

(required)

No trackbacks yet.