Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Increment a counter variable based every x seconds


What is the best way in actionscript to handle incrementing a counter variable, say by 1 unit, every 20 seconds....

This will be used in a little project to simulate the number of visitors entering an amusement park....

-cdr
0
cdr21
Asked:
cdr21
  • 2
1 Solution
 
Jakob_ECommented:
Try this method

// Variable holding your count
var counter = 0

// SetInterval makes a call to your function increaseCount every 20000 millisecond (20 secsonds)
counterInterval = setInterval(increaseCount,20000)

// increaseCount adds one to your counter
function increaseCount(){ counter++ }

// If at some point you want to stop your counter use the clearInterval
clearInterval(counterInterval)


~Jakob E
0
 
BillystyxCommented:
another less easy to use way (just for information)
is the getTimer()
set the variable in frame1:
time=getTimer();
counter=0;
frame2:
if(time+20000<getTimer()){
counter ++;
time=getTimer();
}
frame3:
gotoAndPlay(2);


Billystyx
but Jakob's method is the newer and (better) one.
0
 
cdr21Author Commented:

...I can be pretty dumb when it comes to this actionscripting stuff....

Below is the code I'm using on a variable called 'visits'...I 'm needing to initilize it to around 37000...so far I do have it showing up properly at 37000 in a dyanamic text box...but it is not incrementing....

any ideas why not?

-cdr


onClipEvent (enterFrame) {

var visits = 37000

function increaseCountV() { visits ++ }

// SetInterval makes a call to your function increaseCount every 20000 millisecond (20 secsonds)

counterInterval = setInterval(increaseCountV, 10000)


}
0
 
BillystyxCommented:
the 2 pieces of code jakob provided go on the root timeline, on a single frae - not on an mc.

Billystyx
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now