First time here? First time here? Newsletter Signup Newsletter
651326 Members and growing! | Flash Jobs
Search tutorials
Author: Stephen Harris | Website: http://blog.ninjakiwi.com
Moving platforms

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.

Let's begin!

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 reg point of the player

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 – it’s powerful and free). Give it a quick read and come back. Notice that it’s 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.

Note: If you are experienced in actionscript you may have your own preferences about where to put your code. My personal preference is to contain the 'meat' of the code off the timeline in ‘.as’ files where practical. Others prefer to put the code on the timeline inside MovieClips. Either way is fine, just do what you are most comfortable with. My code examples use classes defined in .as files, so if you are less experienced writing actionscript I recommend following along with how I do it.

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.

check collisions

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.

mainTimeline

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 player’s 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 aren’t, 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:

init game

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

Added: 2006-09-14
Rating: 7.71 Votes: 24
(10 being the highest)
» Author
Flash game programmer
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: doc_chin7
» Description:

The flash's horizontal road drive was done in CS4 and was upgraded to CS6. The car responds to right/ left key and flash will auto-generate the movement of roads from left-right or right-left based on random number. Have fun driving in this virtual environment and avoid other cars... :) If you are creative, a road driving game can be created from it.

Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
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: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.