|
||||||
|
||||||
Flash Tutorial - Collision Detection

Free
Tutorial
![]()
Learn how to make a basic maze game with collision detection. Collision detection is useful in many different game type scenarios and is easier than you might think. The Collision Detection ensures that the Blue Ball (or Player) does not cross the red lines of the Maze.
Example: Download
the Flash file Int
132a
Click on the Movie above and use your Keyboard Arrows to Control the Blue Ball.
Step One: Setting Up the Movie

Step Two: Creating the First Frame
Note: If you wish drag out one of the Standard Buttons from the Common Library: Window > Other Panels > Common Libraries > Buttons (do not use a Knob, fader or Component)
on(release){
gotoAndStop(2);
}Note: The reason that you need this is to make sure the Flash Movie is in Focus. Otherwise when the player goes to use the Arrow Keys on their Keyboard the web page will scroll up and down instead of the Flash Movie responding to the Key commands.
stop();
Your Timeline should now look similar to this.
Step Three: Creating the Player
In my case the Player is the small blue circle:
Note: It must be easily small enough to fit through the Maze.
Step Four: Creating the End
When the Player hits the object called the End the
Movie will go to the next frame. In theory the End could be
invisible. My End looks like this: ![]()
Note: Don't make it too small or your Player could jump right over it rather than hit the end.
Step Five: Creating the Maze
Note: If your Maze drawing is going to very complex I suggest you only draw a small section of the Maze to start with. You can come back and add to the drawing latter. If the walls are too thin or the path (where your blue dot moves) is too narrow you may find that the Player goes straight through the walls. The thicker the Walls and the wider the Paths the faster you can make your Player move so try not to make the walls too thin.
Note: This creates a series of nested Movie Clips one inside another. The Walls Movie Clip is inside the Maze Movie Clip. The Maze Movie Clip is on the Main Stage:
![]()
onClipEvent (enterFrame) {
with (_root.player) {
// Controls Player Speed
mySpeed = 3;
// Controls how far the Player bounces off the wall after impact
myBounce = 3;
// keyboard controls
if (Key.isDown(Key.DOWN)) {
_y += mySpeed;
}
if (Key.isDown(Key.UP)) {
_y -= mySpeed;
}
if (Key.isDown(Key.LEFT)) {
_x -= mySpeed;
}
if (Key.isDown(Key.RIGHT)) {
_x += mySpeed;
}
// detect if edges of the player is colliding with the Maze Walls
if (walls.hitTest(getBounds(_root).xMax, _y, true)) {
_x -= myBounce;
}
if (walls.hitTest(getBounds(_root).xMin, _y, true)) {
_x += myBounce;
}
if (walls.hitTest(_x, getBounds(_root).yMax, true)) {
_y -= myBounce;
}
if (walls.hitTest(_x, getBounds(_root).yMin, true)) {
_y += myBounce;
}
// detect if Maze is finished
if (_root.end.hitTest(_x, getBounds(_root).yMax, true)) {
_root.gotoAndStop(3);
}
}
}
Step Six: Frame 3
on (release){
gotoAndStop(2);
}
Step Seven: Testing the Movie
No Hit
If for example you speed (variable mySpeed) is 3 then the Player only exists
every 3 pixels. This animated effect looks like a smooth motion but in
fact it is not. If your wall is thinner than this the Player will simply
jump right over the top although it looks like the Player goes through
the wall rather than a jump. Even if you think your walls are thick enough
you may occasionally get this problem. This is because of the bounce plus
the speed plus the size of the Player plus the width of the walls all go
into the calculation that controls the movement of the Player. This can
occasionally give you unexpected results. If the hit does not work correctly
then the Player may simply go right through the walls.
To resolve this try the following:
Note: If you need to edit the Maze drawing it is no longer on the Main Stage but nested inside the Movie Clips. Keep double clicking on the drawing until it becomes selected.
Just for fun:
Example: Download
the Flash file Int
132b
More of an Obstacle Course than a Maze.
Because of the organic nature of the walls in the Movie above you may find that it is much harder to test if the hit always works and all the walls are all solid. You literally need to test them all!!
That's it !!
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
96702 visitors to this page since
Jan 04 •
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|