34 Replies Latest reply on Jan 10, 2007 9:18 AM by kglad

# recalculate

I want to create a dynamic text box with a start value of \$918,000

Then I wan to create a button that would minus 125,000 from what ever the dynamic text boxs value is every time it is clicked.

In addtion to this calculation I want to see the number in the text box dynamically decrease - for eample you would see the number very rapidly go from \$918,000 to \$917,00, \$916,000, \$915,000 until it reached a number minus \$125,000 less than the orignal number.
try:

• ###### 2. Re: recalculate
I did receive this error when testing though.

**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 6: Syntax error.
endNum=Number(calculation_txt.text.substring(1))-125000);

**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 8: Unexpected '}' encountered
}

Total ActionScript Errors: 2 Reported Errors: 2

I think one problem may be the addition of a ); at the end of line 7:
But I still get the sam error?
Any thoughts?
• ###### 3. Re: recalculate
use:

• ###### 4. Re: recalculate
Sweet!

Two more things. :)

Most importantly Is there a way to work in a comma, for example \$918,000?

And is there a way to ad an easing effect so that the number calculating slows at the end of its calculation?

I appreciate all your help on this.
• ###### 5. Re: recalculate
There is a prototype somewhere (I forget where) of a great comma adder. To use an easing effect, just have the interval change like it was the numbers for a graphed curve. Make sense?

I'll go get that prototype right now.
• ###### 6. Re: recalculate
String.prototype.makeCommas = function() {
v = "";
for (i=this.length-1; i>-1; i--) {
v += this.substring((this.length-(i+1)), (this.length-i));
if (i%3 == 0 && i != 0) {
v += ",";
}
}
return v;
};

//I don't know who to thank for this code, but it wasn't me. Thanks whomever wrote this!
• ###### 7. Re: recalculate
Thanks for looking for the prototype for adding the comma big help.

Not total sure about interval change like it was the numbers for a graphed curve.

Can explain?
• ###### 8. Re: recalculate
setInterval(FunctionName, IntervalVariable, [...])

function FunctionName() {

IntervalVariable = IntervalVariable / 2

}

I've never actually tried that, but my guess is it'll work with a bit of tweeking. Each time the funciton runs, the Interval will speed up 2x. (You'll need to slow that down, but maybe this helps you get my point).
• ###### 9. Re: recalculate
At first glance still not making sense about the inerval, but I will try and figure it out.

Let me know if you discover anything about adding the comma and thanks again for your help!
• ###### 10. Re: recalculate
The code for adding a comma... am I adding that exactly as it is below my existing code
• ###### 11. Re: recalculate
here's an easing technique:

• ###### 12. Re: recalculate
the prototype object just extends the String class. (!)

what that actually means, is you can just paste that code on frame 1 of your move, then use it like this.

NameAndPathOfYourString.makeCommas();
• ###### 13. Re: recalculate
easing technique works great but only the first time you click the button?

Also, still can't get the comma function to work.

Attached my code and its exactly what you've suggested.

Obviously I have a dynamic text field named calculation_txt and a button named calculateBTN_btn.

Thanks again for all your help.

• ###### 15. Re: recalculate
Perfect. Thanks!

Now about that code for adding the comma I can't figure out were it actually points to my textField?

• ###### 16. Re: recalculate
try:

• ###### 17. Re: recalculate
That did it!

Thanks again for all your help on this one!

Wish I could return the favor but I am not nearly at your level just yet! :)

Best Regards
• ###### 18. Re: recalculate
you're welcome.
• ###### 19. Re: recalculate
Me again!

I have another question for an added feature to this "Re-Calculator"

Currently it decreases the main number by -125,000 everytime you click the button.

What I would like it to do is decrease the main number by an array of differnt numbers i.e. -125,000, -150,000, -162,000, etc. Also disable the button when the main number reached 0.

• ###### 20. Re: recalculate
define a variable as 0, then each time the button is clicked add one.

crazyvariable = 0

onRelease() {
orignalValue -= PredefinedArray[crazyvariable]
crazyvariable++
}

make sense?
• ###### 21. Re: recalculate

//////////

define a variable as 0, then each time the button is clicked add one.

crazyvariable = 0

onRelease() {
orignalValue -= PredefinedArray[crazyvariable]
crazyvariable++
}

//////////

Here's what I have now.

calculation_txt.text = "\$918,000";

String.prototype.makeCommas = function() {
v = "";
for (i=this.length-1; i>-1; i--) {
v += this.substring((this.length-(i+1)), (this.length-i));
if (i%3 == 0 && i != 0) {
v += ",";
}
}
return v;
};

calculateBTN_btn.onPress = function() {

clearInterval(calculateI);
var n = calculation_txt.text.substring(1).split(",").join("");
endNum = Number(n)-125000;
indexVar = 0;
calculateF(endNum);
};

function easeOutF(x0, x1, steps) {
if (!indexVar) {
indexVar = 1;
} else {
indexVar++;
}
return x0+(x1-x0)/(.25+steps-indexVar);
}

function calculateF(endNum) {
clearInterval(calculateI);
calculateI = setInterval(calculateF, easeOutF(1, 200, 125), endNum);
var n = calculation_txt.text.substring(1).split(",").join("");
n = (Number(n)-1000);
calculation_txt.text = "\$"+n.toString().makeCommas();
if (endNum-n == 0) {
clearInterval(calculateI);
}
updateAfterEvent();
}

• ###### 22. Re: recalculate
Couldn't I substitiute

endNum = Number(n)-125000;

for an array of different numbers rather than just -125000?

Thing is I don't know how to do that?
• ###### 23. Re: recalculate
I was able to figure out my last question about adding an array etc. which I will show the code for but now I am trying to do the reverse by adding rather than subtracting and I've almost have it except it animates negatively?

Not sure what I am missing?

• ###### 24. Re: recalculate
if you're trying to add to the number in your textfield until you reach endNum you should change the line with

n = (Number(n)-1000);

to something like

n = Number(n)+Math.round((endNum-n)/steps) // where you must pass or define steps;

and your end condition doesn't look right. you want to terminate your calculateI when n is close to endNum. are you're not using easing, though i don't know if that's something that you still want.
• ###### 25. Re: recalculate
Thanks that did it!

Yes I am trying to keep the easing effect. How do I bring that back?

• ###### 27. Re: recalculate
Sorry, Not sure exactly where that goes or what it may replace?

• ###### 28. Re: recalculate
try:

• ###### 29. Re: recalculate
No it makes it go negitive and then stops at the correct array?

Is there a way to attach a test fla for you to look at?
• ###### 30. Re: recalculate
i thought i answered this, but i don't see my answer (yet).

no, you can't post a file here. you can upload your fla to a server (there are free ones) and post the link here.
• ###### 31. Re: recalculate
All set Thanks!!!
• ###### 32. Re: recalculate
you're welcome. (i think.)
• ###### 33. Re: recalculate
Thank you for your help! I just figured out how to do what I need to do on my own. :) I've attached the code if your interested in reviewing it.

Though I do have another question?

How would I get the \$0 to animate to \$59,897 automatically when the movie starts?

• ###### 34. Re: recalculate
execute your calculateBTN_btn's onPress code after your array and index are defined.