PDA

View Full Version : Adding cheats to game



thejoeknows.com
April 2nd, 2008, 10:00 PM
i would like to have it so if u hit a string of keys in a row a cheat is activated right now i have :



if (Key.isDown(Key.TAB)){
_root.points += 5;
}

Enix591
April 3rd, 2008, 02:41 PM
//Im guessing your using AS2.//

/*Copy this whole post into the first frame of a blank flash to see it in action. You also might want to AutoFormat it as well.*/

//I would create four arrays.
//one that would list all the names of all keys i want to check.
var KeyNameArray:Array = new Array("Right","Left","Up","Down")

//Another that would list the code values of those keys
var KeyCodeArray:Array = new Array(Key.RIGHT,Key.LEFT,Key.UP,Key.DOWN)

/*NOTE: for letters and other keys refer to the "Keyboard Keys and Key Code Values" in flash help*/

//I would create another array for the current combination.
var ComboArray:Array = new Array()

/*Then i would create the final array to list whether or not the keys previously stated were held down*/
var KeyDownArray:Array = new Array(false,false,false,false,false,false)

/*Next,I would create a 'ComboTimer' variable, this basically counts down to zero, and if the user doesnt press a key before it hits zero, it clears the combo array, but if they do, it adds that key to the array and resets it.*/
var ComboTimer=10

/*NOTE: THE FOLLOWING WOULD GO IN AN 'OnEnterFrame' FUNCTION*/

this.onEnterFrame=function(){


/* We would count down on the timer to zero, and once we reach it we clear the combination array*/
ComboTimer--
if(ComboTimer<=0)
{
ComboTimer=10
ComboArray=new Array()
}

/*Next i would have a 'for' statment that would check each key and see if it was being pressed, and if it was just pressed add it to the combo array, but if it is being held down do not.

Also, if we add a key to the array, we reset its timer so it doesnt unexpectadely run out and clear the array.*/

for (var i = 0; i < KeyNameArray.length; i++) {
var keyName = KeyNameArray[i]
var IsKeyDown = Key.isDown (KeyCodeArray[i]);
if (KeyDownArray[i] == false && IsKeyDown) {

ComboTimer=10
ComboArray.push (keyName);
trace(ComboArray)

}
KeyDownArray[i] = IsKeyDown;
}

//In the end you would get arrays such as where ComboArray equals "right","right","a" and such//

/*So convert that to a string then lets say you wanted to give the user 100 points when the press left right up down.*/

var ComboString:String= ComboArray.toString().toLowerCase()

if(ComboString=="left,right,up,down")
{


_root.points += 100;
ComboArray=new Array()
trace("Plus 100 points!")

}

// close the onEnterFrame function
}

Enix591
April 3rd, 2008, 02:53 PM
you could create a switch statement to test when certain combos are pressed

/* Add this on the last line of the onEnterFrame function,before the bracket.*/

TestKeyCombinations ()

/*Next add everything below this after the onEnterFrame function, after the bracket.

function TestKeyCombinations () {

// Test the lowercase form of the combo array as a string

switch (ComboArray.toString ().toLowerCase ()) {


case "left,right,up,down" :


_root.points += 10;
ComboArray = new Array ();
trace ("Plus 10 points!");
break;

case "right,right,right" :


_root.points += 20;
ComboArray = new Array ();
trace ("Plus 20 points!");
break;

case "left,left,left" :


_root.points += 20;
ComboArray = new Array ();
trace ("Plus 20 points!");
break;

case "left,right,up,up,down" :


_root.points += 1000;
ComboArray = new Array ();
trace ("Plus 1000 points!");
break;

}

}