Personal tools

Debugging Listing 2 (ActionScript tutorial)

From Whirled

Jump to: navigation, search

This is for use with the Debugging (ActionScript tutorial).

  1 package {
  2
  3 import flash.display.DisplayObject;
  4 import flash.display.Sprite;
  5
  6 import flash.events.Event;
  7 import flash.events.TimerEvent;
  8
  9 import flash.filters.GlowFilter;
 10
 11 import flash.media.Sound;
 12
 13 import flash.utils.getTimer; // function import
 14 import flash.utils.Timer;
 15
 16 import com.whirled.AvatarControl;
 17 import com.whirled.ControlEvent;
 18
 19 // Text support
 20 import flash.text.TextField;
 21 import flash.text.TextFieldAutoSize;
 22
 23 [SWF(width="181", height="170")]
 24 public class Rooster extends Sprite
 25 {
 26     /**
 27      * Constructor.
 28      */
 29     public function Rooster ()
 30     {
 31         // create and add the image
 32         _image = (new IMAGE() as DisplayObject);
 33
 34         // use the _avatarSprite sprite to easily offset the image without
 35         // having to worry about the image's offset ever again
 36         _avatarSprite = new Sprite();
 37         _avatarSprite.x = 10;
 38         _avatarSprite.y = 10;
 39         _avatarSprite.addChild(_image);
 40         addChild(_avatarSprite);
 41
 42         // create the control for whirled communication
 43         _control = new AvatarControl(this);
 44         // add a listener for appearance updates
 45         _control.addEventListener(ControlEvent.APPEARANCE_CHANGED,
 46             handleAppearanceChanged);
 47         // add a listener to hear about speak events
 48         _control.addEventListener(ControlEvent.AVATAR_SPOKE,
 49             handleSpoke);
 50
 51         // add custom actions and listen for custom action events
 52         _control.registerActions("Beep", "Double Beep", "Test");
 53         _control.addEventListener(ControlEvent.ACTION_TRIGGERED,
 54             handleAction);
 55
 56         // create our beep sound
 57         _beep = (new BEEP() as Sound);
 58
 59         // When we do speak, we're going to glow purple for 200ms, then stop
 60         _speakTimer = new Timer(200, 1);
 61         _speakTimer.addEventListener(TimerEvent.TIMER, doneSpeaking);
 62
 63         // We should erase the chatBox after a second
 64         _chatBoxTimer = new Timer(1000, 1);
 65         _chatBoxTimer.addEventListener(TimerEvent.TIMER, doneBeeping);
 66
 67         // Prepare message
 68         _msg = new TextField();
 69         _msg.border = true;
 70         _msg.background = true;
 71         _msg.autoSize = TextFieldAutoSize.LEFT;
 72
 73         // and kick things off by updating the appearance immediately
 74         updateAppearance();
 75     }
 76
 77     /**
 78      * Handles ControlEvent.APPEARANCE_CHANGED.
 79      */
 80     protected function handleAppearanceChanged (event :ControlEvent) :void
 81     {
 82         updateAppearance();
 83     }
 84
 85     /**
 86      * Update the appearance of this avatar.
 87      */
 88     protected function updateAppearance () :void
 89     {
 90         var orientation :Number = _control.getOrientation();
 91
 92         // we assume that our image naturally faces left
 93         if (orientation < 180) {
 94             // we need to face right
 95             _image.x = _image.width;
 96             _image.scaleX = -1;
 97
 98         } else {
 99             // normal facing
100             _image.x = 0;
101             _image.scaleX = 1;
102         }
103
104         // see if we need to update our bouncing
105         if (_bouncing != _control.isMoving()) {
106             _bouncing = _control.isMoving();
107             if (_bouncing) {
108                 addEventListener(Event.ENTER_FRAME, handleEnterFrame);
109                 _bounceStamp = getTimer();
110
111             } else {
112                 removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
113                 _image.y = 0; // reset to unbouncing
114             }
115         }
116     }
117
118     /**
119      * We've just spoken. Glow purple for a bit.
120      */
121     protected function handleSpoke (event :ControlEvent) :void
122     {
123         // reset the timer, cancels any pending 'doneSpeaking' call.
124         _speakTimer.reset();
125
126         // only set up the filter if it hasn't already
127         if (this.filters == null || this.filters.length == 0) {
128             this.filters = [ new GlowFilter(0xFF00FF, 1, 10, 10) ];
129         }
130         // start the timer
131         _speakTimer.start();
132     }
133
134     /**
135      * It's time to stop showing our speaking glow.
136      */
137     protected function doneSpeaking (event :TimerEvent) :void
138     {
139         this.filters = null;
140     }
141
142     /**
143      * It's time to stop showing our chat box.
144      */
145     protected function doneBeeping (event :TimerEvent) :void
146     {
147         /* Remove the messsage */
148         _avatarSprite.removeChild(_msg);
149     }
150
151     /**
152      * Called just prior to every frame that is rendered to screen.
153      */
154     protected function handleEnterFrame (event :Event) :void
155     {
156         var now :Number = getTimer();
157         var elapsed :Number = getTimer() - _bounceStamp;
158
159         // compute our bounce
160         _image.y = BOUNCE_AMPLITUDE *
161             Math.sin(elapsed * (Math.PI * 2) / BOUNCE_PERIOD);
162     }
163
164     /**
165      * Handle ACTION_TRIGGERED to play our custom actions.
166      */
167     protected function handleAction (event :ControlEvent) :void
168     {
169
170         // Reset the chatBox timer in case an event is
171         // retriggered before the timer goes off.
172         _chatBoxTimer.reset();
173
174         switch (event.name) {
175         case "Beep":
176             _msg.text = "Beep"
177             _beep.play();
178             break;
179
180         case "Double Beep":
181             _msg.text = "Double Beep"
182             _beep.play(0, 2);
183             break;
184
185         default:
186             _msg.text = "Unknown action:" + event.name
187             trace("Unknown action: " + event.name);
188             break;
189         }
190         // For all events pop up a message
191         _avatarSprite.addChild(_msg);
192         _chatBoxTimer.start();
193     }
194
195     /** The image we're displaying. */
196     protected var _image :DisplayObject;
197
198     /** Container for the image (avatar) */
199     protected var _avatarSprite :Sprite;
200
201     /** Our beep sound. */
202     protected var _beep :Sound;
203
204     /** The avatar control interface. */
205     protected var _control :AvatarControl;
206
207     /** Controls the timing of our speak action. */
208     protected var _speakTimer :Timer;
209
210     /** Controls the timing of our chat box. */
211     protected var _chatBoxTimer :Timer;
212
213     /** Message variable holding our chat content */
214     protected var _msg :TextField;
215
216     /** Are we bouncing? */
217     protected var _bouncing :Boolean;
218
219     /** The timestamp at which we started bouncing. */
220     protected var _bounceStamp :Number;
221
222     /** The amplitude of our bounce, in pixels. */
223     protected static const BOUNCE_AMPLITUDE :int = 10;
224
225     /** The period of our bounce: we do one bounce every 500 milliseconds. */
226     protected static const BOUNCE_PERIOD :int = 500;
227
228     /** The embedded image class. */
229     [Embed(source="Rooster.png")]
230     protected static const IMAGE :Class;
231
232     [Embed(source="beep.mp3")]
233     protected static const BEEP :Class;
234 }
235
236 } // END: package