REMOVING DYNAMICALLY CREATED CONTENT


You can easily remove dynamically drawn lines from a timeline using the clear() method. Here's the syntax:

 path.clear(); 

Movie clip instances that are created using duplicateMovieClip() or attachMovie() (such as icons that are dragged and dropped onto the canvas) can be removed using a method of the Movie Clip object called removeMovieClip() .

NOTE

You can't use this method to remove movie clip instances that were not created using ActionScript (that is, those you've actually dragged from the library and placed on the stage).


Removing movie clip instances can be useful for dynamically clearing the stage content, freeing up system resources, re-initializing applications, or adding functionality to some applications. As you can see from the following, the syntax is simple:

 someMovieClip.removeMovieClip() 

In this exercise you will create a function that will clear the canvas of all dynamically drawn lines as well as instances of the icon movie clip that might be dragged and dropped on the canvas.

  1. Open draw4.fla in the Lesson14/Assets folder.

    This is the file as you left it at the end of the previous exercise. In this exercise, you'll add a function to the controller that will clear the canvas of any content and you will add an action to the Clear button in the window2 movie clip instance.

  2. With the Actions panel open, select the controller movie clip instance and add this function definition to the load event:

     function clearContent () {    _root.holder.clear()    i = 0;    while (++i <= v) {      name = "object" + i;      _root[name].removeMovieClip();    }    v = 0;  } 

    The first action in this function clears all dynamically drawn lines from the holder movie clip instance. Next, a simple while loop is used to remove any instances of the icon movie clip instance that have been dragged and dropped onto the canvas. Since the value of v is incremented with each icon instance that is dragged and dropped (as shown in the buildIconList() function definition scripted in the previous exercise), its value always represents the number of icon instances that have been duplicated and placed on the canvas. This value is thus used in the loop to set how many iterations the removal loop should perform. After the loop finishes removing all the dragged and dropped icon instances, the value of v is set to 0 so that the next time an icon instance is dragged and dropped onto the canvas, naming of the duplicated instances can once again begin with 0.

    graphics/14fig25.gif

  3. Double click on the window2 instance to edit it in place. Move the admin frame label.

    This frame contains two buttons, a Clear button, and a Print button. You will add ActionScript to the Clear button that will call the clearContent() function in the controller movie clip instance. While you're editing this frame, you'll also add an action to the Print button.

  4. With the Actions panel open, select the Print button and add this script:

     on (release) {    printAsBitmap ("_root", "bmovie");  } 

    When the button is released, the graphical content of the root timeline, as well as of any of its child movies (essentially everything you see, including dynamically created instances), will be printed as a bitmap graphic at actual size.

    NOTE

    This script is just used to demonstrate how dynamically created instances can be printed as easily as anything else. Had we wanted to get a bit more sophisticated, we might have opted to print only what was drawn on the canvas itself. However, this would require some different syntax in our various functions, and printing options are not the focus of this exercise.

  5. With the Actions panel open, select the Clear button and enter this ActionScript:

     on (release) {    _parent.controller.clearContent();  } 

    When this button is released, the clearContent() function attached to the controller instance will be executed.

  6. Choose Control > Test Movie to test the movie. Draw a few lines and then press the Clear button.

    When you press the Clear button, the clear function is executed and the while loop loops through and deletes every created movie clip instance.

  7. Close this movie and save your work as draw5.fla.

    This completes the exercise and the lesson. As you have learned, the dynamic removal of content is even easier than its creation. However, knowing how to do both allows your projects to scale and change in many ways based on user input and interaction. Isn't ActionScript the best?!



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net