Hi,
I want to create a timer that counts up. I’ve searched but all I found were countdown timers. Any ideas on how to do this ?
Hi,
I want to create a timer that counts up. I’ve searched but all I found were countdown timers. Any ideas on how to do this ?
iniTime = getTimer();
onEnterFrame = function(){
msecs = getTimer()-iniTime;
// convert if necessary from milliseconds (msecs) to anything else
};
Sorry, my mistake. I didn’t explain well: I’m using it for an mp3 player, so I have to be able to stop, pause, and reset it. Which I don’t think is possible with getTimer();
Thanks for helping though sen
getTimer is just used to get a change in time. In the above code, iniTime is used to get the initial getTimer timer and then its assigned to msecs. If you need to stop and restart it, you just need to reset iniTime and add the new calculated msecs to a general timer variable.
RunTimer = function(){
theTime += getTimer()-iniTime;
// convert if necessary from milliseconds (msecs) to anything else
};
StartTimer = function(){
iniTime = getTimer();
this.onEnterFrame = RunTimer;
};
StopTimer = function(){
delete this.onEnterFrame;
};
theTime = 0;
iniTime = getTimer();
StartTimer();
Hey Sen, please explain:
I placed this at the beginning of the main timeline:
[AS]
RunTimer = function(){
theTime = Math.round((getTimer()-iniTime)/1000)
};
StartTimer = function(){
iniTime = getTimer();
this.onEnterFrame = RunTimer;
};
StopTimer = function(){
delete this.onEnterFrame;
};
theTime = iniTime = getTimer();
StartTimer();
[/AS]
And then used StopTimer(); to stop the timer. It worked, but I don’t understand the first part, getTimer()-iniTime. iniTime is a timer too, so you’re substracting a timer from a timer ? And theTime is a timer too, so you’re actually substracting iniTime from a certain getTimer and store it in theTime.
Now when I place only getTimer()-iniTime in a loop and trace the result, it’s 0,0,0,0,2,0,0,1,0,0,0,3,0,0,0,5, … That’s because you are substracting two timers which started practically at the same time right ? But then why does the whole timersystem work ?
I also modified your code a little bit ( theTime += getTimer()-iniTime was supposed to be theTime = Math.round((getTimer()-iniTime)/1000) ).
Ok, I get all the rest except what I explained before. It worked to reset the timer (whichs equals stop on the mp3 control), but how can I pause and resume the timer ?
Ok, I got everything working the way I want it to, but I can’t figure out how to pause/resume the timer. As I said before, I’m using it for an MP3 player. You can see it here. I can provide the FLA too if you want it.
This is the structure of my FLA:
I use an external swf with data for the equalizer. At the first frame of the main timeline, I put these actions:
[AS]
loadMovie(“ldg.swf”, “_level1000”);//ldg.swf is the datafile
paused = false;
minutes = 0;
RunTimer = function () {
theTime = Math.floor((getTimer()-iniTime)/1000);
if (theTime<10) {
theTime = “0”+theTime;
}
if (theTime>=10) {
theTime = “”+theTime;
}
if (theTime>59) {
NewTimer();
}
_root.timerText = “0”+minutes+":"+theTime;
};
StartTimer = function () {
iniTime = getTimer();
this.onEnterFrame = RunTimer;
};
StopTimer = function () {
delete this.onEnterFrame;
minutes = 0;
_root.timerText = “00:00”;
};
PauseTimer = function (){
currentM = minutes
currentT = theTime
delete this.onEnterFrame
}
NewTimer = function () {
delete this.onEnterFrame;
minutes++;
StartTimer();
};
theTime = iniTime=getTimer();[/AS]
Ok, now the two last frames of my player are a loop, because the equalizer needs to change constantly. So the last frame has [AS]gotoAndPlay(‘Spectrum’);[/AS]
and the frame right before it has the label Spectrum. That frame (Spectrum) has these actions:
[AS]
for (i=0; i<18; i++) {
this[“l”+i]._height = 120-(110-_level1000[“s”+i]1.2);
}
_root.progress._x = 12.8+((161_level1000._currentframe)/_level1000._totalframes);[/AS]
l1, l2, l3, … are equalizer bars and _root.progress is a bar representing the playhead. These all work fine. Now for the playback buttons:
Stop -
[AS]
on(release){
_level1000.gotoAndStop(1);
_root.playing = false;
StopTimer();
}
[/AS]
Play -
[AS]
on (release) {
if (_root.playing == false) {
_level1000.play();
StartTimer();
_root.playing = true;
}
}
[/AS]
Pause (so far) -
[AS]
on (release) {
if (paused == false) {
_level1000.stop();
PauseTimer();
_root.playing = false;
paused = true;
} else if (paused == true) {
_level1000.play();
ResumeTimer();
_root.playing = true;
paused = false;
}
}
[/AS]
But I don’t know how to pause/resume the timer so I can’t put action on the pause button. The song itself pauses and resumes perfectly, it’s just the timer. For the notice; the pause buttons pauses when clicks and resumes when clicked again, the stop button stops and rewinds, and the play button plays from the beginning (it is used with the stop button), but should also be able to resume pause. Can you help me Sen ?
Can i see the fla?
… no Just kidding, of course you can. But you’ll need the data swf, which is quite big (3-4MB) for it to work, otherwise you won’t be able to do much. Do you have MSN Messenger ?
Great, I’m there in a sec.
Sorry Voetsjoeba, i was busy all day and couldnt look into your file earlier.
I think its working now.
Man, you shouldn’t be apologizing at all ! The FLA you gave me works perfectly ! Thanks so much =) !
Glad i could help Voetsjoeba
=)
Hey claudio, I’ve got a few more questions for you. I modified the fixed FLA you gave me, I placed the _root.onEnterFrame actions from the last frame inside the frame 6-7 loop, and the functions that were defined on the last frame, I put those at the fifth frame, because I don’t want too much enterFrames so I’m placing it all in one loop and only defining those functions once. It still works fine.
This player is supposed to have multiple songs. So I created an array at the first frame with objects in it to define the filename, title and artist of each data file. This is how my first frame looks like now:
[AS]
song = new Array();
song[0] = {filename:“ldg”,artist:“Rob Zombie”,title:“Living Dead Girl”}
song[1] = {filename:“dragula”,artist:“Rob Zombie”,title:“Dragula”}
song[2] = {filename:“03”,artist:“Can’t remember”,title:“Can’t remember either :P”}
function loadSong(arraynr){
var temp = song[arraynr].filename+".swf";
artist.text = song[arraynr].artist
title.text = song[arraynr].title
loadMovie(temp, “_level1000”)
_level1000.gotoAndStop(1);
}
_root.songnr = 0;
loadSong(_root.songnr)
[/AS]
… and, the 6-7 loop is now:
[AS]
for (i=0; i<18; i++) {
this[“l”+i]._height = 120-(110-_level1000[“s”+i]1.2);
}
_root.progress._x = 12.8+((161_level1000._currentframe)/_level1000._totalframes);
theTime = getTimer()/1000-iniTime;
goTime = theTime-buttonPressTime;
if (playing) {
minutes = Math.floor((goTime/3600)*60);
seconds = Math.floor(((goTime/3600)*60-minutes)*60);
myseconds = (seconds<10) ? “0”+seconds : seconds;
myminutes = (minutes<10) ? “0”+minutes : minutes;
timerText = myminutes+":"+myseconds;
}
[/AS]
The onEnterFrame part of the script at the last frame was moved to the 6-7 loop, as said before, and the button functions were moved to the fifth frame. So my fifth frame looks like this:
[AS]
function stopTimer() {
timerText = “00:00”;
buttonPressTime = getTimer()/1000-iniTime;
_level1000.gotoAndStop(1);
pause();
}
function pause() {
_level1000.stop();
pauseTime = getTimer()/1000;
playing = false;
}
function unpause() {
unpauseTime = getTimer()/1000;
iniTime += (unpauseTime-pauseTime);
_level1000.play();
playing = true;
}
playbutton.onPress = function() {
if (!playing) {
unpause();
pausebutton.gotoAndStop(1);
}
};
stopbutton.onPress = function() {
stopTimer();
};
previous.onPress = function(){
_root.songnr–;
if (_root.songnr < 0){
_root.songnr = song.length-1;
}
stopTimer();
loadSong(_root.songnr)
gotoAndPlay(3)
};
_root.playing = true;
_level1000.play()
[/AS]
Now when I click the previous button, there is a lag, and then it starts playing the previous song (which is correct), but the timer starts at 00:04. I’ve tried everything but I can’t get that timer right. Everythin seems correct, but still it doesn’t work.
And why does it lag ? gotoAndPlay(3) is the frame where the loading indication is. I tried replacing the code from frame 3 with a preloader, but no luck.
I found it: when the data file isn’t loaded yet, it lags, but the timer already starts. When it is already loaded, it starts playing immediately which makes the timer correct.
So I’d have to load all the data files at the beginning ? No, there’s got to be a better way. There should be either a preloader at frame 3, or a loading indication at frame, not a lag.
For some reason, the lag was gone when I added pausebutton.gotoAndStop(1). But when the previous button has been clicked several times, the equalizer became framy. I noticed that, when a framy equalizer is playing, that when you click play and stop, it becomes smooth again. So I added those two actions (from the play and stop buttons) to the previous button. So that’s why I have this script now:
[AS]function stopTimer() {
timerText = “00:00”;
buttonPressTime = getTimer()/1000-iniTime;
_level1000.gotoAndStop(1);
pause();
}
function pause() {
_level1000.stop();
pauseTime = getTimer()/1000;
playing = false;
}
function unpause() {
unpauseTime = getTimer()/1000;
iniTime += (unpauseTime-pauseTime);
_level1000.play();
playing = true;
}
playbutton.onPress = function() {
if (!playing) {
unpause();
pausebutton.gotoAndStop(1);
}
};
stopbutton.onPress = function() {
stopTimer();
};
previous.onPress = function() {
_root.songnr–;
if (_root.songnr<0) {
_root.songnr = song.length-1;
}
pausebutton.gotoAndStop(1);
loadSong(_root.songnr);
gotoAndPlay(3);
stopTimer();
delete (buttonPressTime);
delete (pauseTime);
unpause();
delete (unpauseTime);
};
_root.playing = true;
_level1000.play();[/AS]
Now the framyness doesn’t show up for the first three or four times, but after that, it still gets framy. And, the timer is messed up when I click the previous button. Any ideas how to fix this ?
Hmm i dunno why thats happening voetsjoeba… i will try to look over your file again.
:: Copyright KIRUPA 2024 //--