How to make a flash game with moving platforms
This article assumes you have basic flash and actionscript experience. If you want to create your own platform game with moving platforms, I recommend downloading my source files and reading them in conjunction with the article.
Introduction - what is this article about?
I will take you through the creation of a simple platform game engine with a character that can move and jump on platforms, including moving platforms, without sticking, sliding or other common problems. The code used in the examples in this article are free for anyone to use in their own projects. I suggest creating your own version of what I have made as you go along. If you get stuck, you can always check exactly how I did it by looking at the source files.
Basic movement and ground collision
The first thing we need is a character to move around. This article doesn't cover character animation states so for the sake of simplicity I created a blue circle for the character object. You can make the character any shape or size you want, but for this tutorial make sure the registration point is where the character would make contact with a surface underneath it. For the ball character, this is the bottom edge.
Set the registration point of the character and the platform
The object you use for the character should be a MovieClip. Make sure you have one instance of the MovieClip on the stage, and name it 'player'. Next we need to link it to the code we are about to write. Edit the objects linkage properties and put 'Character' for the actionscript class name. We'll create that class now.
View the Character.as file here, or if you downloaded the source at the start of this article, open the file in your favourite editor (I recommend SEPY its powerful and free). Give it a quick read and come back. Notice that its a pretty simple class, as most of the real meat of the code happens in the game class, which we'll get to shortly. The Character class has a few properties and an Update() method.
View the PlatformGame.as file here. The PlatformGame object has properties which are references, flags and constants. The references are variables that point to objects on the stage, the flags are states that tell the player how to move, and the constants determine the speeds of the player and the moving platforms. Ignore the moving platform stuff until later. For now let's get basic character movement working.
Set up the references to the time line and the player object on the stage. Then create a game loop function and have it called every frame. I do this by using 'Delegate' to call a GameLoop() function whenever onEnterFrame event occurs on the main time line.
Create a game loop function like mine. This needs to resolve input, check collisions, update platforms, and update the player, in that order. Ignore the platform update function call for now.
Next create the ResolveInput() function. This will be the function that controls the players movement. The player will have 3 ways to move: left, right and jump. My preferred method of dealing with input for games is to use state flags. These work very well and I've found them to be more reliable than just using events, but feel free to do it which ever way works best for you. The way it works is that every frame the code checks to see if movement keys are currently being pressed. If they are, the state variable for that movement is set to true. If they arent, it is set to false. This handles multiple key presses nicely, which is important in platform games. Replicate or create an equivalent of my ResolveInput() function in PlatformGame.as.
Next, write the CheckCollisions() function, leaving out the for loops for now. The only code should be the y value check on the character. This sets a y value as the ground location for the game, 398 in my example. The update function in the Character.as will accelerate the player downward every frame unless the variable onGround is set to true.
If you wrote your game code as a class like me you'll need to instantiate it. In frame one of the main time line of your .fla file, write this:
This immediately creates the object and calls the constructor function for it, which sets the game going. If you haven't already, run your program and clean up any errors it might find. If all goes well, you should be able to control your little character by pressing arrow keys left and right, and make it jump up by pressing up. If this isn't the case, check your code against mine and see what might be causing the problem.
|» Level Intermediate|
Rating: 8 Votes: 23
|Flash game programmer|
|Download the files used in this tutorial.|
|Download (106 kb)|
|More help? Search our boards for quick answers!|