V 0.3
Big additions added before the final V1 release, you can now simply addCheat and remCheat.
also, this version (and post) are fully documented, mods, if you read this, please remove the last 3 posts by me.
common uses:
was expression typed?
cheats
other nifty stuff
since V0.03 you can use a simpler form of code and do
i = new KeyQueue(20);//20 is the length
i.listen();//starts listening to to the keys
i.setCheat("heyz", function () { //sets heyz as the cheat, and when it triggers, launches
//"function", notice that there are only 2 params, simply add ,true if you want it to only
//trigger once, read the setCheat docs for more instructions
trace("heyz"+random(50));
});
Deprecated instructions: (works)
create a new instance of the class by typing
my_keyqueue=new KeyQueue(<max length>);
//the longer the max is the bigger cheats it can contain and the more usage it takes
my_keyqueue.listen();//starts key queuing
//actual checking for cheats
this.onEnterFrame=function(){/*if in movieclip from this point on put in onClipEvent(enterFrame) /*
if(my_keyqueue.cheatCheck("hey lolz"){//replace hey lolz with your cheat
//actions that trigger when cheat is true go here
}
this is a pretty simple docs I whipper up:
NOTE: inherits from the MovieClip class to enable pathing (passing 'this' to a listener)
Properties:
innerQueue :Array
this is the actual queue the data goes in and out of. this is encapsulated so apart from knowing it's there it's not really an interest
constructor:
KeyQueue(length:Number)
creates a new KeyQueue the length of the passed parameter
Functions:
function setCheat(cheat:String, f:Function[, once:Boolean])- sets a cheat for the current queue, the once parameter is optional and it determines if the cheat triggers once or not, by default, the cheat triggers constantly , the "f" parameter is a function, remmember not to sent the () but only the name, or use the function() creator, the cheat parameter is the actual cheat, for example you can do
function myFunc(){
_root.hero.gotoAndStop(5);
}
setCheat("43243,myFunc,true);
or simply
setCheat("43243",function(){
_root.hero.gotoAndStop(5);
},true);
function remCheat() - removes the cheat set with the last setCheat call.
function getLastKey():Number - returns the last typed key, pretty useless but it makes sense to have it there, type my_keyqueue.getLastKey(); to get the last user typed key
function listen() -starts the actual watching over the user keys, use this to activate the actual capture
function getKeyFromQueue(pos:Number):Number - returns the pos key typed, for example if you typed omg, getKeyFromQueue(17) would return 'o' I believe in a 20 sized queue,
function toString():String - returns the current KeyQueue as a string
function wasTyped(expression:String):Boolean - checks if the last expression typed is equal to the passed string for example wasTyped("hello") returns a boolean true if the last typed thing was "hello"
public function cheatCheck(expression:String):Boolean -identical to wasTyped
function stop():Boolean - call this function to stop updating the queue (stop listening), this function is useless if "listen" hasn't been called, it doesn't delete the queue, it just stops updating it, returns true if the stop call was successful
function reset():Boolean - resets the current queue and stops listening formatting the current queue, returns true if both queue is reformatted and listening is stopped
function flush():Boolean --resets the content of the queue, for example if you check if a cheat is true often you want the check to only trigger once, in such a case you often want to flush the KeyQueue
enjoy :)
(instructions: copy contents into a new file, call it KeyQueue.as , the name is IMPORTENT, put it in the same dir as your fla file, it will still be one .swf file)
This code is under the GNU GPL (edit it freely, distribute it freely, credit me)
dynamic class KeyQueue extends MovieClip {
//KeyQueue class V 0.3 by Inglor Sep 15th 2005
//Last update Sep 16th 2005
private var innerQueue:Array;
public function KeyQueue(leng:Number) {
innerQueue = new Array(leng);
for (i=0; i<leng; i++) {
innerQueue[i] = 65;
}
}
public function stop(Void):Boolean {
return delete _root.keylisten;
}
public function reset(Void):Boolean {
return this.flush() && this.stop();
}
public function getLastKey(Void):Number {
return Key.getAscii();
}
public function listen(Void):Void {
_root.keylisten = new Object();
_root.keylisten.creator = this;
_root.keylisten.onKeyDown = function(Void):Number {
this.creator.innerQueue.push(Key.getAscii(
));
return this.creator.innerQueue.shift();
};
Key.addListener(_root.keylisten);
}
public function getKeyFromQueue(pos:Number):Number {
return this.innerQueue[pos];
}
public function toString(Void):String {
s = new String();
for (var i = 0; i<this.innerQueue.length; i++) {
s += String.fromCharCode(this.innerQueue[i]);
}
return s;
}
private function qts(a:Array):String {
s = new String();
for (var i = 0; i<a.length; i++) {
s += String.fromCharCode(a[i]);
}
return s;
}
public function flush(Void):Boolean {
var templen:Number = this.innerQueue.length;
this.innerQueue = new Array();
for (var i = 0; i<templen; i++) {
this.innerQueue.push(65);
}
return Boolean(this.innerQueue.length);
}
public function wasTyped(expression:String):Boolean {
var loc:Number = this.innerQueue.length-expression.length;
s = this.qts(this.innerQueue).substr(loc, this.innerQueue.length);
return (s==expression);
}
public function cheatCheck(expression:String):Boolean {
return this.wasTyped(expression);
}
public function setCheat(cheat:String, f:Function, once:Boolean):Void {
_root.createEmptyMovieClip("KeyQueueCheatC
hecker", 532432);
KeyQueueCheatChecker.ch = cheat;
_root.KeyQueueCheatChecker.func = f;
KeyQueueCheatChecker.ref = this;
if ((arguments.length == 2) || ((arguments.length) == 3 && (once == false))) {
_root.KeyQueueCheatChecker.onEnterFrame = function() {
if (this.ref.wasTyped(this.ch)) {
this.func();
}};
} else if (arguments.length == 3 && (once == true)) {
_root.KeyQueueCheatChecker.onEnterFrame = function() {
if (this.ref.wasTyped(this.ch)) {
this.func();
this.ref.remCheat();
}};
}
}
public function remCheat():Void {
delete _root.KeyQueueCheatChecker.onEnterFrame;
_root.KeyQueueCheatChecker.removeMovieClip
();
}
}