Doing the Konami Code with AS3
December 23rd, 2010For a project I was working on, I wanted to the ability to enter a secret string of keys on the Keyboard (e.g. the Konami code). Here’s the class to be able to do just that.
package
{
import flash.display.*;
import flash.ui.*;
import flash.utils.*;
import flash.events.*;
/**
* This class is for creating secret codes
* entered on the keyboard (e.g. Konami Code)
* @author Matt Moore matt@cloudkid.com
*/
public class SecretCode extends EventDispatcher
{
/** Event when the code entered has succeeded */
public static const SUCCESS:String = "onSuccess";
/** Event when the code has been entered bad */
public static const FAILED:String = "onFailed";
/** Event dispatched when the code listening times out */
public static const TIMEOUT:String = "onTimeout";
/** We'll save the konami code, this is the default */
public static const KONAMI:Array = [
Keyboard.UP, Keyboard.UP,
Keyboard.DOWN, Keyboard.DOWN,
Keyboard.LEFT, Keyboard.RIGHT,
Keyboard.LEFT, Keyboard.RIGHT,
66, //B
65, //A
];
/** The stage to listen to keystrokes */
protected var __stage:DisplayObject;
/** The secret code (default here is Konami) */
protected var __code:Array;
/** The limit input timer, code times out */
protected var __timeoutTimer:Timer;
/** Record the key inputs */
protected var __keyRecord:Array = [];
/** Whether this is enabled or not */
protected var __enabled:Boolean;
/**
* Listen for secret codes
* @param obj The stage to listen to keystrokes on
* @param timeThreshold The number of ms you have to enter the time
* @param code The optional array of keyboard inputs to check for
*/
public function SecretCode(obj:DisplayObject, timeThreshold:int=5000, code:Array=null): void
{
__stage = obj;
__code = code == null ? KONAMI : code;
__timeoutTimer = new Timer(timeThreshold, 1);
enabled = true;
}
/**
* Enable or disable this
*/
public function set enabled(enabled:Boolean): void
{
__enabled = enabled;
__timeoutTimer.removeEventListener(TimerEvent.TIMER, onTimeout);
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
if (enabled)
{
__timeoutTimer.addEventListener(TimerEvent.TIMER, onTimeout);
reset();
}
}
/**
* Get the enabled status
*/
public function get enabled(): Boolean
{
return __enabled;
}
/**
* Entering the code has timed out from when you hit the first key
* @param ev Timer Event
*/
private function onTimeout(ev:TimerEvent): void
{
dispatchEvent(new Event(TIMEOUT));
reset();
}
/**
* This resets listening for a code
*/
private function reset(): void
{
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__stage.addEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__keyRecord = [];
__timeoutTimer.reset();
}
/**
* When the user starts inputting on the keyboard
* @param Keyboard event key down
*/
private function onStartCodeInput(ev:KeyboardEvent): void
{
// The first key must be entered correctly to start listening
if (ev.keyCode != __code[0]) return;
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.addEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__timeoutTimer.start();
}
/**
* Record the keyboard inputs
* @param ev Keyboard event key up
*/
private function onCodeInput(ev:KeyboardEvent): void
{
__keyRecord.push(ev.keyCode);
if (__keyRecord.length < __code.length)
{
return;
}
if (__keyRecord.length > __code.length)
{
dispatchEvent(new Event(FAILED));
reset();
return;
}
if (__keyRecord.toString() == __code.toString())
{
dispatchEvent(new Event(SUCCESS));
reset();
return;
}
}
/**
* Destroy this class. Don't use after this
*/
public function destroy(): void
{
__timeoutTimer.removeEventListener(TimerEvent.TIMER, onTimeout);
__timeoutTimer.reset();
__timeoutTimer = null;
__keyRecord = null;
__code = null;
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__stage = null;
}
}
}
{
import flash.display.*;
import flash.ui.*;
import flash.utils.*;
import flash.events.*;
/**
* This class is for creating secret codes
* entered on the keyboard (e.g. Konami Code)
* @author Matt Moore matt@cloudkid.com
*/
public class SecretCode extends EventDispatcher
{
/** Event when the code entered has succeeded */
public static const SUCCESS:String = "onSuccess";
/** Event when the code has been entered bad */
public static const FAILED:String = "onFailed";
/** Event dispatched when the code listening times out */
public static const TIMEOUT:String = "onTimeout";
/** We'll save the konami code, this is the default */
public static const KONAMI:Array = [
Keyboard.UP, Keyboard.UP,
Keyboard.DOWN, Keyboard.DOWN,
Keyboard.LEFT, Keyboard.RIGHT,
Keyboard.LEFT, Keyboard.RIGHT,
66, //B
65, //A
];
/** The stage to listen to keystrokes */
protected var __stage:DisplayObject;
/** The secret code (default here is Konami) */
protected var __code:Array;
/** The limit input timer, code times out */
protected var __timeoutTimer:Timer;
/** Record the key inputs */
protected var __keyRecord:Array = [];
/** Whether this is enabled or not */
protected var __enabled:Boolean;
/**
* Listen for secret codes
* @param obj The stage to listen to keystrokes on
* @param timeThreshold The number of ms you have to enter the time
* @param code The optional array of keyboard inputs to check for
*/
public function SecretCode(obj:DisplayObject, timeThreshold:int=5000, code:Array=null): void
{
__stage = obj;
__code = code == null ? KONAMI : code;
__timeoutTimer = new Timer(timeThreshold, 1);
enabled = true;
}
/**
* Enable or disable this
*/
public function set enabled(enabled:Boolean): void
{
__enabled = enabled;
__timeoutTimer.removeEventListener(TimerEvent.TIMER, onTimeout);
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
if (enabled)
{
__timeoutTimer.addEventListener(TimerEvent.TIMER, onTimeout);
reset();
}
}
/**
* Get the enabled status
*/
public function get enabled(): Boolean
{
return __enabled;
}
/**
* Entering the code has timed out from when you hit the first key
* @param ev Timer Event
*/
private function onTimeout(ev:TimerEvent): void
{
dispatchEvent(new Event(TIMEOUT));
reset();
}
/**
* This resets listening for a code
*/
private function reset(): void
{
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__stage.addEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__keyRecord = [];
__timeoutTimer.reset();
}
/**
* When the user starts inputting on the keyboard
* @param Keyboard event key down
*/
private function onStartCodeInput(ev:KeyboardEvent): void
{
// The first key must be entered correctly to start listening
if (ev.keyCode != __code[0]) return;
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.addEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__timeoutTimer.start();
}
/**
* Record the keyboard inputs
* @param ev Keyboard event key up
*/
private function onCodeInput(ev:KeyboardEvent): void
{
__keyRecord.push(ev.keyCode);
if (__keyRecord.length < __code.length)
{
return;
}
if (__keyRecord.length > __code.length)
{
dispatchEvent(new Event(FAILED));
reset();
return;
}
if (__keyRecord.toString() == __code.toString())
{
dispatchEvent(new Event(SUCCESS));
reset();
return;
}
}
/**
* Destroy this class. Don't use after this
*/
public function destroy(): void
{
__timeoutTimer.removeEventListener(TimerEvent.TIMER, onTimeout);
__timeoutTimer.reset();
__timeoutTimer = null;
__keyRecord = null;
__code = null;
__stage.removeEventListener(KeyboardEvent.KEY_UP, onCodeInput);
__stage.removeEventListener(KeyboardEvent.KEY_DOWN, onStartCodeInput);
__stage = null;
}
}
}
Here’s an example of implementing the SecretCode object. In the constructor you can pass in your secret code into the constructor as an array of keyCodes. If you don’t specify anything, like below, it uses the Konami Code.
var secret:SecretCode = new SecretCode(stage, 3000);
secret.addEventListener(SecretCode.SUCCESS, onSuccess);
function onSuccess(event:Event): void
{
trace("You have entered the secret code correctly!");
}
secret.addEventListener(SecretCode.SUCCESS, onSuccess);
function onSuccess(event:Event): void
{
trace("You have entered the secret code correctly!");
}