How to Delete a Movie Clip in AS3

There is no delete movie clip command available in the Flash ActionScript 3 (AS3) programming language. However, there are AS3 methods that can remove a movie clip from the Flash stage, loader methods that can unload a loader that contains a movie clip, and a removeChild method that can delete the movie clip from the AS3 display list and Flash stage. If you use the unload, or unLoadandStop, method on the loader, you will delete the movie clip from memory but not the stage.

Things You'll Need

  • Adobe Flash Professional: CS3, CS4 or CS5 versions
  • SWF Flash file
Show More

Instructions

    • 1

      Start the Flash program. Click "Flash File (ActionScript 3.0)" from the splash window to create a new file for an AS3 Flash animation project.

    • 2

      Select the "Actions" option from the Window menu on the main Flash menu bar to open the Actions editor. Position your mouse cursor on the first line of the Actions editor. Click your mouse button and type in the code below to create a Loader object that will load a movie clip SWF file named "Shape_Break.swf" that is in the directory in which you plan to save your Flash AS3 project file.

      var movieClipLoader:Loader = new Loader();

      var movieClipFile:MovieClip = new MovieClip();

      var movieClipLocation:URLRequest = new URLRequest("Shape_Break.swf");

      movieClipLoader.load(movieClipLocation);

    • 3

      Type the code below, starting at the next line in the Actions editor, to add an event listener to the Loader object that will detect when the SWF file has been loaded and call a function named loadMovie_fn that adds the SWF movie clip to the Flash stage at the Flash stage coordinate (250,250) and scales the size of the SWF movie clip by 50 percent.

      movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMovie_fn);

      function loadMovie_fn(event:Event):void

      {

      movieClipFile=event.target.content;

      addChild(movieClipFile);

      movieClipFile.scaleX = 0.5;

      movieClipFile.scaleY = 0.5;

      movieClipFile.x = 250;

      movieClipFile.y =250;

      };

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(350, 200, 50, 20);

      myButton.graphics.endFill();

      addChild(myButton);

      myButton.addEventListener(MouseEvent.CLICK, callFunction_fn);

      function callFunction_fn(e:MouseEvent): void {

      removeChild(movieClipFile);

      removeChild(movieClipLoader);

      movieClipLoader.unload();

      addChild(movieClipFile);

      addChild(movieClipLoader);

      ;

      };

    • 4

      Type the code below starting at the next line in the Actions editor to create and place a red rectangular graphic (called myButton) on the stage that is positioned at Flash stage coordinates (350, 200), has a width of 50 pixels, a height of 20 pixels and is drawn with a black line with a width of 4 pixels.

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(350, 200, 50, 20);

      myButton.graphics.endFill();

      addChild(myButton);

    • 5

      Type the code below starting at the next line in the Actions editor to add an event listener to myButton that will execute the code in a function named "callFunction_fn" when myButton is clicked.

      myButton.addEventListener(MouseEvent.CLICK, callFunction_fn);

    • 6

      Type the code below starting at the next line in the Actions editor to remove the movie clip and the loader when myButton is clicked and then attempt to add the movie clip and loader back onto the stage.

      function callFunction_fn(e:MouseEvent): void {

      removeChild(movieClipFile);

      removeChild(movieClipLoader);

      movieClipLoader.unload();

      addChild(movieClipFile);

      addChild(movieClipLoader);

      ;

      };

    • 7

      Copy and paste the code below into the Actions editor to ensure that there are no syntax errors and the delete movie clip program runs as intended.

      var movieClipLoader:Loader = new Loader();

      var movieClipFile:MovieClip = new MovieClip();

      var movieClipLocation:URLRequest = new URLRequest("Shape_Break.swf");

      movieClipLoader.load(movieClipLocation);

      movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMovie_fn);

      function loadMovie_fn(event:Event):void

      {

      movieClipFile=event.target.content;

      addChild(movieClipFile);

      movieClipFile.scaleX = 0.5;

      movieClipFile.scaleY = 0.5;

      movieClipFile.x = 250;

      movieClipFile.y =250;

      };

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(350, 200, 50, 20);

      myButton.graphics.endFill();

      addChild(myButton);

      myButton.addEventListener(MouseEvent.CLICK, callFunction_fn);

      function callFunction_fn(e:MouseEvent): void {

      removeChild(movieClipFile);

      removeChild(movieClipLoader);

      movieClipLoader.unload();

      addChild(movieClipFile);

      addChild(movieClipLoader);

      ;

      };

    • 8

      Click the "TestMovie" option in the Control menu on the main Flash menu bar to play the delete movie clip movie. Observe that the Shape_Break movie is on the stage. Click the red button on the stage and observe that the "Shape_Break.swf" movie clip is removed from the stage. Also observe that an error is displayed in the Flash output panel that indicates that the attempt to add the movieclip and the loader back onto the stage failed because the loader already has been unloaded (as intended).

Learnify Hub © www.0685.com All Rights Reserved