(AS2) Coming back to Flash to relearn some code, and could use some help!

Hello to everyone on the board!

First of all, I’ve been using Kirupa for a while to learn and re-learn the basics of Flash again, and I just want to say thanks for everything!

Onto the questions I have. I’m positive some of these are going to be incredibly basic, but these few questions will just point me in the right direction so I can finish up a interactive animation I’m working on.

And, I know that AS2 is completely out of date, but I’m fairly comfortable with it and just want to relearn some basics.

First:

For the Flash, I want to make it so that when you type a certain phrase into the DynamicText/InputText box, it will show a different phrase in that same box.

Example: If I type in “Hello” and press Enter, I want that same box to erase the text there and instead display something like “How are you?”

It seems simple, but I’ve been poking around for an exact script and I can’t find one anywhere.

Second:

This box at the moment has a Submit button underneath that when you press it, it detects what you’ve typed in and takes you to an appropriate frame in the animation if what you typed matches what is in the Text Box. However, I realized that just using this:

if (password_txt.text == “help”) {
gotoAndStop(999);}

makes it so that it goes to the proper frame, but it has to match the lower cased letters as well. Is there a method I can use where it detects both Upper and Lower cased letters for the input?

Third, and last question:

I know there’s a way around this with AS3, but I also need to freshen up on Listeners as well, so I figured I could best ask this here too.

With that same “Submit” button I have made above, it has an instance name of “submit_btn”. I also set it so that it’s:

on (release, keyPress “”) {

Of course, pushing enter does nothing, as I know that the actual frame needs a listener set to it as well to acknowledge the key being pressed.

Essentially, I just want some help with making it so that either pressing the “Submit” button or hitting your “Enter” key does the same thing, and what to use to make it acknowledge the Enter key being released (KeyUp).

And those are my questions! Again, they come off as very basic, but it’s been years since I’ve properly used Flash and just want a refresher on these specific actions.

Thanks as always for your guy’s help!

I’d reccoment doing all your coding in the timeline instead of on symbols. Something like this might work (though it’s been a while since I’ve coded anything):

function onKeyUp() {
    if(Key.getCode() == Key.ENTER) submit_btn.onPress()
}
submit_btn.onPress = function() {
    if(password_txt.text.toLowerCase() == "help") gotoAndStop(999)
}

Thanks for the reply!

I did try the method you provided in various ways (Changed the onPress to onRelease, made sure the names matched up for the symbols, and even changed it to the keycode instead of using key.ENTER for it), and it didn’t function, neither on the symbols or the timeline itself.

I tried a few variations with “toLowerCase” as well, and it didn’t seem to acknowledge or turn the inputted text lower cased either.

I’ll do some other permutations with these as well to give them a shot, and I’ll report back if I have any success. Thanks again!

Try starting from scratch with a new FLA. Put one movie clip (with something drawn in it) on the stage with instance name submit_btn and one input text field with instance name password_txt. Then put just this code in the timeline:
submit_btn.onPress = function() {
trace(password_txt.text)
}

Test the movie, type something in the text field and press the button. You should get whatever you typed in the output from the trace. Does that work?

1 Like

Yes, that worked perfectly! I think I might be seeing where I had the problem at. I’ll message back with the results.

Alright I’m back! After doing some testing with the original .fla I was working on it will always acknowledge the password when I type into it and hit the “Submit” button I created, but hitting the ‘Enter’ Key still does nothing, no matter what I do.

To get around the second problem, I just decided to use a “textField.restrict = “a-z”;” to force password_txt to only output in lowercase, so that resolved that problem, but I am still curious on if there’s a way I can have it recognize what I put in and not care about if the character is Upper and Lower cased. Not as huge of a priority for what I’m doing, but it still works fine.

The big problem still is getting the “Submit” button to have the same function when I press the ‘Enter’ key on the keyboard. Again, I know that it’s a simple thing to do, but I’m having the hardest time with it for seemingly no reason, especially since the code you provided should work, even with AS2.

Again, thanks for the help you’ve been giving so far. It’s been a huge success and I’m jumping back and forth doing a ton of different things again.

EDIT:

I’ve got it! I just decided to use a listener, and just using this:

var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) submit_btn.onRelease();
}
Key.addListener(keyListener);

in the timeline got the result I needed for pressing Enter, so now it’s detecting both!

One last question, and this one is going to be a tough one too:

Is there any way that I can have a text box respond to just one specific word and ignore any other words/characters put in it? For example, if I typed in “Hello, my name is Tyjan!”, it will only look at “Tyjan” and respond to that, but not if anything else is put in?

I’m not entirely hung up on getting this one resolved, as I’m positive I’ll be able to wrangle up something out of what I’ve been doing for the past 2 hours, but I’m just looking through everything here again and thought I’d give it a shot.

Thanks again!

Calling toLowerCase on the text field’s text property should work to make the comparison case insensitive. For an example, try this in an empty movie:

trace(new String("fOo BAR BoO far").toLowerCase()) //foo bar boo far

Be careful with onKeyDown because it will be called continuously while the key is pressed. onKeyUp may be a better option.


As for your last question, it would be so much more robust if using AS3 with regex. But, in AS2, you can rig something up with the various String methods. Something like this might work:

trace(new String("Hello, my name is Tyjan!").indexOf("Tyjan") != -1)) //true
trace(new String("Hello, my name is John!").indexOf("Tyjan") != -1)) //false

PS: If you don’t know about the language reference, you should check it out: http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=Part2_AS2_LangRef_1.html

Awesome as always! All of the above worked and more! I even used an index search to find specific words by using search commands combined with that, so this has all been super helpful.

Thanks again for the help, and for the link! It’s certainly been very useful, and I’m positive this answers all my questions.

If anything else comes up, I’ll post again, but I think this topic can be safely closed.

Fantastic work as always!