|
-
[F8] User input text questions..
I have a little program where the user will input a question in an input text box and click a submit button. Based on the question asked a dynamic text box will answer it.
right now it is set up something like:
user: What is your name?
Flash: My name is Jimmy.
I have a couple questions to help make this better, as everything "works" right now...
1) What can I do to make the user input text transform to all lowercase letters, so that if they type: "WHAT is YOUr namE?" Flash will see: "what is your name?" ?
2) Is there a way to set an array up full of questions AND answers? Right now I am using a metric butt ton of if/else statements. I would prefer to use less code. I thought of an array with questions and another with answers, and if input text == questionArray[4] dynamic text will show answerArray[4] and so on, is that possible?
3) What about a key word criteria, instead of exacts? Like right now the question "What is your name?" MUST be entered exactly like that to generate the response. But some people talk differently, so a user might type "what's your name?" or "do you have a name?" Is there some way I can have Flash ignore certain words (What, Do, How, your, etc...) and focus only on the rest?
I know some of these (#3) may be very complicated, or impossible, but it never hurts to ask, right?
Thanks for any/all help with this!
~MoN
If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN
-
anyone else hear that?
1) use myString.toLowerCase() to convert a string to all lowercase
2) absolutely possible...seems like this would be the right way to handle this vs. using if..then calls
3) whew...you could convert the input string to an array using myString.split(" ") and then test each item in a for loop against an array of keywords and then have those keywords cue a particular "answer". That would give you the most flexibility, but would require the largest amount of data to make sure you didn't get too many null matches.
Hope that helps!!
Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.
-
Well, that does help... now how to impliment?
I had the ideas, I just don't know exactly what to do with them...
1) use myString.toLowerCase() is "myString" a generic name like I would change that to whatever I have the input box text set to (currently question.text) or leave it?
I picture something like:
[code]
on (release) {
this.gotoAndStop(1);
myString.toLowerCase();
if (_root.question.text == "What is your name?") {
_root.answer.text = "My name is Jimmy.";
} else {
_root.answer.text = "Sorry, that is not a good question.";
}
}
[code]
2) Arrays give me fits (hence the if/else) How would I set that up?
Code:
Array = questionArray["what is your name?", "what is your favorite color?", "what is your quest?"];
Array = answerArray["Arthur, King of the Britians.", "Blue.", "I seek the Holy Grail."];
on(release){
if (_root.question.text == questionArray[//insert some formula for i here]){
_root.answer.text = answerArray[//same i formula];
}
3) We can ignore that one for now! 
Laughable, I know... but I am learning!
Thanks for the help!
~MoN
If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN
-
anyone else hear that?
Not laughable at all...we're all learning here...

You're pretty close on the code...I attached a little sample that cleans it up a little.
Basically, I set it up using movieclips instead of buttons (I generally avoid buttons and scenes...as a rule).
I tried to put comments in there to help some...I set up the arrays using keywords loosely based on your arrays...
Code:
//sets up the two arrays
var questionArray:Array = ["name", "color", "quest"];
var answerArray:Array = ["Arthur, King of the Britians.", "Blue.", "I seek the Holy Grail."];
//sets up the blank string
var lowerCaseString:String;
//sets up the blank array
var lowerCaseArray:Array=new Array();
//sets up the boolean "found" variable
var found:Boolean;
//when you release the button movie clip
btn_mc.onRelease = function() {
//converts the input to lower case
lowerCaseString = input_txt.text.toLowerCase();
//breaks each word in the sentence into a separate array entry
lowerCaseArray = lowerCaseString.split(" ");
//set the default found variable
found = false;
//loop each word in the array through each item in the question array to see if it matches
for (i=0; i<lowerCaseArray.length; i++) {
for (j=0; j<questionArray.length; j++) {
if (lowerCaseArray[i] == questionArray[j]) {
//if it matches, put the corresponding answer in the field
this._parent.answer_txt.text = answerArray[j];
found = true;
}
}
}
//if there's no match, give the default response
if (!found) {
this._parent.answer_txt.text = "Sorry, that is not a good question.";
}
};
Hope that helps!!
Last edited by flashpipe1; 09-10-2007 at 01:37 PM.
Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.
-
Great! I guess I should have mentioned, I am using MCs too, not buttons (I feel the same way)
I will take a peek and see what I can make happen! Thanks again!
Maybe once this is all done and working, I will get back with you (via PM?) to work out how to do #3...
~MoN
If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN
-
anyone else hear that?
Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.
-
AKKK! I noticed that the script works great unless you use punctuation!
If I enter: "What is your favorite color?" I get the false response.
If I enter: "What is your favorite color" I get "Blue."
I can't expect to ask people to type a question and not put a question mark, heh.
Is there a way to fix this? Otherwise it is perfect.
If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN
-
anyone else hear that?
Hmm...I suppose, since you know the punctuation (question mark) will be at the end, you could do something like:
Code:
for (i=0; i<lowerCaseArray.length; i++) {
for (j=0; j<questionArray.length; j++) {
testWord = lowerCaseArray[i]
if (i == lowerCaseArray.length-1) {
trace(testWord.length);
trace(testWord.substr(testWord.length-1, 1));
if(testWord.substr(testWord.length-1, 1)=="?"){
testWord=testWord.substr(0,testWord.length-1)
trace(testWord);
}
}
if (testWord == questionArray[j]) {
//if it matches, put the corresponding answer in the field
this._parent.answer_txt.text = answerArray[j];
found = true;
break;
}
}
}
Hope that helps!!
Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.
-
That did the trick! And I even think I understand the set up! I am going to try to write this myself now with my own real questions and answers and see what happens!
Thanks again for the assistance!
~MoN
If I am wrong, please just correct me and move on.. there is no need for all that pointing and laughing! ~MoN
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|