Optimisation

I imagine this is a very trivial problem to solve but I’ve been out of the AS loop for a while and can’t think of how to do this. Basically, I have two textboxes. One is a dynamic textbox with the variable name “tlation” and the other is an input textbox with the variable name “input”.

When the user enters certain words or numbers into the input textbox, certain words appear in the dynamic textbox accordingly. I created this using if, else if, else.

The problem arises when I have a couple hundred words that need to be recognised (i.e. you enter one of them and another word appears in the dynamic textbox).

I thought it might be possible to do this with a prototype but I’m not sure. Here is the prototype I created but I don’t know how to implement it:
[AS]//Prototype to optimise translator
MovieClip.prototype.translate = function (x, y)
{
if (input == x)
{
tlation = y ;
}
} ;[/AS]

x is the word entered into the input textbox and y is the word that appears in the dynamic textbox.

As I understand it, I would need a movieclip in order to call the prototype and because there would be hundreds/thousands of possibilites, this does not appear to be the most efficient approach.

Any help is much appreciated,
JK.

I havnt tried this code but I have used it before, it should do the trick.

[AS]

switch (input) {
case word1:
tlation = “word1Answer”;
break;
case word2:
tlation = “word2Answer”;
break;
case word3:
tlation = “word3Answer”;
break;
default:
tlation = “No word Match”;
}

[/AS]

or just create 2 arrays, one of input texts, one of display texts, that’ll be faster than using switch

ok ok just because your smarter than me lol :stuck_out_tongue:

Ok… I haven’t really used arrays for anything like this in Flash so how would I go about getting it to search through the array of input texts for one which matches the one that was entered by the user and then displaying the relevant translation from the second array?

  • I would imagine that if they have the same key number, you could tell Flash to search the second array for the element with the same key number.

(Btw, if it is possible to use associative arrays in AS, you could just set the elements as the input texts and the keys as the translations (although, I don’t know how to search through the array and then display a certain part of it). If at all possible, I do not want to use PHP or other scripting languages… I wish to keep it all contained within the one .swf (just in-case somebody was thinking of suggesting that).

I would just use an object you could set it up like this:

translator = new Object();
translator[“input here”] = “translated text”;
translator[“another input here”] = “more translated text”;

and repeat that for all your translations

then, using an onChanged event of the textField, or really whatever means you chose to perform the check of inputs contents, you can just say something to the effect of

if (translator[input] != undefined) tlation = translator[input];

This is what i would do:
Create 2 textfields: one set as input text and another as dynamic text. Set them variables as “input” and “trans” respectively.(no quotes)
Now create a button and give it an instance name of “btn”
Place the following code on your first frame:
[AS]var words = [“one”, “two”, “three”, “four”, “five”];//input words
var twords = [“first”, “second”, “third”, “fourth”, “fifth”];//translate words
Array.prototype.translate = function(word) {
var found = false;
var i = 0;
while (i<this.length && found == false) {
if (word == this*) {
found = true;
trans = twords*;
} else {
i++;
trans = “not found!”;//error message
}
}
};
btn.onPress = function() {
words.translate(input.toLowerCase());
};[/AS]

claudio, I’m going to give your method a try in just one second. Sen, I was following you fine for the first bit with the object but you lost me with the “if (translator[input] != undefined) tlation = translator[input];”

Basically, I have the setup I explained in the initial post and it’s a 2 keyframe animation. The first frame has the AS (if and if else - i.e. it checks what the value of the input textbox is and then if it meets one of the words I have in one of the if/if else statements, it performs the function I specified in the {} of that statement) and then when you click a button (there is a basic button that responds to pressing Enter or being released), you are taken to keyframe 2. In keyframe 2, I just have “gotoAndStop(1) ;” to take you back to frame one. It works like a charm…

I think your approach with objects would be easier at a first glance so if you could go into more detail with the second part of that post, that would be great.

Meanwhile, I’m going to test out claudio’s method :).

Thanks so much for all this help :thumb:,
JK.

claudio, your way works perfectly :). No need to explain the objects thing for the moment Sen (just as well - I’m a slow learner when it comes to AS ;)). Thanks to everyone who helped me out with this - it was really bugging me :beam:.

translator is an object with variables in it all with names of your input phrases. The values of these variables is the translation of those phrases (as you defined them). Anytime you access one of these variables like with
trace(translator.word); // traces value of ‘word’ variable
or
trace(translator[“a phrase”]); // traces value of ‘a phrase’ variable
and you get that value. Now, if you try to access some variable that doesn’t exist, you get undefined out of that since… well its not defined
trace(translator[“doesnt exist”]); // traces undefined

so, that if statement just checks theres a value to the input variable before it assigns a value to tlation. You can easily make tlation say anything else if the variable or input doesnt exist, or even just clear it alltogether with an else

if (translator[input] != undefined) tlation = translator[input];
else tlation = “”;

welcome :beam:

Sen, your way seems interesting… havent thought about that.
:slight_smile:

Ahh, I understand what you meant now Sen. Thanks for explaining it, it sure does seem interesting :).