Skip to main content
Participating Frequently
March 9, 2022
Question

Issue converting old AS2 code to AS3 to make a calculator work.

  • March 9, 2022
  • 2 replies
  • 164 views

I would be very grateful for any suggestions in getting this original AS2 calculator script to run as AS3 as am having problems getting Math functions to execute along with Incorrect Input function.

I have 4 Input Text fields: speed, hook, area and drag. also a dynamic Text field: total_txt with a calculate and clear and close btn.  

 

 

// create permissible variable to ensure field is cleared on application load
permissible = "";
calculate_btn.onPress = function() {

// execute script to calculate values
if (speed>0 && hookload>0 && area >0 && drag >1.19999){
permissible = Math.round(Math.sqrt(hookload*1.2/(drag*area))*speed*100)/100
var num = String(permissible);
if(permissible > speed) permissible = speed;

}else{

//issue message if incorrect input
permissible = "Incorrect Input";
}
}
clear_btn.onPress = function(){
//clear named fields
permissible = "";
speed="";
hookload="";
area="";
drag="";
}

close_btn.onRelease=function(){
//unload this movie
unloadMovie("");
}

 

Thanks in advance.

 

This topic has been closed for replies.

2 replies

Participating Frequently
March 10, 2022

Hi Kglad,

 

Many thanks for your kind help, which is greatly appreciated.

 

 

kglad
Community Expert
Community Expert
March 10, 2022

you're welcome.

kglad
Community Expert
Community Expert
March 10, 2022

1. you need to define the variables.  if they were assigned to textfield properties in the as2 fla, you can call textFieldsVariablesF each time you need to update those variables:


2. you can unload if this was loaded by another swf (using a loader).  else just create an empty keyframe or whatever you want to display to close "this".


function textFieldVariablesF():void{
hookload = Number(your_hookload_tf.text);
speed = Number(your_speed_tf.text);
etc
}

 

 

// create permissible variable to ensure field is cleared on application load
var permissible:Number;
var speed:Number;
var hookload:Number;
var drag:Number;
var area:Number;

calculate_btn.addEventListener("MouseEvent.MOUSE_DOWN,calculateF);

function calculateF(e:MouseEvent):void {
// you probably need to call textFieldVariablesF() here
// execute script to calculate values
if (speed>0 && hookload>0 && area >0 && drag >1.19999){
permissible = Math.round(Math.sqrt(hookload*1.2/(drag*area))*speed*100)/100
if(permissible > speed) {
total_txt.text = String(permissible);
}else{
//issue message if incorrect input
total_txt.text = "Incorrect Input";
}
}
clear_btn.addEventListener("MouseEvent.MOUSE_DOWN,clearF);

 

function clearF(e:MouseEvent):void{
//clear named fields
//assign text property to your textfields
}

close_btn.addEventListener(MouseEvent.MOUSE_DOWN,closeF);

 

function closeF(e:MouseEvent):void{
//unload this movie
this.loaderInfo.loader.unloadMovie();  // assuming "this" was loaded
}