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

22May/113

Movable Platforms

I've got a few requests to do this one and I did a quick search and found that someone had already done it in the XNA forums. The post is here: http://forums.xna.com/forums/t/23694.aspx so all credit goes to him for getting it to work. I'll do a run-through on how to get it working in your game. It's pretty straight forward but I'll try to make it as easy as I can so you don't get lost. The code here is his and I'm just relaying it so I won't explain it as I go.

Here's a video showing what you should end up with.

 

First off you'll need a new class. Here's the new MovableTile class:

 

After you add that to your project, and change the namespace to match yours, we can move onto Player.cs.

At the top of HandleCollisions(...) you should see something similar to the follow code:

After that code, add this:

Underneath the "private void HandleCOllisions()" method, add this method:

 

Now onto Level.cs

Near the top add the following:

Let's add a case for our movable tiles so we can add them to our levels. In the "LoadTile(char tileType, int x, int y)" method let's add the following case:

 

We have a new method (LoadMovableTile(...)) so let's create that method below the LoadTile(...) method:

 

Since we have our own list of movable tiles that means we need to update/draw them ourselves instead of having our DrawTiles(...) method draw them. In the Update(...) method let's add the following after the UpdateEnemies(...) call:

 

Since we don't have an UpdateMovableTiles(...) method yet, let's add it below the Update(...) method:

 

Now that our movable tiles will update themselves, let's draw them now! In the Draw(...) method let's add the following call right after "DrawTiles(spriteBatch);"

 

That's it for the additions, not you'll just have to open the level you want to add movable tiles to (0.txt for example) and add 'M's wherever you want movers at. This example has them moving horizontally only and once they hit another mover or a block they'll wait for the specified amount of time and move in the other direction. If you want to change the time they wait at each end, find this line in your MovableTile class:

and change 0.1f to whatever you'd like!

I don't have any plans on modifying this to add vertical movers so that's something you'll have to tackle yourself. :)

 

Comments (3) Trackbacks (0)
  1. Most of it looks good, but when it says “In HandleCollisions(…) find this line:” I’m not sure what to do. There’s some more code in the block you included than in the starter kit, but when I add in the extra code I just get errors. Any ideas?

    • It seems a bunch of this post actually got removed when I moved the site over a while ago. I updated the post so everything should work correctly now!

  2. Thanks this really helps =)


Leave a comment

(required)

No trackbacks yet.