Skip to main content
Participant
December 15, 2012
Question

Action Script problem. Math based question. Stage has 4 text input components, one button. Advice?

  • December 15, 2012
  • 1 reply
  • 513 views

I'll explain to help you understand my situation. Like it says from the main subject of my problem, I'm trying to get 3 text fields to calculate one result. But I'm trying to have two text fields use one type of operator (divide) with out showing the result. While the 3rd text field is meant to use another operator (multiply) to calculate with the divided result.

The Image displays 3 input text fields. The 4th text field is non editable. It's meant to only show the result.
This is not an everyday calculator. Main point is to get 3 values ending in one result using two (2) operators.

I gave each text field an instance name.
top (num1_txt)
mid(num2_txt)
3rd(num3_txt)

and the 4th (not editable) text field  instance name (window_txt)
also with a button instance name (equal_btn).
I understand I have to convert the text to a different data type due to the fact Action Script looks at these components as
String data types. I got my first objective to work with a conditional statement  for not displaying any value unless all text input fields are
filled in.

import flash.events.MouseEvent;

function calculate (event:MouseEvent):void
{
  trace (num1_txt.text);
  trace (num2_txt.text);
  trace (num3_txt.text);
  if (num1_txt.text == "" || num2_txt.text == "" || num3_txt.text == "")
  {
  window_txt.text = "Missing An Input Value";
  }

}
equal_btn.addEventListener(MouseEvent.CLICK, calculate);

proceeding to accomplish the main objective, I had the idea as converting these text values to a Number data type to have it result. What can be used for a container to hold this type of result (first result)? I entered this  convert The Number data type but I only know how to do two vaules;

window_txt.text =String(Number( num1_txt.text) / String(Number(num2_txt.text);

But then I forgot I didn't want the first result to display. How do I have the first result held in order for num3_txt.text to mutiply displaying the end result on window_txt.text? How do I get it to be some type of varible? Can this be possible?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 15, 2012

you could assign that result to a variable but there's no need for that.  just use:

import flash.events.MouseEvent;

function calculate (event:MouseEvent):void
{
  trace (num1_txt.text);
  trace (num2_txt.text);
  trace (num3_txt.text);
  if (num1_txt.text != ""&&| num2_txt.text != ""&&| num3_txt.text != "")
  {
  window_txt.text = String(Number(num1_txt.text)*Number(num3_txt.text)/Number(num2_txt.text));
  } else {

window_txt.text="Missing An Input Value";

}
}
equal_btn.addEventListener(MouseEvent.CLICK, calculate);