PDA

View Full Version : Help with simple calculator


cmpaccione
02-09-2011, 04:20 AM
This is my first week in ActionScript and it shouldn't be this hard!!

Anyway...I think I have the code right but for the life of me I can't figure out how to get the value of a function into a textfield on the stage.

Basically, I have three textfields that have been placed on the stage dynamically. Two are input fields that except a number value and the two values are calculated with a function. I want the result of that calculation to show up in the third text field.

This is how I started my code:

package com.paccione{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;


public class Main extends MovieClip{

public function Main(){

var origWeight:Number;
var endWeight:Number;

Each text field is set up like this...I won't list them all for the sake of redundancy:

var inputStartWeight:TextField = new TextField();
this.addChild(inputStartWeight);
inputStartWeight.type = TextFieldType.INPUT;
inputStartWeight.width = 100;
inputStartWeight.height = 20;
inputStartWeight.x = 350;
inputStartWeight.y = 100;
inputStartWeight.border = true;


My function to calculate the two input fields:

public function calculation(origWeight:Number,endWeight:Number):Nu mber {
var percentage:Number = (origWeight / endWeight) * 100;
return(percentage);
}

I hope what I'm asking makes sense...it seems simple enough but for some reason I can't figure it out. And please noob it down for me...like I said, it's my first week in my ActionScript class and I have to be using very basic commands.

Thanks in advance!

SDragon029
02-09-2011, 05:16 PM
Hi,
could you place your whole .as file code here and how your calling certain functions in the .fla so that I can see how the class looks and how your trying to implement it. Also when your posting code on here try to use the AS or CODE tags so that it makes it look better.

Regards,
Tim

cmpaccione
02-10-2011, 02:39 AM
My class file is separate from my .fla file and I link them in the properties panel instead of doing it on the timeline (teacher rules). In the properties panel I link it to the .as using com.paccione.Main

My actionscript code in my Main.as file:

package com.paccione{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFieldType;


public class Main extends MovieClip{

public function Main(){

var origWeight:Number;
var endWeight:Number;

percentage(200, 175);
trace(percentage);


var heading:TextField = new TextField();
this.addChild(heading);
heading.text = "Calculate Percentage of Weight Loss"
heading.x = 200;
heading.y = 50;
heading.width = 350;

var originalWeight:TextField = new TextField();
this.addChild(originalWeight);
originalWeight.text = "Original Weight";
originalWeight.width = 100;
originalWeight.height = 20;
originalWeight.x = 200;
originalWeight.y = 100;

var currentWeight:TextField = new TextField();
this.addChild(currentWeight);
currentWeight.text = "Current Weight";
currentWeight.width = 100;
currentWeight.height = 20;
currentWeight.x = 200;
currentWeight.y = 150;

var percentLost:TextField = new TextField();
this.addChild(percentLost);
percentLost.text = "Percentage Lost";
percentLost.width = 100;
percentLost.height = 20;
percentLost.x = 200;
percentLost.y = 200;

var inputStartWeight:TextField = new TextField();
this.addChild(inputStartWeight);
inputStartWeight.type = TextFieldType.INPUT;
inputStartWeight.width = 100;
inputStartWeight.height = 20;
inputStartWeight.x = 350;
inputStartWeight.y = 100;
inputStartWeight.border = true;

var inputEndWeight:TextField = new TextField();
this.addChild(inputEndWeight);
inputEndWeight.type = TextFieldType.INPUT;
inputEndWeight.width = 100;
inputEndWeight.height = 20;
inputEndWeight.x = 350;
inputEndWeight.y = 150;
inputEndWeight.border = true;

var finalResult:TextField = new TextField();
this.addChild(finalResult);
finalResult.width = 100;
finalResult.height = 20;
finalResult.x = 350;
finalResult.y = 200;
finalResult.border = true;

}
public function calculation(origWeight:Number, endWeight:Number):Number{
var percentage:Number = (origWeight / endWeight) * 100;
return percentage;


And that's about it...I don't even know if I'm calling the function correctly and then especially how to get that result into the finalResult text field.

Thanks!

cmpaccione
02-10-2011, 05:28 AM
My teacher helped me figure it out!

Thanks for anyone else who was trying, I really appreciate it! I'm sure I'll be posting here more often!

SDragon029
02-11-2011, 09:15 PM
Glad to hear you guys got it working. So what was wrong with it then?

Regards,
Tim