Solved

Fast, smooth, 'counter' function?

Posted on 2004-03-24
7
180 Views
Last Modified: 2010-04-03
I would like a function that, when called, counts smoothly from 0-100 in, say, 300 milliseconds.

I will use the counter value to control the alpha property of an mc.

What is the best way to do this?
0
Comment
Question by:SandyCooke
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Expert Comment

by:RedLava
ID: 10673146
Hi SandyCooke

The best option to get a smooth count is to use setInterval().

== Demo ==

To test this demo create a textbox with an instance name of 'counter' on the main timeline.

Also add the following actionscript to the main timeline:

  function incCounter() {
    counter.text++
   if (counter.text == 100) clearInterval(counterID);  // Stop counter at 100.
  }

Then create a button with an instance name of 'startButton' that will call the following actionscript from the main timeline:

  startButton.onRelease = function () {
    counter.text = 0; // Start counter from 0.
    counterID = setInterval(incCounter, 300); // 300 millisecond interval.
  };

Good luck

RedLava
0
 
LVL 2

Author Comment

by:SandyCooke
ID: 10689228
Hi RedLava

Rather than count from 0-100 in 300 milliseconds, does this technique not just increase the value of counter.text every 300 milliseconds?

I do get the necessary result if I set the frame-rate of the movie to 100+ fps and reduce the interval to -10, but surely this is bad practice?

Or is there no other way?
0
 
LVL 9

Expert Comment

by:krishnaprathab
ID: 10706140
Just change the code to :

startButton.onRelease = function () {
    counter.text = 0; // Start counter from 0.
    counterID = setInterval(incCounter, 3000);
};

- KP
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:SandyCooke
ID: 10709911
Hi krishnaprathab, hi RedLava

Rather than count from 0-100 in 300 milliseconds, does this technique not just increase the value of counter.text every 300(0) milliseconds?

I do get the necessary result if I set the frame-rate of the movie to 100+ fps and reduce the interval to less than 10 milliseconds, but surely this is bad practice?

Or is there no other way?
0
 
LVL 4

Accepted Solution

by:
RedLava earned 250 total points
ID: 10709964
Hi SandyCooke,

counter.text can be any variable. I only used a text item to display it.

It can be just a variable named "theCounter" or whatever.

To get it to count 0-100 in 300 milliseconds you need to set the interval value to (300/100) => 3.

e.g

  startButton.onRelease = function () {
    counter.text = 0; // Start counter from 0.
    counterID = setInterval(incCounter, 3); // 3 millisecond interval.
  };

If you alter your frame rate this may cause other issues with the running of your movie.

Hope this helps,

RedLava
0
 
LVL 2

Author Comment

by:SandyCooke
ID: 10713346
>> To get it to count 0-100 in 300 milliseconds you need to set the interval value to (300/100) => 3
0
 
LVL 9

Expert Comment

by:krishnaprathab
ID: 10868859
No Assist :(
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to how to record live broadcast.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now