Groups | Blog | Home
all groups > flash actionscript > may 2005 >

flash actionscript : Seperate Equations in a Calculator



kglad
5/20/2005 12:00:00 AM
if your textfields and input variables are on the _root timeline you can use

priceTotal_txt.text=0;
for(var i=1;i<=3;i++){
_root["qty"+i+"_txt].onChanged=function(){
_root["qty4_txt].text=totalQuantF();
priceTotal_txt.text=totalPriceF();
}
}
qty5_txt.onChanged=function(){
qty4_txt.text-=Number(this.text);
// it's not clear how price or the other quantities would change
// and i'm assuming qty5_txt wouldn't change until something is in qty4_txt
}
function totalQuantF(){
qty4_txt.text=0;
for(var i=1;i<=3;i++){
qt4_txt.text+=Number(_root["qty"+i+"_txt].text);
}
}
function totalPriceF(){
priceTotal_txt.text=0;
for(var i=1;i<=3;i++){
priceTotal_txt.text+=Number(_root["qty"+i+"_txt].text)*_root["input"+i];
}
}
apodkolinska
5/20/2005 12:00:00 AM
athe onChanged is applied to the text field.. not the actual frame.

So i would create a function that updates all the fields:

qty1_txt.onChanged = qty2_txt.onChanged = qty3_txt.onChanged =
qty4_txt.onChanged = function() {

priceTotal_txt.text =
Number(qty1_txt.text)*Number(input1)+Number(qty2_txt.text)*Number(input2)+Number
(qty3_txt.text)*Number(input3);
qty4_txt.text =
Number(qty1_txt.text)*Number(input1)+Number(qty2_txt.text)*Number(input2)+Number
(qty3_txt.text)*Number(input3);
}

mbm1776
5/20/2005 12:00:00 AM
OK, I don't know if I did it right but it now WORKS!!!! Now... when I type in
$25.00 plus $25.50 it should equal $50.50... but it comes up as $50.5 ... with
no ending zero. So, my next question is how do I make the answer fields come up
with the numbers ending with a "0" or a ".00" if the number is even like
$50.00??
Thanks!

Here is what I used.

var input1 = 1;
var input2 = 1;
var input3 = 1;


//Calculate quantity times price
qty1_txt.onChanged = qty2_txt.onChanged = qty3_txt.onChanged =
qty4_txt.onChanged = function() {
priceTotal_txt.text =
Number(qty1_txt.text)*Number(input1)+Number(qty2_txt.text)*Number(input2)+Number
(qty3_txt.text)*Number(input3);
qty4_txt.text =
Number(qty1_txt.text)*Number(input1)+Number(qty2_txt.text)*Number(input2)+Number
(qty3_txt.text)*Number(input3);

};
qty4_txt.onChanged = qty5_txt.onChanged = function() {
priceTotal_txt2.text = Number(qty4_txt.text)-Number(qty5_txt.text);
};


apodkolinska
5/20/2005 12:00:00 AM
HA! Oh fun!
Convert the whole thing to cents and display as a string :)


mbm1776
5/20/2005 12:00:00 AM
apodkolinska
5/20/2005 12:00:00 AM
There are a couple of ways to do this...

qty4_txt.onChanged = qty5_txt.onChanged = function() {
priceTotal_txt2.text = Number(qty4_txt.text)-Number(qty5_txt.text);
yourSeparatedPrice = new Array;
//this will take your number 50.4 and make it in to "50.4" which is a string
and then separate it out with a delimiter using the "."
yourSeparatedPrice = String(priceTotal_txt2.text ).split(".");
// this will give you an array... yourSeparatedPrice[0] are you
dollars...yourSeparatedPrice[1] are your cents.

then you can do an if function
// have to conver back to number to compare if it's less than 10
if(Number(yourSeparatedPrice[1]) <10){
//add a 0 after your number as less than 10 indicates that you have 10, 20,
30, etc
yourSeparatedPrice[1] = yourSeparatedPrice[1] +"0";
}
priceTotal_txt2.text = yourSeparatedPrice[0]+"."+yourSeparatedPrice[1];

};

Let me know if that makes sense :D

mbm1776
5/20/2005 12:00:00 AM
Hey that works great! But, now if I type in $50 + $50 + $50 it equals $150.0
Not $150.00
And, since its show up in to 2 separate result lines... if I mess with the
yourSeparatedPrice[1] = yourSeparatedPrice[1] +"0"; line it simply adds too
many "0"s!!
I'm so close.
thanks again for all your great help!
apodkolinska
5/20/2005 12:00:00 AM
mbm1776
5/20/2005 12:00:00 AM
OK, great! So if I wanted the answer line to contain 2 ".00" , how do I do that?
For instance if I type in numbers more than .99 ... like 20 +20 +20 it still
comes up with $60.0
I want it to come up with .00
I beginning to under the line yourSeparatedPrice[1] = yourSeparatedPrice[1]
+"0";
But if I add another 0 to the +"00'; it messes things up in a scerio like
$20.50 + $20.00.... it simple adds too many zeros!
apodkolinska
5/20/2005 12:00:00 AM
Right! If you separate and it's a round number yourSeparatedPrice[1] will be
equal to 0... although it should be undefined.

if(Number(yourSeparatedPrice[1]) <10 && Number(yourSeparatedPrice[1]) >0){
yourSeparatedPrice[1] = yourSeparatedPrice[1] +"0";
}else if(yourSeparatedPrice[1] == undefined){
yourSeparatedPrice[1] = "00";
}
priceTotal_txt2.text = yourSeparatedPrice[0]+"."+yourSeparatedPrice[1];
mbm1776
5/20/2005 12:00:00 AM
:confused;I am creating a calculator in flash that lets the end user type in 3
separate amounts (qty1_txt, qty2_txt, qty3_txt) that while you are typing it is
being added up into the amount category (priceTotal_txt) and in another amount
category (qty4_txt).
OK, now this is where it gets tricky... Along with (qty4_txt) there is another
input area (qty5_txt) that will let you SUBTRACT this amount from (qty4_txt)
and gives you a new amount in a new area (priceTotal_txt2). These amounts need
to be added and subtracted while the person is typing. How can I get the amount
((qty4_txt) to Subtract (aty5_txt) separtately?
The client doesn't want a "Calculate" button... I wish they did because I can
get that to work with no issues!
This is what I have so far.

var input1 = 1;
var input2 = 1;
var input3 = 1;

//Calculate quantity times price
this.onEnterFrame = function() {
priceTotal_txt.text =
Number(qty1_txt.text)*Number(input1)+Number(qty2_txt.text)*Number(input2)+Number
(qty3_txt.text)*Number(input3);
qty4_txt.text =
Number(qty1_txt.text)*Number(input1)+Number(qty2_txt.text)*Number(input2)+Number
(qty3_txt.text)*Number(input3);

};

Also, if anyone knows how I can make this thing keep the zeros after the
decimal points, I would greatly appreciate it. (e.g. $49.50) Its showing up as
$49.5
thanks!


kglad
5/20/2005 12:00:00 AM
mbm1776
5/20/2005 12:00:00 AM
You mean, instead of this.onEnterFrame = function()
I should use this.onChanged = function() ?
David Stiller
5/20/2005 9:07:26 AM
mbm1776,

[quoted text, click to view]

Did you look up the onChanged event handler? If you simply replaced
this ...

this.onEnterFrame = function() { ...

with this

this.onChanged = function() { ...

.... then you betcha, it won't work. That's because "this" -- which I
presume is a movie clip or the main timeline -- isn't an object that
features an onChanged event handler. As kglad pointed out, you'll need to
look up the TextField class to find onChanged. The ActionScript Language
Reference (F1 key) has code samples galore.

The main timeline basically is a movie clip, so you'll note under the
"MovieClip class" entry that onEnterFrame is one of the available event
handlers. But you won't see onChanged. Make sense? :)


David
stiller (at) quip (dot) net
"Luck is the residue of good design."

AddThis Social Bookmark Button