Predicting Collisions - Page 6
       by kirupa  |  23 October 2005

In the previous page, I finished explaining what all of the ActionScript code does. In this page, I will summarize what was discussed in the past five pages.

Quick Summary
Occasionally, for longer tutorials, I will try to provide a quick English-only summary of the code in a few paragraphs. That may help to give you a better idea of how the code spread across the previous pages fits together.

When you click your mouse at a target, your mainCircle movie clips begins to move in that direction. The code for the movement is based on Lostinbeta's tutorial. Here is your objective: you want any objects that lie in the mainCircle's path to move out of the way. You need to predict a collision before it happens.

You check for collisions by sending out a number of path circles that propagate towards your final target. The number of circles you send out is determined by your steps variable. Any object that collides with any of the path circles is instantly added to an array (final_targets). In the end, the final_targets array contains the name of every obstacle that is in the mainCircle's path.

Once you have a list of names in your final_targets array, you realize that they must be moved or else face collision with mainCircle. Using the moveAway function, you cycle through each name found in the final_targets array and move those named objects out of the way.

All of the above happens before your mainCircle has even moved a few pixels!

External Tutorials Referenced

This tutorial relied heavily on code and ideas from the following tutorials:

  Easing on Mouse Click (by lostinbeta)

  Collision Detection Among Multiple Objects

  Finding Values in an Array

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!


page 6 of 6


SUPPORTERS:'s fast and reliable hosting provided by Media Temple.