by
nathan stockton | 10 September 2005
Platform games are an increasing popular form of game for online gaming. This
tutorial will help you on your way to creating your own by deconstructing a very
simple game you see below:
[ the example of my game; refresh the page if you die or
fall off ]
Use your arrow keys to have your character move
and jump around. The space key fires your weapon once you have it. Since this is
more of a case study than a tutorial, I will go ahead and provide you with the
source to the above animation below:
Beginning
We will start off by drawing 2 objects on the stage and making them movie clips
(MC’s).
The first MC is your character. For now, we will draw a circle. Draw your
circle, select it and press F8. Once you circle is a movie clip double click it.
Then select all of the shape. Press Ctrl + K and align the shape horizontally
centered and bottom edge We now need to give it an instance name, for this
tutorial we will call it char.
Your next MC will be your ground. So, draw a rectangle about 20px thick
underneath your character, so when we add jumping he has something to fall on
to. Once you have created it give the instance name of ground.
Basic Movement
Now for our first bit of code, in a platform game, you will need left and right
movement. The following code also has friction in it. Friction gives the game a
more realistic feel:
Go down to the actions for your character movie clip and add this code:
- onClipEvent (load)
{
- speed =
0;
- maxmove =
15;
- }
- onClipEvent (enterFrame)
{
- if (_root.dead)
{
- this.gotoAndStop("dead");
- } else
{
- speed *=
.85;
- if (speed>0)
{
- dir =
"right";
- } else
if (speed<0)
{
- dir =
"left";
- }
- if (dir
== "right"){
- this._x
+= speed;
- _root._x
-= speed;
- }
- if (dir
== "left")
{
- this._x
+= speed;
- _root._x
-= speed;
- }
- if (Key.isDown(Key.LEFT))
{
- if (speed>-maxmove)
{
- speed--;
- }
- this.gotoAndStop("run");
- this._xscale
= -100;
- } else
if (Key.isDown(Key.RIGHT))
{
- if (speed<maxmove)
{
- speed++;
- }
- this._xscale
= 100;
- this.gotoAndStop("run");
- }
- if (speed<1
&& speed>-1
&& !attacking)
{
- speed =
0;
- this.gotoAndStop("idle");
- }
- }
- }
Gravity and Jumping
Next up we need jumping. This code will give you jumping; it would be inserted
after the other key.isDown events:
- if (speed<1
&& speed>-1
&& !attacking)
{
- speed =
0;
- this.gotoAndStop("idle");
- }
- if (Key.isDown(Key.UP)
&& !jumping)
{
- jumping =
true;
- }
- if (jumping)
{
- this.gotoAndStop("jump");
- this._y
-= jump;
- jump -= .5;
- if (jump<0)
{
- falling =
true;
- }
- if (jump<-15)
{
- jump =
-15;
- }
- }
The above code simulates jumping similar to the following image:
Onwards to the next page!
|
page 1 of
5 |
|
|