Home / Tutorials / Games / Collision Detection /

Flash Tutorials

Collision Detection - Collision Detection

Posted by : PhilS on Nov 03, 2008

 

5.0/5

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.

Click on the Movie and use your Keyboard Arrows to Control the Blue Ball.

[tl]Step One: Setting Up the Movie[/tl] 1. Open a new: Flash Movie 2. Go to: Modify - Document 3. Set the Movie to: 550 x 400 4. If you wish set a: Background Colour 5. Click: OK 6. In the Timeline right click (Mac: Ctrl click) Frame 3 and select: Insert Frame 7. In the Timeline use the Insert Layer button to add 5 Layers so you have a total of: 6 Layers 8. Name the Layers as follows:

Your Timeline should look similar to this.

[tl]Step Two: Creating the First Frame[/tl] 1. Select Frame 1 of the: Text & Button Layer 2. Create a button with a Label: Click to Play

      [quote]**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)[/quote]

3. Attach the following ActionScript to the Button:

          on(release){
              gotoAndStop(2);
          }



      [quote]**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.[/quote]

4. You don't want the Button to be visible on the next frame so:

  Right click on **Frame 2** of the **Text & Button Layer** and select: **Insert Blank Keyframe**
  1. Select Frame 1 of the: Background Layer
  2. Place any other graphics on this frame that you may want. I have placed a shadow of the Maze in this frame. If you wish to do this come back and do this latter when you have finished creating the Maze: Create a Background
  3. You don't want the Background to be visible on the next frame so:

    Right click on Frame 2 of the Background Layer and select: Insert Blank Keyframe

  4. Select Frame 1 **of the: **ActionScript Layer

  5. Attach the following ActionScript to Frame 1:

          stop();
    

Your Timeline should now look similar to this.

[tl]Step Three: Creating the Player[/tl]

In my case the Player is the small blue circle:

  1. Right Click on Frame 2 **of the Player Layer and select: **Insert Blank Keyframe
  2. Select the Oval Tool:
  3. Create a small: Circle (or square if you prefer)
  4. In the Property Inspector set the size to: 8 x 8 pixels[quote]** Note:** It must be easily small enough to fit through the Maze.[/quote]
  5. Return to the standard Selection tool:
  6. Select the: Circle (make sure the whole thing is selected)
  7. Right click the Circle and select: Convert it to Symbol
  8. For Name type: player
  9. For Behavior select: Movie Clip
    1. Click: OK
    2. If the Property Inspector is closed, open it: **Window - Properties **(Ctrl F3)
    3. Give the Movie Clip an Instance Name: player

[tl]Step Four: Creating the End[/tl]

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:

  1. Right Click on Frame 2 of the End Layer **and select: **Insert Blank Keyframe
  2. Select the Rectangle Tool:
  3. Create a small: Rectangle
  4. In the Property Inspector set the size to about: 30 x 20 pixels

      [quote]**Note: **Don't make it too small or your Player could jump right over it rather than hit the **end**.[/quote]
    
  5. It you wish select the Text tool and type: END
  6. Return to the standard Selection tool:
  7. Select the: Rectangle (make sure the whole thing is selected)
  8. Right click the rectangle and select: Convert it to Symbol
  9. For Name type: end
    1. For Behavior select: Movie Clip
    2. Click: OK
    3. If the Property Inspector is closed, open it: Window - Properties (Ctrl F3)
    4. Give the Movie Clip an Instance Name: end

[tl]Step Five: Creating the Maze[/tl]

  1. Right Click on Frame 2 **of the **ActionScript Layer and select: Insert Blank Keyframe
  2. Select one of the Drawing tools such as the Pen Tool:
  3. Draw your: Maze Walls

      **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.
    
  • My Walls are at least: 6 Pixels Thick
  • My Paths are at least: 10 Pixels Wide

    1. Return back to the standard Selection tool:
    2. Select the: **Drawing **(make sure the whole thing is selected)
    3. Right click the Maze drawing and select: Convert it to Symbol
    4. For Name type: walls
    5. For Behavior select: Movie Clip
    6. Click:** OK**
    7. If the Property Inspector is closed, open it: Window - Properties (Ctrl F3)
    8. Give the Movie Clip an Instance Name:** walls**
    9. Right Click on the Movie Clip again and select: Convert it to Symbol
    10. For Name type: maze
    11. For Behavior select: Movie Clip
    12. Click: OK[quote]** 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:[/quote]
    13. Attach the following ActionScript to the outside of the Maze Movie Clip:

        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);
                }
            }
        }
      

[tl]Step Six: Frame 3[/tl] 1. Right Click on Frame 3 of the Text & Buttons Layer and select: Insert Blank Keyframe 2. Type something like: You One !! 3. Create a button to return and play again: Replay Button 4. Add the following ActionScript to this Button:

          on (release){
              gotoAndStop(2);
          }

[tl]Step Seven: Testing the Movie[/tl] 1. In Frame 2 position the Player Movie Clip at the: Start Position 2. In Frame 2 position the End Movie Clip at the: End Position

  Building the Movie is now complete. What you are testing is that the Player does not wobble or go straight through the walls.

[u]** The Wobbles**[/u] The Player may vibrate if the path between the walls is not big enough and the bounce is too big (see example here).

  To resolve this either:[list][li]Make the variable myBounce smaller[/li][li]Make the Player smaller[/li][li]Make the Path (gap between the walls) bigger[/li][/list]
  [u]**No Hit**[/u]
  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:[list][li]Make the variables myBounce and mySpeed smaller[/li][li]Make the path (space between the walls) larger[/li][li]Make the walls thicker[/li][li]Make the Player size either bigger or smaller[/li][/list][quote]**
        Note:** I have found that the variables myBounce and mySpeed are more likely to cause problems if they are set to different values. So I suggest that if you change one you change the other.[/quote]

3. Test your Movie: Control - Test Movie (Ctrl Enter)[quote] 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.[/quote][tl]Just for fun:[/tl] [More of an Obstacle Course than a Maze.][15]

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 !!

Source: http://www.webwasp.co.uk/tutorials/b32-collision-detection/index.php **

no comment

Add comment

Please login to post comments.


SUPPORTERS