# Timer Function

**URL:** <https://forum.kirupa.com/t/timer-function/182741>\
**Category:** flash\
**Created:** [March 21, 2006, 3:42am UTC](https://forum.kirupa.com/t/timer-function/182741 "2006-03-21T03:42:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 3:42am UTC](https://forum.kirupa.com/t/timer-function/182741/1 "2006-03-21T03:42:28Z")

</div>

Ok, here’s the deal. I’m working on a small game, and the first step is going to be this, the timer.  
I’ve made a little script that turns my imput in seconds into frames, and the movie is running at 36 fps.  
so every 2 second is 24 frames, or -1 from every 12 seconds. I’m doing it this way because the game is to have no real value, but only that of helping me learn. And right now I don’t really want to get into setInterval.  
So, now that I’ve explained it, could someone help me figure out why this doesn’t work.  
I have a bar, 5 pixles high and 1 wide, with the registration point on the left.  
but this doesn’t make the bar the correct width.

```auto
_root.sec.text=1;
_root.timed.text=_root.sec.text*36;
_root.bar1.text=(_root.timed.text)/530;
_root.bar2.text=_root.bar1.text*530;
_root.bar._width=bar3;
_root.ab.text=text(_root.bar1.text)

```

36 being the fps,  
530 being the area I would like the bar to take up at the most.  
I’m still not quite sure how the time will deteriorate, but I will figure that out.  
Right now I just want to know why the bar will now become longer…  
Thanx if you can help.😉

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 21, 2006, 3:56am UTC](https://forum.kirupa.com/t/timer-function/182741/2 "2006-03-21T03:56:52Z")

</div>

um every 2 seconds at 32fps is 64 frames…

first of all dont use Textbox.text to store your variables and work with multiplications - make variables and do your maths to them…then set the text of textboxes after youve got the results stored in your variables.

im not 100% clear what u want to do?

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 4:10am UTC](https://forum.kirupa.com/t/timer-function/182741/3 "2006-03-21T04:10:11Z")

</div>

Ok, I very much realize what I did before was wrong.  
Try this.

```auto
onEnterFrame=function(){
	//sec conversion
	_root.sec=1;
	_root.bar._width=(_root.fpsec/530)*530;
	//timer
	setInterval(timer,10);
	timer(_root.sec,_root.bar._width);
}
function timer(time,barw){
	if(time>0){
		time=time-.1;
		barw=barw-(time*530);
	}
}

```

It still doesn’t afect the length of the bar…

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 21, 2006, 4:21am UTC](https://forum.kirupa.com/t/timer-function/182741/4 "2006-03-21T04:21:16Z")

</div>

**1)**\* (\_root.fpsec / 530) \* 530\* will [U]always[/U] equal 530

**2)** It doesn’t make sense to call setInterval from an onEnterFrame function, especially since you aren’t passing any parameters so all those variables will be undefined. And even when you do call the function correctly with \*timer(\_root.sec, \_root.bar.\_width); \*it doesn’t do anything since the variables are a reference to the ones you passed to the function. Using setInterval also goes against your “I don’t really want to get into setInterval” policy.

**3)** What are you trying to do?

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 21, 2006, 4:29am UTC](https://forum.kirupa.com/t/timer-function/182741/5 "2006-03-21T04:29:35Z")

</div>

> [@TheCanadian](#):
>
> **3)** What are you trying to do?

it would help if he told us :ear:

one things for sure - your passing bar.\_width to the timer function assumeing that you can use barw=barw-COLOR=#000000[/COLOR];

bar.\_width will just pass an integer to the timer function. its better to pass a reference to a movie clip…

[COLOR=Black] function timer[/COLOR]COLOR=Black[/COLOR][COLOR=Black]{

and then edit the .\_width of that think about it - a number doesnt have a width property

…but like TheCanadian said - you are in an onEnterFrame why add an interval? Id reccomend just using an interval…  
[/COLOR]

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 4:51am UTC](https://forum.kirupa.com/t/timer-function/182741/6 "2006-03-21T04:51:10Z")

</div>

I’m making a game, where these balls randomly shoot off and slow down, there’s a health bar, and a timer, you get to the next level by outstanding the timer and having health left.  
I’m just having imense difficulty with this timer part.  
I also decided to use setInteral after I looked it up on google, after I first heard about it it sounded a bit too hard for me. I still don’t understand what paramaters to insert. Although I do get that part about taking the setInterval out of the onEnterFrame.

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 5:05am UTC](https://forum.kirupa.com/t/timer-function/182741/7 "2006-03-21T05:05:27Z")

</div>

Ok, I got it down it this.

```auto
[FONT=Courier New][LEFT][COLOR=#808080]*//timer function*[/COLOR]
[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]sec[/COLOR]=[COLOR=#000080]10[/COLOR];
[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]barwi[/COLOR]=[COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]sec[/COLOR]*[COLOR=#000080]1000[/COLOR][COLOR=#000000])[/COLOR]/[COLOR=#000080]530[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]bar[/COLOR].[COLOR=#0000ff]_width[/COLOR]=[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]barwi[/COLOR];
timer[COLOR=#000000]([/COLOR][COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]sec[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#0000ff]setInterval[/COLOR][COLOR=#000000]([/COLOR]timer,[COLOR=#000080]100[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000] **function** [/COLOR] timer[COLOR=#000000]([/COLOR][COLOR=#0000ff]time[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]
    [COLOR=#0000ff]if[/COLOR][COLOR=#000000]([/COLOR]time>[COLOR=#000080]0[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]
        [COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]sec[/COLOR]=[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]sec[/COLOR]-[COLOR=#000080].1[/COLOR];
    [COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]ab[/COLOR].[COLOR=#0000ff]text[/COLOR]=[COLOR=#0000ff]_root[/COLOR].[COLOR=#000080]barwi[/COLOR];

```

[/LEFT]  
[/FONT]The bar extends but the time dosen’t decrease and the bar dosent shorten either.

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 21, 2006, 5:06am UTC](https://forum.kirupa.com/t/timer-function/182741/8 "2006-03-21T05:06:09Z")

</div>

```auto
var maxTime:Number = 10;
var currTime:Number = maxTime;
var div:Number = 100;
var timerInt:Number = setInterval(function () {
 if (currTime > 0 + div / 100) {
  currTime -= 1 / div;
 } else {
  currTime = 0;
  clearInterval(timerInt);
 }
 bar._xscale = currTime / maxTime * 100;
}, 1000 / div);

```

:thumb:

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 5:16am UTC](https://forum.kirupa.com/t/timer-function/182741/9 "2006-03-21T05:16:34Z")

</div>

[FONT=Courier New][LEFT][COLOR=#808080]_//timer function_[/COLOR]  
[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]sec[/COLOR]=[COLOR=#000080]10[/COLOR];  
[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]barwi[/COLOR]=[COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]bar[/COLOR].[COLOR=#0000ff]\_width[/COLOR]=[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]barwi[/COLOR];  
timerCOLOR=#000000[/COLOR];  
[COLOR=#0000ff]setInterval[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000] **function** [/COLOR] timerCOLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]if[/COLOR]COLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]sec[/COLOR]=[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]sec[/COLOR]-.[COLOR=#000080]1[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]ab[/COLOR].[COLOR=#0000ff]text[/COLOR]=[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]barwi[/COLOR];  
[/LEFT]  
[/FONT]

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 2:18pm UTC](https://forum.kirupa.com/t/timer-function/182741/10 "2006-03-21T14:18:40Z")

</div>

> [@Catalist](#):
>
> [FONT=Courier New][LEFT][COLOR=#808080]_//timer function_[/COLOR]  
> [COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]sec[/COLOR]=[COLOR=#000080]10[/COLOR];  
> [COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]barwi[/COLOR]=[COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]round[/COLOR]COLOR=#000000[/COLOR];  
> [COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]bar[/COLOR].[COLOR=#0000ff]\_width[/COLOR]=[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]barwi[/COLOR];  
> timerCOLOR=#000000[/COLOR];  
> [COLOR=#0000ff]setInterval[/COLOR]COLOR=#000000[/COLOR];  
> [COLOR=#000000] **function** [/COLOR] timerCOLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
> [COLOR=#0000ff]if[/COLOR]COLOR=#000000[/COLOR][COLOR=#000000]{[/COLOR]  
> [COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]sec[/COLOR]=[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]sec[/COLOR]-.[COLOR=#000080]1[/COLOR];  
> [COLOR=#000000]}[/COLOR]  
> [COLOR=#000000]}[/COLOR]  
> [COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]ab[/COLOR].[COLOR=#0000ff]text[/COLOR]=[COLOR=#0000ff]\_root[/COLOR].[COLOR=#000080]barwi[/COLOR];  
> [/LEFT]  
> [/FONT]

Ok, this code now works. But the bar nor the time decrease. As if it’s only running the timer function once…😕

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 21, 2006, 10:41pm UTC](https://forum.kirupa.com/t/timer-function/182741/11 "2006-03-21T22:41:55Z")

</div>

:puzzled:

```auto

sec=5;
setInterval (timer,100)
timer(sec)
function timer(time){
    if(time>0){
        time=time-.1;
    }
    ab.text=sec;
}

```

Why dosent this work? Is it because I have no clue how to use setInterval?

---

<div class="post-metadata">

**Author:** ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)\
**Post date:** [March 21, 2006, 11:35pm UTC](https://forum.kirupa.com/t/timer-function/182741/12 "2006-03-21T23:35:30Z")

</div>

um first of all define your function ‘timer’ **before** calling it using a setInterval

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [March 22, 2006, 2:17am UTC](https://forum.kirupa.com/t/timer-function/182741/13 "2006-03-22T02:17:49Z")

</div>

> [@matthewjumps](#):
>
> um first of all define your function ‘timer’ **before** calling it using a setInterval

Actually functions defined as _function myFunction(){}_ are accesible prior to their definition in the script, and besides the function isnt called for 100 ms 😉

> [@Catalist](#):
>
> :puzzled:Why dosent this work? Is it because I have no clue how to use setInterval?

Apparently - what’s wrong with the code I posted?

---

<div class="post-metadata">

**Author:** ![Catalist](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/catalist/32/2148_2.png) [@Catalist](https://forum.kirupa.com/u/Catalist)\
**Post date:** [March 22, 2006, 2:45am UTC](https://forum.kirupa.com/t/timer-function/182741/14 "2006-03-22T02:45:49Z")

</div>

Ok, I got it down. I see how it all works. But there seems to be some sort of math error in flash.  
I made up a test script to see if it really was.

```auto
sec=20;
interval=setInterval(time,10);
function time(){
    sec-=.1;
    if (sec<=0){
        clearInterval(interval);
    }
}
onEnterFrame=function(){
    display.text=sec;
}

```

Make a dynamic text box witht he instance name display.  
at about 16-15 and then 9-8 it goes to 15.19999999999999  
Is this just some sort of lag in my computer and the countdown?  
Even if it is, I know how do fix it. I need some sort of Math.round but to the tenth.  
Ya :h:
