Timer Function

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.

_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.:wink:

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?

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

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…

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?

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]

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.

Ok, I got it down it this.

[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.

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:

[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…:confused:

:puzzled:


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?

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 :wink:

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

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.

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: