- Game Design
- Creating the Graphics
- Adding the Code
- Summary
Adding the Code
The logic that drives all of the action in the game is encased within ActionScript. There is a lot, so let's get going.
Select frame in the scripts layer and add the following ActionScript. This script establishes the default values for variables that you will be using throughout the game:
x = 200; // character's world coordinates - X-Axis y = 200; // character's world coordinates - Y-Axis offx = 0; // offset for controlling movement of // world for X-Axis offy = 0; // offset for controlling movement of // world for Y-Axis dx = 0; // character's x and y velocity dy = 0; maxspeed = 10; // fastest speed you can run maxfall = 10; // fastest speed you can fall jumpheight = 15; // how high you jump keyright = false; // flags keyleft = false; startrun = false; jumping = false; falling = false; downkey = false; scrollon = true; // defines the game will scroll stop();
Select the button that you placed on the stage in Frame 1. Add the following script. This tells Flash to go to Frame 2 to play the game:
on (release) { play(); }
Select Frame 2 of the Scripts layer and add the ActionScript stop();.
Select the Controller square that you placed on the screen. Here is where all of the variables that you added at the beginning of the game have their values accessed:
onClipEvent (enterFrame) { if (_root.keyright == true) { _root.ball._xscale = 100; _root.dir = 1; _root.dx += 1; } if (_root.keyleft == true) { _root.ball._xscale = -100; _root.dir = -1; _root.dx -= 1; } if (_root.keyleft == false and _root.keyright == false) { if (_root.dx<0) { _root.dx += .5; } if (_root.dx>0) { _root.dx -= .5; } if (_root.dx == 0 and _root.dy>=0) { _root.ball.gotoAndPlay(1); } } if (_root.dx>_root.maxspeed) { _root.dx = _root.maxspeed; } if (_root.dx<-_root.maxspeed) { _root.dx = -_root.maxspeed; } if (_root.startrun == true) { _root.ball.gotoAndPlay(7); _root.startrun = false; } if (_root.downkey == true) { _root.ball.gotoAndPlay(57); } if (_root.dy == 0) { _root.jumping = false; } if ((_root.ground.blocks.hitTest(_root.ball._[ccc] x, _root.ball._y+_root.dy, true) == true) and [ccc] !_root.jumping) { _root.falling = false; _root.jumping = false; _root.dy = 0; } else if (_root.ground.blocks.hitTest[ccc] (_root.ball._x, _root.ball._y-40+_root.dy, true)) { _root.falling = true; _root.jumping = false; _root.dy = 2; } else { _root.falling = true; _root.dy++; }
Check for collision with walls horizontally:
if (_root.dx<0) { if (_root.downkey == false) { if (_root.ground.blocks.hitTest[ccc] (_root.ball._x-15, _root.ball._y-10, true) [ccc] or _root.ground.blocks.hitTest(_root.ball._[ccc] x-15, _root.ball._y-40, true)) { _root.dx = 0; } } else { if (_root.ground.blocks.hitTest[ccc] (_root.ball._x-15, _root.ball._y-10, true)) { _root.dx = 0; } } } else if (_root.dx>0) { if (_root.downkey == false) { if (_root.ground.blocks.hitTest[ccc] (_root.ball._x+15, _root.ball._y-10, true) [ccc] or _root.ground.blocks.hitTest(_root.ball._[ccc] x+15, _root.ball._y-40, true)) { _root.dx = 0; } } else { if (_root.ground.blocks.hitTest[ccc] (_root.ball._x+15, _root.ball._y-10, true)) { _root.dx = 0; } } } if (_root.dy>_root.maxfall) { _root.dy = _root.maxfall; } _root.y += _root.dy; _root.x += _root.dx; _root.ground._x = -_root.offx; _root.ground._y = -_root.offy; _root.ball._x = _root.x-_root.offx; _root.ball._y = _root.y-_root.offy; }
This defines the area of the scrolling screen. You might want to change these coordinates when you customize your own games.
if (_root.scrollon == true) { if ((_root.x-_root.offx)>330) { _root.offx += _root.dx; } if ((_root.x-_root.offx)<70) { _root.offx += _root.dx; } if ((_root.y-_root.offy)>270) { _root.offy += _root.dy; } if ((_root.y-_root.offy)<70) { _root.offy += _root.dy; } } else { if ((_root.x-_root.offx)>370) { _root.offx += 370; } if ((_root.x-_root.offx)<30) { _root.offx -= 370; } if ((_root.y-_root.offy)>280) { _root.offy += 270; } if ((_root.y-_root.offy)<30) { _root.offy -= 270; } } }
Next, you need to select the droid and add the ActionScript that will allow you to control the droid as it moves around the screen:
onClipEvent (keyDown) { if (Key.getCode() == Key.RIGHT and [ccc] root.keyright == false and _root.downkey [ccc] == false) { _root.keyright = true; _root.startrun = true; } if (Key.getCode() == Key.LEFT and [ccc] root.keyleft == false and _root.downkey [ccc] == false) { _root.keyleft = true; _root.startrun = true; } if (Key.getCode() == Key.SHIFT && [ccc] root.jumping == false && _root.dy == [ccc] 0 and _root.downkey == false) { _root.falling = true; _root.jumping = true; _root.dy = -_root.jumpheight; } if (Key.getCode() == Key.DOWN) { _root.downkey = true; _root.keyleft = false; _root.keyright = false; } } onClipEvent (keyUp) { if (Key.getCode() == Key.RIGHT) { _root.keyright = false; } if (Key.getCode() == Key.LEFT) { _root.keyleft = false; } if (Key.getCode() == Key.DOWN and [ccc] root.keyleft == false and _root.keyright [ccc] == false) { _root.downkey = false; } }
The final step is to select the exit door at the end of the game. As the droid moves over this door, you will be able to use the onClipEvent (enterFrame) to trigger and event. This can send the character to another screen or to a new Web site. Select the exit door and add the following ActionScript:
onClipEvent (enterFrame) { if (this.hitTest(_root.ball)) { getURL("http://www.matthewdavid.ws", _self); _root.gotoAndPlay (1); } }
You can see that the hitTest object is used a lot in this movie. hitTest is a very useful command that lets you know when you have collided with an object on the stage. In this instance, the collision is with a movie clip doorway.