Log in

View Full Version : Actionscript 2.0 question



kelsobrooks
05-20-2008, 04:48 PM
Hello all...

I have been playing around with 2.0 actionscript to make some simple learning activities / games. I am wondering how and if you can use a input text box to take several characters and then if they are correct make another movie clip play and goto another frame, etc.

For example, I am making a quiz and I want a specific movie clip to advance if the user gets the correct answer for calculation that they have put into a input text box.


Thanks for any help or guidance...:)

KB

Medyman
05-21-2008, 12:47 PM
Hey KB,

To target the text within a input text field you have to utilize the textfield.text property. You'll then evaluate that string value against a hardcoded string value or a string type variable.

So, if you had an input text field with an instance name of "theAnswer", and the correct answer to your question/prompt was "Yellow", you might code something like this:


if(theAnswer.text == "Yellow") {
movieclip.gotoAndPlay("frame label");
}

The highlighted bits are all filler text/variables. You would need to substitute your own, of course. This method works great if you're only evaluating one question/answer. If you have any more, you'll end up with a ton of repetitive code.

For multiple uses, I suggest that you create a dynamic function that you can call any time you need to do this kind of evaluation. Let's take the same example as above but this time add a submit button with an instance name of "submit". Your code might look like this:



var answer:String = "Yellow";
submit.onRelease = checkAnswer;

function checkAnswer() {
if(theAnswer.text == answer) {
movieclip.gotoAndPlay("frame label");
}
}

Using this, you would only need to change the value of the answer variable, not rewrite the entire function. To reset the variable, you could attach it to the submit button:


submit.onRelease = function() {
answer = "Yellow";
checkAnswer();
}

or if you're using the timeline (e.g. 1 frame per question), you could declare the variable at the top of each frame.

In case it's hard to grasp what I'm talking about in the abstract, here (http://www.visualbinary.net/files/tutorials/flash-quiz/) are some "real-life" examples. They're not really very usable -- once you submit an answer, you'll have to refresh the page to re-enter a value. There are two types of questions: 1) a text input as you asked about in your question and for good measure 2) a bunch of radio buttons. The ActionScripting differs in both of these examples as the technique to evaluate radio buttons vs. text field strings is different. The Flash 8 and Flash CS3 sources are included on the page and released under Creative Commons NC.

Hope that helps. If you have any more questions, feel free to post.