PDA

View Full Version : text commands



typh_
March 10th, 2009, 12:09 PM
Hi all,

I would like to make a text command "terminal" similar to one found in that game:

http://chat.kongregate.com/gamez/0003/7440/live/loader.swf?kongregate_game_version=1235240359

I'm guessing that i need to play around with IndexOf() function?? Unfortunately I dont feel that comfortable with scripting yet :/

Any help welcome!

mpelland
March 10th, 2009, 12:21 PM
haha, interesting game

you could do straight comparisons
you could do terminalString.indexOf(targetString) != -1
you could break apart what the user is writing (using split) and run through the array to compare individual words. it all depends what you are looking for

if you are looking at something more like a traditional terminal window, i would recommend a combination of the options above. ie: ipconfig -a

you could take that string into a variable... var terminal:String = "ipconfig -a";
then break it into an array base on what your criteria is for separating arguments... var args:Array = terminal.split(" ");
and then you can run through the array and do your comparisons ..
for (var i:Number = 0; i < args.length; i++) {
// do test here
}

typh_
March 11th, 2009, 11:30 AM
ok it makes some sense but i would be grateful for a bit more information ;>
my idea is to insert input text box and add key listener to it, when user presses "enter" key script runs through typed text in search for keywords...main problem is how to translate it to AS ;/

BenBart
March 11th, 2009, 11:55 AM
ok it makes some sense but i would be grateful for a bit more information ;>
my idea is to insert input text box and add key listener to it, when user presses "enter" key script runs through typed text in search for keywords...main problem is how to translate it to AS ;/

That game rocks. I'll give you more code in a second if someone else doesn't get to it first.

BenBart
March 12th, 2009, 02:25 AM
//There is a input text box on the stage called "terminal"
terminal.addEventListener(KeyboardEvent.KEY_DOWN, keypressed);

function keypressed(e:KeyboardEvent){
//if the key that was pressed is Enter call the readText function
if(e.keyCode == 13){
readText();
}
}

function readText(){
/*define an array. Then declare a value inside of that array.
This is the text inside of "terminal". It is placed into array by spliting
where ever there are spaces
var currentTerminalText:Array = terminal.text.split(" ");*/

trace(currentTerminalText);
//Then we look through all the words in the array;
for(var i=0;i<currentTerminalText.length; i++){
/*We have a case statement for the current word
This detects if any of the key words are said
if you have the same funtion for words with the same meaning
you can handle it the same way I handled the words "poo" and "pee" */
switch(currentTerminalText[i-1]){
case "poo":
case "pee":
trace("Poo or Pee was typed in.");
break;

case "pants":
trace("Something about pants");
break;

default:
trace("none of the words are found");
break;
}
}
}This is the basic concept. When you get really into it, things can be a headache. Just to warn you.

typh_
March 13th, 2009, 04:21 PM
Thanks a lot!!! I catch your drift, so where f ex i can trigger some actions if proper text is typed?