Lets get platforms and moving platforms in our game. Create a MovieClip in flash for the platform, making sure that the platforms 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. Heres 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.
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 players 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 players 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 players 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 players myMovPlat back to null.
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:
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.
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|
Rating: 7.44 Votes: 25
|Flash game programmer|
|Download the files used in this tutorial.|
|Download (106 kb)|
|More help? Search our boards for quick answers!|