PDA

View Full Version : Refreshing text box output



cheesy_x
May 20th, 2005, 07:44 PM
Alright. I've made a tool in flash that calculates the value of money in comparison to the value of goods avaliable. however, the values that my program is supposed to calculate wont update when variables are changed, how do i fix this?

here is my code, commented for ease of reading:
/* Declares amtGood */
var amtGood:Number = 10;
/* Adds 1 to amtGood */
produce1.onRelease = function() {
amtGood += 1;
trace(amtGood);
};
/* adds 10 to amtGood */
produce10.onRelease = function() {
amtGood += 10;
trace(amtGood);
};
/* adds 50 to amtGood */
produce50.onRelease = function() {
amtGood += 50;
trace(amtGood);
};
/* Declares amtMon */
var amtMon:Number = 10;
/* Adds 1 to amtMon */
print1.onRelease = function() {
amtMon += 1;
trace(amtMon);
};
/* Adds 10 to amtMon */
print10.onRelease = function() {
amtMon += 10;
trace(amtMon);
};
/* Adds 50 to amtMon */
print50.onRelease = function() {
amtMon += 50;
trace(amtMon);
};
/* Declares valGood */
var valGood:Number = amtMon / amtGood;

As a brief explanation, I have 4 sets of buttons (the code for the first two sets is in there), They Create goods that are used to back paper currency (amtGood and amtMon are the amount of goods and money). To calculate the dollar value of goods, the total money is devided by the total amount of goods. However it won't calculate anything but the original 10/10

virusescu
May 20th, 2005, 08:04 PM
the line
var valGood:Number = amtMon / amtGood;
get's executed instantly (imediatley you define your code) and only once. You should recalculate the valGood value each time you modify your amtMon or amtGood variables ;)

cheesy_x
May 20th, 2005, 08:07 PM
Okay, I understand, but I'm not sure how I would go about doing this....


EDIT: hmm, trying something now

*okay fixed, but i get crazy decimals....

I put the calculation in each individual onrelease function

produce1.onRelease = function() {
amtGood += 1;
valGood = amtMon/amtGood
trace(amtGood);
};

Is there a way I can limit the amount of decimal places?

virusescu
May 20th, 2005, 08:13 PM
use Math.round(amtMon / amtGood) instead... this will round your number.
Search the forums... there were some number methods form Senocular for parsing a number with decimals to obtain another with a wanted set of decimals.

cheesy_x
May 20th, 2005, 08:17 PM
Thanks for your help. I'll do a search now.
the problem is that I can't really round the decimal, but need to truncate it instead.

EDIT: I'm all set, I have them at two decimal places now


produce1.onRelease = function() {
amtGood += 1;
prevalGood = amtMon / amtGood
valGood = int((prevalGood)*100)/100;
trace(amtGood);
};

Thanks for all your help.