Debugging Listing 1 (ActionScript tutorial)

From Whirled Club Wiki
Jump to: navigation, search

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

<actionscript>

 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 imageholder sprite to easily offset the image without
35         // having to worry about the image's offset ever again
36         var imageHolder :Sprite = new Sprite();
37         imageHolder.x = 10;
38         imageHolder.y = 10;
39         imageHolder.addChild(_image);
40         addChild(imageHolder);
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         // Prepare message
67         _msg = new TextField();
68         _msg.border = true;
69         _msg.background = true;
70         _msg.autoSize = TextFieldAutoSize.LEFT;
71
72         // and kick things off by updating the appearance immediately
73         updateAppearance();
74     }
75
76     /**
77      * Handles ControlEvent.APPEARANCE_CHANGED.
78      */
79     protected function handleAppearanceChanged (event :ControlEvent) :void
80     {
81         updateAppearance();
82     }
83
84     /**
85      * Update the appearance of this avatar.
86      */
87     protected function updateAppearance () :void
88     {
89         var orientation :Number = _control.getOrientation();
90
91         // we assume that our image naturally faces left
92         if (orientation < 180) {
93             // we need to face right
94             _image.x = _image.width;
95             _image.scaleX = -1;
96
97         } else {
98             // normal facing
99             _image.x = 0;

100 _image.scaleX = 1; 101 } 102 103 // see if we need to update our bouncing 104 if (_bouncing != _control.isMoving()) { 105 _bouncing = _control.isMoving(); 106 if (_bouncing) { 107 addEventListener(Event.ENTER_FRAME, handleEnterFrame); 108 _bounceStamp = getTimer(); 109 110 } else { 111 removeEventListener(Event.ENTER_FRAME, handleEnterFrame); 112 _image.y = 0; // reset to unbouncing 113 } 114 } 115 } 116 117 /** 118 * We've just spoken. Glow purple for a bit. 119 */ 120 protected function handleSpoke (event :ControlEvent) :void 121 { 122 // reset the timer, cancels any pending 'doneSpeaking' call. 123 _speakTimer.reset(); 124 125 // only set up the filter if it hasn't already 126 if (this.filters == null || this.filters.length == 0) { 127 this.filters = [ new GlowFilter(0xFF00FF, 1, 10, 10) ]; 128 } 129 // start the timer 130 _speakTimer.start(); 131 } 132 133 /** 134 * It's time to stop showing our speaking glow. 135 */ 136 protected function doneSpeaking (event :TimerEvent) :void 137 { 138 this.filters = null; 139 } 140 141 /** 142 * It's time to stop showing our chat box. 143 */ 144 protected function doneBeeping (event :TimerEvent) :void 145 { 146 /* Clear the container */ 147 _container.removeChild(_msg); 148 } 149 150 /** 151 * Called just prior to every frame that is rendered to screen. 152 */ 153 protected function handleEnterFrame (event :Event) :void 154 { 155 var now :Number = getTimer(); 156 var elapsed :Number = getTimer() - _bounceStamp; 157 158 // compute our bounce 159 _image.y = BOUNCE_AMPLITUDE * 160 Math.sin(elapsed * (Math.PI * 2) / BOUNCE_PERIOD); 161 } 162 163 /** 164 * Handle ACTION_TRIGGERED to play our custom actions. 165 */ 166 protected function handleAction (event :ControlEvent) :void 167 { 168 169 // Reset the chatBox timer in case an event is 170 // retriggered before the timer goes off. 171 _chatBoxTimer.reset(); 172 173 switch (event.name) { 174 case "Beep": 175 _msg.text = "Beep" 176 _container.addChild(_msg); 177 _beep.play(); 178 break; 179 180 case "Double Beep": 181 _beep.play(0, 2); 182 break; 183 184 default: 185 trace("Unknown action: " + event.name); 186 break; 187 } 188 _chatBoxTimer.start(); 189 } 190 191 /** The image we're displaying. */ 192 protected var _image :DisplayObject; 193 194 /** Our beep sound. */ 195 protected var _beep :Sound; 196 197 /** The avatar control interface. */ 198 protected var _control :AvatarControl; 199 200 /** Controls the timing of our speak action. */ 201 protected var _speakTimer :Timer; 202 203 /** Controls the timing of our chat box. */ 204 protected var _chatBoxTimer :Timer; 205 206 /** Message variable and a container for the message */ 207 protected var _msg :TextField; 208 protected var _container :Sprite; 209 210 /** Are we bouncing? */ 211 protected var _bouncing :Boolean; 212 213 /** The timestamp at which we started bouncing. */ 214 protected var _bounceStamp :Number; 215 216 /** The amplitude of our bounce, in pixels. */ 217 protected static const BOUNCE_AMPLITUDE :int = 10; 218 219 /** The period of our bounce: we do one bounce every 500 milliseconds. */ 220 protected static const BOUNCE_PERIOD :int = 500; 221 222 /** The embedded image class. */ 223 [Embed(source="Rooster.png")] 224 protected static const IMAGE :Class; 225 226 [Embed(source="beep.mp3")] 227 protected static const BEEP :Class; 228 } 229 230 } // END: package </actionscript>