Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to code a button to check all input fields at once?

New Here ,
Feb 14, 2013 Feb 14, 2013

I have made an interactive math problem that has the user go through about a dozen steps.  One of the steps requires the user to complete the problem on their own.  In order to fill out the problem, I have created multiple input text fields so that they can be filled with the correct answers.  In this problem, there are about 23 of these text fields.  After all of these inputs are filled in, I would like to find a way to display the correct answes with the click of a button.  Once, the button is clicked, I would like to have the correct answers to have a highlight of green or some other indicator behind it to indicate to the user that it is correct.  I have tried to find an if statement to statsify these conditions but I have not been successful at doing so.  In other words, I know what I want to do but I do not know how to tell actionscript 3.0 and flash to do so.  Could someone please help me in figuring this out?

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2013 Feb 14, 2013

if your input textfields have reference names like input_tf1,input_tf2,..,input_tf23, you can use array notation to loop through them:

var i:int;

for(i=1;i<=23;i++){

if(this["input_tf"+i].text==answerA){  // where answerA is an array of the correct answers

this["input_tf"+i].filters=[glowFilter];  // create this glow filter

}

}

or, you can list your input textfields in an array and loop through them:

tfA=[input_tf1,input_tf2,...,input_tf23];

for(i=1;i<=23;i++){

if(tfA.text==answerA){  // where answerA is an array of the correct answers

tfA.filters=[glowFilter];  // create this glow filter

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 15, 2013 Feb 15, 2013

I added my code that I am using now but obviously I do not want to go this certain route to check all the answers at once.  If you could please explain to me how some of your code would work as I only have basic experience with ActionScript. Also can I just Google how to create a glow filter?  I would appreciate your help.  Thank you

Code:

/*-------------show correct answers---------*/
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredo);
function onehundredo(e:Event):void {
       if (fourteenthinput.text == "0") {
    gotoAndStop("label100") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}

fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredone);
function onehundredone(e:Event):void {
       if (fifteenthinput.text == "40.25") {
    gotoAndStop("label101") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwo);
function onehundredtwo(e:Event):void {
       if (sixteenthinput.text == "0") {
    gotoAndStop("label102") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredthree);
function onehundredthree(e:Event):void {
       if (seventeenthinput.text == "-52.5") {
    gotoAndStop("label103") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfour);
function onehundredfour(e:Event):void {
       if (eighteenthinput.text == "26.25") {
    gotoAndStop("label104") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}
fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfive);
function onehundredfive(e:Event):void {
       if (nineteenthinput.text == "-78.75") {
    gotoAndStop("label105") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredsix);
function onehundredsix(e:Event):void {
       if (twentyinput.text == "0") {
    gotoAndStop("label106") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredseven);
function onehundredseven(e:Event):void {
       if (twentyoneinput.text == "0") {
    gotoAndStop("label107") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredeight);
function onehundredeight(e:Event):void {
       if (twentytwoinput.text == "0") {
    gotoAndStop("label108") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundrednine);
function onehundrednine(e:Event):void {
       if (twentythreeinput.text == "40.25") {
    gotoAndStop("label109") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredten);
function onehundredten(e:Event):void {
       if (twentyfourinput.text == "0") {
    gotoAndStop("label110") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredeleven);
function onehundredeleven(e:Event):void {
       if (twentyfiveinput.text == "0") {
    gotoAndStop("label111") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwelve);
function onehundredtwelve(e:Event):void {
       if (twentysixinput.text == "-385") {
    gotoAndStop("label112") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredthirteen);
function onehundredthirteen(e:Event):void {
       if (twentyseveninput.text == "-8.75") {
    gotoAndStop("label113") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfourteen);
function onehundredfourteen(e:Event):void {
       if (twentyeightinput.text == "227.5") {
    gotoAndStop("label114") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredfifteen);
function onehundredfifteen(e:Event):void {
       if (twentynineinput.text == "-175") {
    gotoAndStop("label115") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredsixteen);
function onehundredsixteen(e:Event):void {
       if (thirtyinput.text == "7") {
    gotoAndStop("label116") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredseventeen);
function onehundredseventeen(e:Event):void {
       if (thirtyoneinput.text == "140") {
    gotoAndStop("label117") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredeighteen);
function onehundredeighteen(e:Event):void {
       if (thirtytwoinput.text == "0") {
    gotoAndStop("label118") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundrednineteen);
function onehundrednineteen(e:Event):void {
       if (thirtythreeinput.text == "0") {
    gotoAndStop("label119") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwenty);
function onehundredtwenty(e:Event):void {
       if (thirtyfourinput.text == "40.25") {
    gotoAndStop("label120") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";


}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwentyone);
function onehundredtwentyone(e:Event):void {
       if (thirtyfiveinput.text == "268800") {
    gotoAndStop("label121") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwentytwo);
function onehundredtwentytwo(e:Event):void {
       if (thirtysixinput.text == "2450") {
    gotoAndStop("label122") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}fourteenthreset_btn.addEventListener(MouseEvent.CLICK, onehundredtwentythree);
function onehundredtwentythree(e:Event):void {
       if (thirtyseveninput.text == "97300") {
    gotoAndStop("label123") ;
} else {
  fourteenthresponse_txt.text = "Incorrect. Please try again.";
function fourteenthfocusInListener(e:FocusEvent) {
fourteenthinput.text = "";
fourteenthresponse_txt.text = "";

}
}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2013 Feb 15, 2013

you're checking 24 textfields with that code each time fourteenthreset_btn is clicked which doesn't make sense in your code.  in mine, it made sense.

in any case, you should be using arrays like i showed to condense that code to a few lines and you should explain what you're trying to check when that button is clicked.  or, did you mean to use a different button for each textfield?

also, you're nesting named functions which has no benefit and is always a bad idea.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 15, 2013 Feb 15, 2013

You are right! I would like to condense my code because it is very logn.  I would like to  check all textfields with the click of my fourteenthreset_btn.  Could you please explain to me where you created the array in your code? Do I need to create an array for each of my textfields? Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 17, 2013 Feb 17, 2013

To build a bit on kglad's suggestion.

First, you should rethink your naming conventions. Since everything in programming is number/integer based - you will be much better off if you replace input names from spelled out numbers to integers.

For example, fourteenthinput should be input_14; fifteenthinput - input_15, etc.

This way you can link different data containing object by integers - especially it is convenient with arrays.

If you do that - you base code can be something like example below. As you see - it takes less than 40 lines of code instead of hundreds.

Also, everything is dynamic and the only thing you will need to do if you add or remove input fields - rewrite correct answers array and adjust couple of initial parameters like numInputs. The rest of the code will take care of answers verification functionality automatically.

Read comments.

Also, never use nested functions - they are evil.

import flash.events.FocusEvent;

import flash.filters.GlowFilter;

import flash.text.TextField;

// grab input texts into sigle array

var answerInputs:Array = [];

// number of input fields

var numInputs:int = 24;

// this variable will be reused in the while loop

var input:TextField;

// loop assumes that text fields are name input_<int> from 0 on. For example input_0, input_1, input_15, etc.

while (numInputs--)

{

          input = this["input_" + numInputs];

          // add focus event listener

          input.addEventListener(FocusEvent.FOCUS_IN, onFocus);

          // add field to array

          answerInputs[numInputs] = input;

}

// array of correct answers - array elements are positioned in the same index as answerInputs elements

var solutions:Array = [0, 40.25, 0, -52.5, 26.25, -78.75, 0, 0, 0, 40.25, 0, 0, -385, -8.75, 227.5, -175, 7, 140, 0, 0, 40.25, 268800, 2450, 97300];

// assuming that button that invokes checking for answers is named checkForAnswers instead of fourteenthreset_btn

checkForAnswers.addEventListener(MouseEvent.CLICK, checkAnswers);

// glow filters

var greenGF:GlowFilter = new GlowFilter(0x00ff00);

var redGF:GlowFilter = new GlowFilter(0xff0000);

function checkAnswers(e:MouseEvent):void

{

          // reusable text field instance

          var inputField:TextField;

          for (var i:int = 0; i < answerInputs.length; i++)

          {

                    inputField = answerInputs as TextField;

                    /**

                     * set filter depending on whether answer is right or wrong -

                     * compare to solutions array element in the same position

                     */

                    inputField.filters = [Number(inputField.text) == solutions ? greenGF : redGF];

          }

}

// single focus listener takes care of all input fields

function onFocus(e:FocusEvent):void

{

          TextField(e.currentTarget).text = "";

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2013 Feb 15, 2013

if you want to check all the textfields use:

import flash.filters.GlowFilter;

var tfA:Array=[fourteenthinput,fifteeninput,...,thirtyseveninput];

var answerA:Array=[0,40.25,...,97300];

var greenGF:GlowFilter=new GlowFilter(0x00ff00);

var redGF:GlowFilter=new GlowFilter(0xff0000);

fourteenthreset_btn.addEventListener(MouseEvent.CLICK,checkF);

function checkF(e:MouseEvent):void{

for(var i:int=0;i<tfA.length;i++){

if(tfA.text==String(answerA)){

tfA.filters=[greenGF];

} else {

tfA.filters=[redGF];

}

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 17, 2013 Feb 17, 2013
LATEST

Also, you technically don't have to deal with placing text fields on timeline manually. The code below based on the code I showed previously with the exception that it creates inputs automatically based on numInputs value.

Just create new FLA, drop a regular button on the timeline and name this button checkForAnswers, and drop the code onto action layer.

import flash.events.FocusEvent;

import flash.filters.GlowFilter;

import flash.text.TextField;

import flash.text.TextFormat;

// grab input texts into sigle array

var answerInputs:Array = [];

// number of input fields

var numInputs:int = 10;

// this variable will be reused in the while loop

var input:TextField;

// menages y position

var nextY:Number = 10;

// create input fields dynamically

for(var i:int = 0; i < numInputs; i++)

{

          trace(numInputs);

          input = new TextField();

          input.type = "input";

          input.width = 50;

          input.height = 20;

          input.defaultTextFormat = new TextFormat("Arial", 12);

          input.border = true;

          input.multiline = input.wordWrap = false;

          input.y = nextY;

          input.x = 10;

          nextY += input.height + 4;

          addChild(input);

          // add focus event listener

          input.addEventListener(FocusEvent.FOCUS_IN, onFocus);

          // add field to array

          answerInputs.push(input);

}

// array of correct answers - array elements are positioned in the same index as answerInputs elements

var solutions:Array = [0, 40.25, 0, -52.5, 26.25, -78.75, 0, 0, 0, 40.25, 0, 0, -385, -8.75, 227.5, -175, 7, 140, 0, 0, 40.25, 268800, 2450, 97300];

// assuming that button that invokes checking for answers is named checkForAnswers instead of fourteenthreset_btn

checkForAnswers.addEventListener(MouseEvent.CLICK, checkAnswers);

// glow filters

var greenGF:GlowFilter = new GlowFilter(0x00ff00);

var redGF:GlowFilter = new GlowFilter(0xff0000);

function checkAnswers(e:MouseEvent):void

{

          // reusable text field instance

          var inputField:TextField;

          for (var i:int = 0; i < answerInputs.length; i++)

          {

                    inputField = answerInputs as TextField;

                    /**

                     * set filter depending on whether answer is right or wrong -

                     * compare to solutions array element in the same position

                     */

                    inputField.filters = [inputField.text.replace(/\s/g, "") != "" && Number(inputField.text) == solutions ? greenGF : redGF];

          }

}

function onFocus(e:FocusEvent):void

{

          TextField(e.currentTarget).text = "";

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines