Skip to content

Instantly share code, notes, and snippets.

@og2t
Created March 2, 2010 09:51
Show Gist options
  • Save og2t/319402 to your computer and use it in GitHub Desktop.
Save og2t/319402 to your computer and use it in GitHub Desktop.
Trigger action by typing a trigger word
/*---------------------------------------------------------------------------------------------
[AS3] CheatTrigger
=======================================================================================
Copyright (c) 2009 Tomek Augustyn aka Og2t
All Rights Reserved.
VERSION HISTORY:
v0.1 Born on 20/05/2009
USAGE:
TODOs:
DEV IDEAS:
- make it singleton
KNOWN ISSUES:
---------------------------------------------------------------------------------------------*/
package
{
// IMPORTS ////////////////////////////////////////////////////////////////////////////////
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
// CLASS //////////////////////////////////////////////////////////////////////////////////
public class CheatTrigger extends EventDispatcher
{
// CONSTANTS //////////////////////////////////////////////////////////////////////////
// MEMBERS ////////////////////////////////////////////////////////////////////////////
private var stage:Stage;
private var keystrokes:String;
private var triggers:Object;
// CONSTRUCTOR ////////////////////////////////////////////////////////////////////////
public function CheatTrigger(stage:Stage)
{
this.stage = stage;
keystrokes = "";
triggers = {};
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
}
// PUBLIC METHODS /////////////////////////////////////////////////////////////////////
public function dispose():void
{
for each (var trigger:Object in triggers) removeEventListener(trigger.type, trigger.listener);
if (stage) stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage = null;
triggers = null;
keystrokes = null;
debug("disposal of CheatTrigger complete");
}
override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = true):void
{
debug("adding listener for: " + type);
super.addEventListener(type, listener, useCapture, priority, useWeakReference);
if (!triggers.hasOwnProperty(type)) triggers[type] = {
type: type,
listener: listener
};
}
override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
{
debug("removing listener for: " + type);
super.removeEventListener(type, listener, useCapture);
delete triggers[type];
}
// PRIVATE METHODS ////////////////////////////////////////////////////////////////////
// EVENT HANDLERS /////////////////////////////////////////////////////////////////////
private function keyDownHandler(event:KeyboardEvent):void
{
keystrokes += String.fromCharCode(event.keyCode).toLowerCase();
for each (var trigger:Object in triggers)
{
if (keystrokes.indexOf(trigger.type) != -1)
{
dispatchEvent(new Event(trigger.type));
keystrokes = "";
}
}
}
// GETTERS & SETTERS //////////////////////////////////////////////////////////////////
// HELPERS ////////////////////////////////////////////////////////////////////////////
private function debug(string:String):void
{
trace("[CheatTrigger]", string);
}
}
}
/*---------------------------------------------------------------------------------------------
[AS3] CheatTriggerDemo
=======================================================================================
Copyright (c) 2009 Tomek Augustyn aka Og2t
All Rights Reserved.
VERSION HISTORY:
v0.1 Born on 20/05/2009
USAGE:
TODOs:
DEV IDEAS:
KNOWN ISSUES:
---------------------------------------------------------------------------------------------*/
package
{
// IMPORTS ////////////////////////////////////////////////////////////////////////////////
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
// CLASS //////////////////////////////////////////////////////////////////////////////////
public class CheatTriggerDemo extends Sprite
{
// CONSTANTS //////////////////////////////////////////////////////////////////////////
// MEMBERS ////////////////////////////////////////////////////////////////////////////
private var cheatTrigger:CheatTrigger;
// CONSTRUCTOR ////////////////////////////////////////////////////////////////////////
public function CheatTriggerDemo(stageInit:Boolean = true)
{
if (stageInit) addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
// PUBLIC METHODS /////////////////////////////////////////////////////////////////////
// PRIVATE METHODS ////////////////////////////////////////////////////////////////////
public function init(event:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
cheatTrigger = new CheatTrigger(stage);
cheatTrigger.addEventListener("god", enableGodMode);
}
private function enableGodMode(event:Event):void
{
trace("GOD mode on");
cheatTrigger.removeEventListener("god", enableGodMode);
cheatTrigger.addEventListener("skip", skipSomething);
cheatTrigger.addEventListener("stat", toggleStats);
cheatTrigger.addEventListener("kill", killCheatTrigger);
cheatTrigger.addEventListener("fps", function():void
{
trace("showFPS");
});
}
private function skipSomething(event:Event):void
{
trace("skip something");
}
private function toggleStats(event:Event):void
{
trace("toggleStats");
}
private function killCheatTrigger(event:Event):void
{
cheatTrigger.dispose();
}
// EVENT HANDLERS /////////////////////////////////////////////////////////////////////
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment