// DYNAMIC OBJECTS with Mouse Click controlling adding and removing new objects package { import flash.display.*; import flash.events.*; public class Wk9DynamicObjects extends MovieClip { // Create two new dynamic objects, a Butterfly and a Hummingbird var fly: Butterfly = new Butterfly( ); var bird: Hummingbird = new Hummingbird( ); var counter = 0; // A counter variable keeping track of mouse clicks function Wk9DynamicObjects( ) { fly.x = 100; fly.y = 100; bird.x = 200; bird.y = 100; // Add mouse event listener to the stage. stage.addEventListener(MouseEvent.MOUSE_DOWN, AddToCounter) } // end constructor // When the user clicks anywhere on the stage, increment the counter // and decide which object to display depending on the count function AddToCounter(evt:MouseEvent) { // Increase the counter each time the stage is clicked counter++; trace("stage clicked. Counter = " + counter); // When the counter reaches 1, add the butterfly, but stop its nested timelines, // so wings don't flap and it's stationary if (counter == 1) { addChild(fly); fly.stop( ); fly.leftWing.stop( ); fly.rightWing.stop( ); trace("butterfly added"); } // When the counter reaches 2, start the wings flapping else if (counter == 2) { fly.leftWing.play( ); fly.rightWing.play( ); } // When the counter reaches 3, move the butterfly on a motion path else if (counter == 3) fly.play( ); // When the counter reaches 4, remove the butterfuly, and add the hummingbird // but stop the bird's timeline, so it's hovering in one spot else if (counter == 4) { removeChild(fly); trace("butterfly removed"); addChild(bird); bird.stop( ); trace("hummingbird added"); } // When the counter reaches 5, play the bird's timeline, so it moves in a motion path else if (counter == 5) bird.play( ); // When the counter reaches 6, remove the hummingbird and set counter back to 0 // Now nothing is on the stage) else if (counter == 6) { counter = 0; removeChild(bird); } } // end addToCounter } }