First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Stephen Harris | Website: http://blog.ninjakiwi.com


Lets get platforms and moving platforms in our game. Create a MovieClip in flash for the platform, making sure that the platform’s registration point is on its top surface. Create as many types and sizes of platform you want. I went with two kinds, but you could easily get away with one type, and just resize each instance to suit the level. Be aware that if you make the platforms too thin, the collision check between it and the base point of the character might fail. Here’s the fun part! Place and resize your platforms to create a simple game level. Decide which platforms will be moving and which will be static. We'll set these actual values in code. For every platform type in the library, set its linkage properties to the Platform class, which you will create next.

Have a look at my Platform.as class. It has some properties and an update method, all of which relate to moving platforms. The Update() method should be called every frame from the PlatformGame class. All it does is reverse the direction if it has moved to one of its limits, and updates the x position based on the current speed and direction. To create a platform with vertical motion, you would need to add properties to set the upper and lower limits of movement. Have fun experimenting with different speeds and types of motion for your platforms.

Note: Static platforms don't require any more properties than the built in MovieClip class, so technically we don't need to make static platforms part of this class. I decided however to have all platforms under the one class so you could chop and change which platforms were moving and which were static without having to mess around too much with different symbol types and linkages in flash. In addition it allows us to add properties to all platforms as our game gets more complex without needing to change anything major. Ultimately it's a preference thing, do what you think will work best for you.

Next we have to set up the platforms in the PlatformGame class. I favour using the Array class for storing objects, it is powerful and easy to use. Add every platform in your level to the array, then set the boolean value movPlat to true for each platform you want to move. Set the limits of movement, the initial direction, and the speed of each platform marked as a moving one. Again you may opt to use a different way to store the data, to allow for multiple levels for instance.

Go back the CheckCollisions function and add a 'for loop' like the one in my PlatformGame.as. You need to perform a MovieClip-to-point hit test between each platform and the player’s base position. If the hit test is true, do three things. Firstly set the touchGround boolean to true, indicating that if falling, the player will stop. Secondly to make the contact clean and without penetration, set the player’s y value equal to the y value of the platform it just collided with. This is why we made the registration point of the character its base, and the platform, its upper surface. If the platform is a moving platform, set the character's myMovPlat reference variable equal to the platform we just collided with. This allows the player’s movement to be affected by the movement of the platform. Also ensure that if the collided platform isn't a moving one, you set the player’s myMovPlat back to null.

check platforms

To make this work, go back to your character class file and add a check in the Character.as Update() function to see if the myMovPlat is not null. If it is not null, modify the x value of the character by the speed and direction of that platform:

check mov plat

Finally, add an UpdateMovingPlats() function to your GameLoop() function, making sure to put it before the call to update the character so the character will stick better to the platform.

game loop

In UpdateMovingPlats() I manually call update on each moving platform. Try running the game again, fixing any obvious bugs, or referring back to my code if you get stuck. You should be able to move the character around the level, jumping from platform to platform.

There are a multitude of cool games you could make with a simple platform game engine, and I wish you the best of luck with it, have fun! View my finished swf file here.

If you found any of this helpful, or end up using some of these concepts in a game you make, let me know, I'd love to see what you create.


» Level Intermediate

Added: 2006-09-14
Rating: 7.44 Votes: 25
» Author
Flash game programmer
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.