by
nathan stockton | 10 September 2005You would
also need to set jump to 0 and jumping true within the load event for the
character so when the game starts he falls to the ground. Therefore, your load
events being (the bold is the new code):
- onClipEvent (load)
{
- jumping =
true;
- jump =
0;
- speed =
0;
- maxmove =
15;
- }
Even though we now have jumping we will fall through the ground. To resolve
that we need hitTest events. Insert this directly after the jumping events.
Place it after this part of the jumping code:
- if (jump<-15)
{
- jump =
-15;
- }
- }
- if (_root.ground.hitTest(this._x,
this._y,
true)
&& falling)
{
- jump =
12;
- jumping =
false;
- falling =
false;
- }
That code checks that the char is hitting its X and Y positions on the ground
and they are falling. Then is sets the variables to prevent errors.
Thus, all of your code should look like the following:
- onClipEvent (load)
{
- jumping =
true;
- speed =
0;
- maxmove =
15;
- jump =
0;
- }
- onClipEvent (enterFrame)
{
- if (_root.dead)
{
- this.gotoAndStop("dead");
- } else
{
- speed *=
.85;
- if (dir
== "right")
{
- 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");
- } else
if (speed<1
&& speed>-1)
{
- 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;
- }
- }
- if (_root.ground.hitTest(this._x,
this._y,
true)
&& falling)
{
- jump =
12;
- jumping
= false;
- falling
= false;
- }
- }
- }
Now test you movie. You will notice that if you walk off the edge without
jumping you do not fall. However, if you constantly check if your character is
not touching the ground and that they aren’t jumping you can make them fall by
setting a fall speed and setting jumping true.
Simply insert this code after the enterFrame code:
- …
- onClipEvent (enterFrame)
{
- if (!_root.ground.hitTest(this._x,
this._y,
true)
&& !jumping)
{
- this._y
+= 6;
- }
- if (_root.dead)
{
- this.gotoAndStop("dead");
- } else
{
- …
Now it is actually beginning to look like a game. There is more of this game
coverage on the next page!
Onwards to the next page!
|
page 2 of 5 |
|
|