forked from destroytoday/DestroyFramework
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKonamiCode.as
More file actions
247 lines (214 loc) · 5.35 KB
/
KonamiCode.as
File metadata and controls
247 lines (214 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.destroytoday.desktop
{
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.events.TimerEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
import org.osflash.signals.Signal;
/**
* KonamiCode is a class to easily listen for the Konami code (up, down, up, down, left, right, left right, b, a, enter)
* @author Jonnie Hallman
*/
public class KonamiCode
{
//--------------------------------------------------------------------------
//
// Signals
//
//--------------------------------------------------------------------------
/**
* Dispatched upon successful entry of the Konami code
*/
public const executed:Signal = new Signal();
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
/**
* @private
* The correct Konami code combination
*/
protected const correctKeyCombination:Array =
[
Keyboard.UP,
Keyboard.UP,
Keyboard.DOWN,
Keyboard.DOWN,
Keyboard.LEFT,
Keyboard.RIGHT,
Keyboard.LEFT,
Keyboard.RIGHT,
Keyboard.B,
Keyboard.A,
Keyboard.ENTER
];
/**
* @private
* The user-inputted key combination
*/
protected var currentKeyCombination:Array = [];
/**
* @private
* The timer that controls the key-entry threshold
*/
protected var thresholdTimer:Timer = new Timer(1000.0, 1.0);
/**
* @private
*/
protected var _enabled:Boolean = true;
/**
* @private
*/
protected var _stage:Stage;
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
/**
* Constructs the KonamiCode class
* @param stage the stage to listen for key entry
* @param threshold the time in milliseconds allowed for key-entry before resetting
*
*/
public function KonamiCode(stage:Stage = null, threshold:Number = 1000.0)
{
this.stage = stage;
this.threshold = threshold;
thresholdTimer.addEventListener(TimerEvent.TIMER_COMPLETE, thresholdTimerCompleteHandler);
}
//--------------------------------------------------------------------------
//
// Getters / Setters
//
//--------------------------------------------------------------------------
/**
* Specifies whether or not the Konami code is enabled
* @return
*/
public function get enabled():Boolean
{
return _enabled;
}
/**
* @private
* @param value
*/
public function set enabled(value:Boolean):void
{
if (value == _enabled) return;
_enabled = value;
if (_stage && _enabled)
{
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
else if (_stage && !_enabled)
{
_stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
}
/**
* The stage to listen for key entry
* @return
*/
public function get stage():Stage
{
return _stage;
}
/**
* @private
* @param value
*/
public function set stage(value:Stage):void
{
if (value == _stage) return;
if (_stage)
{
_stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
_stage = value;
if (_stage && _enabled)
{
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
}
/**
* The time in milliseconds allowed for key-entry before resetting
* The minimum value is 100 milliseconds
* @return
*/
public function get threshold():Number
{
return thresholdTimer.delay;
}
/**
* @private
* @param value
*/
public function set threshold(value:Number):void
{
if (value == thresholdTimer.delay) return;
thresholdTimer.delay = value < 100.0 ? 100.0 : value;
}
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Resets the key entry
*/
public function reset():void
{
currentKeyCombination.length = 0;
thresholdTimer.reset();
}
//--------------------------------------------------------------------------
//
// Handlers
//
//--------------------------------------------------------------------------
/**
* @private
* @param event
*/
protected function keyUpHandler(event:KeyboardEvent):void
{
// ignore any key presses that have modifiers
if (event.ctrlKey || event.shiftKey || event.altKey) return;
// if the key pressed is the next one in the combo
if (event.keyCode == correctKeyCombination[currentKeyCombination.length])
{
// if it's the last key (previously confirmed as correct)
if (currentKeyCombination.length == correctKeyCombination.length - 1)
{
executed.dispatch();
reset();
}
// if it's not the last key in the combo, move forward and restart the timer
else
{
currentKeyCombination[currentKeyCombination.length] = event.keyCode;
thresholdTimer.reset();
thresholdTimer.start();
}
}
// wrong key pressed
else
{
reset();
}
}
/**
* @private
* @param event
*/
protected function thresholdTimerCompleteHandler(event:TimerEvent):void
{
// too slow
reset();
}
}
}