How to Play and Remove Animation in Flash AS3

Playing and removing animation can be readily accomplished with Flash ActionScript 3 (AS3), the Flash programming language. Flash has hundreds of classes, methods and properties that let you control not just entire animations but also every individual animated pixel, sound, graphic and text element within an animation. Flash AS3 will let you play or remove sound, complete animation sequences, specific graphics in an animation sequence or pixel-level animated sequences.

Things You'll Need

  • Adobe Flash Professional: CS3, CS4 or CS5 versions
  • A Flash animation that you previously created with Adobe Flash and saved in the standard SWFfile format.
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 "Actions" from the Window menu on the main Flash menu bar to open the AS3 editor. Click your cursor on the first line of the editor and type in the code below to load the animation that you previously created and saved as"MasterIllustrationReferenceLibrary.swf" into this new Flash animation project.

      var movieContainer:MovieClip;

      var movieLoader:Loader = new Loader();

      movieLoader.load(new URLRequest("MasterIllustrationReferenceLibrary.swf"));

      movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);

      function movieLoaded_fn(e:Event): void {

      movieContainer = movieLoader.content as MovieClip;

      addChild(movieContainer);

      movieContainer.x = 100;

      }

    • 3

      Type the code below starting at the next line in the AS3 editor to create four buttons (red, yellow, green and blue) that, when pressed, will place the animation, remove the animation, play the animation or stop the animation.

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(10, 50, 50, 20);

      myButton.graphics.endFill();

      addChild(myButton);

      myButton.addEventListener(MouseEvent.CLICK, callFunction_fn);

      function callFunction_fn(e:MouseEvent): void {

      removeChild(movieContainer);

      };

      var myButton1:MovieClip = new MovieClip();

      myButton1.graphics.lineStyle(4);

      myButton1.graphics.beginFill(0xFFFF00);

      myButton1.graphics.drawRect(10, 100, 50, 20);

      myButton1.graphics.endFill();

      addChild(myButton1);

      myButton1.addEventListener(MouseEvent.CLICK, callFunction2_fn);

      function callFunction2_fn(e:MouseEvent): void {

      addChild(movieContainer);

      };

      var myButton2:MovieClip = new MovieClip();

      myButton2.graphics.lineStyle(4);

      myButton2.graphics.beginFill(0x00FF00);

      myButton2.graphics.drawRect(10, 150, 50, 20);

      myButton2.graphics.endFill();

      addChild(myButton2);

      myButton2.addEventListener(MouseEvent.CLICK, callFunction3_fn);

      function callFunction3_fn(e:MouseEvent): void {

      movieContainer.stop();

      };

      var myButton3:MovieClip = new MovieClip();

      myButton3.graphics.lineStyle(4);

      myButton3.graphics.beginFill(0x0000FF);

      myButton3.graphics.drawRect(10, 200, 50, 20);

      myButton3.graphics.endFill();

      addChild(myButton3);

      myButton3.addEventListener(MouseEvent.CLICK, callFunction4_fn);

      function callFunction4_fn(e:MouseEvent): void {

      ;

      movieContainer.play();

      };

    • 4

      Copy and paste the code to ensure that there are no syntax errors and that the Play and Remove Animations program works correctly.

      var movieContainer:MovieClip;

      var movieLoader:Loader = new Loader();

      movieLoader.load(new URLRequest("MasterIllustrationReferenceLibrary.swf"));

      movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);

      function movieLoaded_fn(e:Event): void {

      movieContainer = movieLoader.content as MovieClip;

      addChild(movieContainer);

      movieContainer.x = 100;

      }

      var myButton:MovieClip = new MovieClip();

      myButton.graphics.lineStyle(4);

      myButton.graphics.beginFill(0xFF0000);

      myButton.graphics.drawRect(10, 50, 50, 20);

      myButton.graphics.endFill();

      addChild(myButton);

      myButton.addEventListener(MouseEvent.CLICK, callFunction_fn);

      function callFunction_fn(e:MouseEvent): void {

      removeChild(movieContainer);

      };

      var myButton1:MovieClip = new MovieClip();

      myButton1.graphics.lineStyle(4);

      myButton1.graphics.beginFill(0xFFFF00);

      myButton1.graphics.drawRect(10, 100, 50, 20);

      myButton1.graphics.endFill();

      addChild(myButton1);

      myButton1.addEventListener(MouseEvent.CLICK, callFunction2_fn);

      function callFunction2_fn(e:MouseEvent): void {

      addChild(movieContainer);

      };

      var myButton2:MovieClip = new MovieClip();

      myButton2.graphics.lineStyle(4);

      myButton2.graphics.beginFill(0x00FF00);

      myButton2.graphics.drawRect(10, 150, 50, 20);

      myButton2.graphics.endFill();

      addChild(myButton2);

      myButton2.addEventListener(MouseEvent.CLICK, callFunction3_fn);

      function callFunction3_fn(e:MouseEvent): void {

      movieContainer.stop();

      };

      var myButton3:MovieClip = new MovieClip();

      myButton3.graphics.lineStyle(4);

      myButton3.graphics.beginFill(0x0000FF);

      myButton3.graphics.drawRect(10, 200, 50, 20);

      myButton3.graphics.endFill();

      addChild(myButton3);

      myButton3.addEventListener(MouseEvent.CLICK, callFunction4_fn);

      function callFunction4_fn(e:MouseEvent): void {

      ;

      movieContainer.play();

      };

    • 5

      Click "Test Movie" in the Control menu to play the Play and Remove Animations program. Observe that the animation that you previously created (titled "MasterIllustrationReferenceLibrary.swf") is removed from the stage when the red button is activated, it's placed back on the stage when the yellow button is activated, it stops playing when the green button is activated and it begins playing again when the blue button is activated.

Learnify Hub © www.0685.com All Rights Reserved