/** * Virtual Zoo Example Program, Final Version * from O'Reilly's Essential ActionScript 3.0 * by Colin Moock */ package { import flash.display.Sprite; //import zoo.*; import flash.events.*; // The actionscript_zoo_VirtualZoo class is the main application class. It extends Sprite // so that it can be instantiated and added to the display list at // program-start time. public class actionscript_zoo_VirtualZoo extends Sprite { // The VirtualPet instance private var pet:actionscript_zoo_lib_VirtualPet; // The object that will render the pet to the screen private var petView:actionscript_zoo_lib_VirtualPetView; // Constructor public function actionscript_zoo_VirtualZoo () { // Create a new pet, and attempt to give it a name try { pet = new actionscript_zoo_lib_VirtualPet("Bartholomew McGillicuddy"); } catch (e:Error) { // If attempting to create a VirtualPet object causes an exception, // then the object won't be created. Hence, we report the problem // and create a new VirtualPet object here with a known-to-be-valid // name. trace("An error occurred: " + e.message); pet = new actionscript_zoo_lib_VirtualPet("Stan"); } // Create the object that will render the pet to the screen petView = new actionscript_zoo_lib_VirtualPetView(pet); // Register this actionscript_zoo_VirtualZoo object to be notified when the // rendering object (the "petView") has finished initializing petView.addEventListener(Event.COMPLETE, petViewCompleteListener); } // An event listener triggered when the VirtualPetView object (petView) // has finished initializing public function petViewCompleteListener (e:Event):void { // Add the view to the display list addChild(petView); // Begin the pet's life cycle pet.start(); // Feed the pet pet.eat(new actionscript_zoo_lib_Sushi()); } } }