How to Count the Number of Movie Clips Within a Boundary in Flash

How to Count the Number of Movie Clips Within a Boundary in Flash thumbnail
Use ActionScript to manage a Flash boundary.

Adobe Flash enables you to create multimedia projects using numerous tools and processes. You can use a movie clip as a box to create a boundary that allows you to set limits within your project. Perhaps you want objects to move within a limited space or you want to track your animated objects within that space. Using Flash's ActionScript programming language, you can count the movie clips that are present within your boundary.

Instructions

    • 1

      Open Flash and select "New" from the File menu. Choose to create an ActionScript 2.0 Flash file.

    • 2

      Create a box for your boundary using the rectangle tool. To make this invisible, use the Properties panel to set the alpha to 0. Use the Properties panel to set the instance name to "boundaryBox."

    • 3

      Create a circle using the oval tool. Press "F8" to set this to a movie clip. Copy the movie clip about five times to produce several clips. Place two of the circle clips inside the boundary.

    • 4

      Press "F9" to open the Actions window and insert the following:

      // bounds of the boundaryBox clip on the stage

      var clipBounds = boundaryBox.getBounds(_root);

      countMoviesInBox = function() {

      theCnt = 0

      for (var i in _root) {

      if ( typeof (_root[i]) == "movieclip") {

      // if within the boundary but don't count the boundary clip

      if( (_root[i]._x >= clipBounds.xMin && _root[i]._x <= clipBounds.xMax &&

      _root[i]._y >= clipBounds.yMin && _root[i]._y <= clipBounds.yMax) && i.substr(-3) != "Box") {

      theCnt++;

      }

      }

      }

      trace("Counted "+theCnt+" clips within the boundary.");

      }

      countMoviesInBox();

    • 5

      Press "Ctrl" and "Enter" to run the program. The message should output "Counted 2 clips within the boundary." Move the clips around and run the program again to test the accuracy of the message.

Related Searches:

References

  • Photo Credit movie reel image by Maria Bell from Fotolia.com

Comments

You May Also Like

Related Ads

Featured