-
How to increase number with decimals?
Hi guys,
I'm new with action script so I'm really sorry if my question sounds silly ^^;
I'm trying to find a script to increase number from 0.00 to 999.99. When I searched online, this script works for me:
Code:
timer_txt.text = 0; // start time
function timer(){
Number(timer_txt.text++);
if(Number(timer_txt.text == 999)){
clearInterval(myTimer);
trace("finished");
}
}
myTimer = setInterval(timer, 10); // 1000 milliseconds = 1 second
// so the textfield is decreased by 1, every second
Unfortunately, when I tried to put decimals on any number the script stopped to work (e.g timer_txt.text = 0.00; or if(Number(timer_txt.text == 999.99)). When I tried to make the increasement in decimals, it refuses to work as well (Number(timer_txt.text=timer_txt.text+0.55); )
What can I do to make the number increase in decimals? And is it possible to control the increasement? (say +0.55 on every second). I have attached my .fla file if it helps.
Thank you for taking your time to read this post! Any help will be very much appreciated.numberIncrease.fla
-
Prid - Outing
Hello and welcome
The code you found on the internet is an odd and rather messy way of achieving what you want, as using variables would be a lot easier and better - but, taking into account that you are new to actionscript, the code probably suits you better. But, instead of making the code even longer and more messier (which will probably make it look more complicated), I decided to write my own version to make the code easily editable to suit your needs
Code:
num = 0; // starting number
step = 0.55; // increase number
max = 999.99; // maximum number, where it stops
timer_txt.text = num; // shows the starting number in the textfield
function timer(){
num += step; // increase num by step value (0.55)
if(num >= max){ // if num is greater than or equals to max value (999.99)
num = max; // then set num as max
clearInterval(myTimer); // and stop increasing
}
timer_txt.text = Math.floor(num*100)/100; // show num value in the textfield
// Math.floor(num*100)/100 makes the num value round to 2 decimals, because
// without it, you would get like 765.6500000000, and we don't want that :P
}
myTimer = setInterval(timer, 1000);
I have tried to explain what is going on in each line, and I hope that it doesn't look too advanced for you :O You can just change the 3 values at the top to whatever you like and the code will automatically adjust to those changes.
I hope this helped you, even though it's super late and you're probably gone but, I figured I should help you anyways in case you return.
Have a nice day
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
-
PHP Code:
var start_num = 0.00; var increasement_num = 0.55; var max_num = 999.99; var timer_interval = setInterval(timer, 10);
function timer() { if (start_num <= max_num) { timer_txt.text = Math.floor(start_num * 100) / 100; start_num += increasement_num; } else { clearInterval(timer_interval); } }
A little modification for more perfection.
-
Prid - Outing
nice, I thought about that, but I figured I should leave it since it would have required more explanation => more comment lines => messier.
I am back, guys ... and finally 18 :P
BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|