PDA

View Full Version : Problems with simple calculator


moosildinho
10-21-2008, 10:31 AM
Hey!
As you will very soon realise, I am VERY new to ActionScript 2.0. Basically, a friend and I are ultimately designing a site to host calculators for specific needs (mostly physics). So, to kick-off my learning, I thought i would start with the most basic physics; acceleration. I have tried to design a calculator where the user puts in values for initial velocity, final velocity and time, and receive a value for acceleration (of course after pressing a "calculate" button). It's simple, but it's a start. Anyway, so far I have made 'Input' fields for the Initial/Final velocity as well as time, made a 'dynamic' field for the acceleration to be displayed in, a button to initiate a calculation, and some basic lines of script. When I try to run the calculator, nothing happens on pressing the 'calculate' button. Here is the script so far:

{calc_btn.onPress = function (calculate);}

function calculate (event.onPress):void;

var acceleration:String
var time:String
var initial:String
var final:String

time = t.text
initial = initial.text
final = final.text
{
if (time!="") and (init!="") and (final!="") {
acceleration.toString = ((((parseInt(final)) - (parseInt(initial))) / (parseInt(time)))
} else {
acceleration == "error";
}
}


If anyone has any idea what i am doing wrong, any help would be GREATLY appreciated. I'm sure there are a lot of bracketing errors, and the script probably looks like illegible crap to you, and if so, any advice would be awesome.
Thanks in advance.

EDIT: sorry for putting this in the wrong section before, should have looked around a bit more first :(

vinayak.kadam
10-21-2008, 12:30 PM
Can you upload the FLA for us to have our hands on it.

CliveCarrington
10-21-2008, 05:36 PM
Yeah, it's illegible, yeah, there are many bracketing errors... The script you wrote above looks like a drawing of Picasso:).
It does not abide by the rules of ActionScript 2.0. I'll try to explain how to write it properly.

The first line of the script you wrote above should look like this:

calc_btn.onPress = function () {
calculate ();
};

That is if you have a function named "calculate" and you want to run it when the button is pressed.
What happens here is you first create the function ("onPress"), then open a round bracket, put in the variables you want to use in your function (in this case, none. More about this later, if you ask for it) and close with a round bracket.
Then open a brace ("{"). Here you write what you want the function to do (in this case, activate another function named "calculate"). Then you close with the proper brace ("}").

The second line you wrote (actually, it's the third, but never mind) seems to be creating the function named "calculate" in the beginning, but then it starts getting messy, and more, it has no connection the the part you wrote on the bottom of the script, that seems to do the actual calculation.

To create this function properly, you should write it like this:

function calculate () {
if (time and init and final) {
// This makes sure the variables are what I call "positive". Means they are not
// equal to any of the following values:
// undefined, null, false, NaN (Not a Number), "" (empty string), or 0.
acceleration = (final - initial) / time;
// in this case it's not important to use the parseInt function.
// It will work the same without it.
} else {
acceleration = 'Error!';
// It doesn't matter if you use a single quote or a double quote,
// as long as both the opening and the ending quotes are the same.
}
};


The part where you attempted to create the variables named "acceleration", "time", "initial" and "final", is also written wrongly. There are two problems:
1. You didn't set a value for the variables you created. Therefor their value is set to undefined.
2. The names you gave your variables collide with the names of the text fields.
There can not be both a textField with the name "initial" and a variable with the name "initial", because when you write "initial" the script can't know which of them you are referring to. For the script to work properly, you must give unique names to the objects you want to refer in the script.

Considering you have three "input" text fields named "time", "initial" and "final", if you want to create variables the will refer to the text of these text fields, you must give these variables unique names. For example, "myTime", "myInitial" and "myFinal". It can be anything as long as no names overlap.
The code will look like this:

var acceleration:String = "";// An empty string for a start
var myTime:String = time.text;
var myInitial:String = initial.text;
var myFinal:String = final.text;

You have to also remember that this will happen once, in the first frame. It means that if you write something in a text field "final", for example, it won't change the value of myFinal. To make sure that the values of your variables stays in sync with the text in the text fields, you should either put the above inside an "onEnterFrame" function, to update constantly, or inside your "calculate" function, to update only when you call the function (in this case, when you press the button).

Based on everything said above, the final script should look like this:

// Create your variables and set them to empty text:
var acceleration:String = "";
var myTime:String = "";
var myInitial:String = "";
var myFinal:String = "";
// Create the onPress function for your button:
calc_btn.onPress = function () {
calculate ();
};
// Create the calculation function:
function calculate () {
myTime = time.text, myInitial = initial.text, myFinal = final.text;
// In most cases you can put a comma instead of a semicolon.
if (myTime and myInitial and myFinal) {
acceleration = (myFinal - myInitial) / myTime;
} else {
acceleration = 'Error!';
}
// Naturally, you also need to display the result to the user.
// You can do that by tracing the variable "acceleration",
// if you run the file in a debugging environment (e.g. in Flash):
trace (acceleration);
// Or you can send the value of "acceleration" to a textField you've created in advance:
resultTextBox.text = acceleration;
};


I really hope I helped you with this... article:)...
Clive.