I have been working on creating a flash calculator with actionscript 2
It is all working fine, except I cannot work out the code i need for the ‘CE’ or clear button. I want it to just clear the input fields ready for new data.
Any ideas?
I’m new here so don’t know if theres a way I can send you the FLA, but the script is:
[COLOR=Blue]/* Calculator program, reads text from input (and validates text fields) and uses math class to display text at runtime
*/
//declaring variables
var value1:Number; //Assign data type as number
var value2:Number;
var answer:Number;
add_btn.onRelease = function ( ) //Release of button performs function
{
readInputs();
if ( validateInput() ){ //validation to ensure numbers are entered
performOperation( "add" );
displayResult();
} else {
output_txt.text = "Please enter numbers" // display message if non-numbers are entered
}
}
subtract_btn.onRelease = function ( ) //Release of button performs function
{
readInputs();
if ( validateInput() ){ //validation to ensure numbers are entered
performOperation( “subtract” );
displayResult();
} else {
output_txt.text = “Please enter numbers” // display message if non-numbers are entered
}
}
divide_btn.onRelease = function ( ) //Release of button performs function
{
readInputs();
if ( validateInput() ){ //validation to ensure numbers are entered
performOperation( “divide” );
displayResult();
} else {
output_txt.text = “Please enter numbers” // display message if non-numbers are entered
}
}
multiply_btn.onRelease = function ( ) //Release of button performs function
{
readInputs();
if ( validateInput() ){ //validation to ensure numbers are entered
performOperation( “multiply” );
displayResult();
} else {
output_txt.text = “Please enter numbers” // display message if non-numbers are entered
}
}
//attempt to add a clear function which clears data from input fields
clear_btn.onRelease = function ()
{
clear;
}
function readInputs() { //function for reading inputs
value1 = Number(input1_txt.text); //value1 is read from input1_text
value2 = Number(input2_txt.text);
}
function validateInput(){ //vaslidation of input
if ( isNaN(value1) || isNaN(value2) ){
return false;
} else {
return true;
}
}
function performOperation( op:String ){ //performOperation function to do calculations
if ( op == "add") {
answer = value1 + value2;
}
if ( op == "subtract") {
answer = value1 - value2;
}
if ( op == "divide") {
answer = value1 / value2;
}
if ( op == "multiply") {
answer = value1 * value2;
}
}
function displayResult() { //Displaying the answer in the output dynamic text box
output_txt.text = answer;
}
[/COLOR]