Timer In AS3 And C++
Hi there,
I'm writing a multiplayer AS3 game, running atop a C++ server.
The problem is trying to get the two to syncronize nicely; I'm using clock_gettime() (on Linux with C++) which returns secs and nsecs - but the nsecs aren't particularly reliable in that they overflow every few seconds back to zero, so you can't just append the usecs to the secs to get a consistent 'timestamp'.
My question is does anybody know of a way to keep a reliable timer running on both the C++ server and AS3 client? It does not need to be an actual Unix Timestamp, and so can begin at Zero when the server begins (And it'll tell clients whereabouts it is) and accuracy needs to be to about a hundredth of a second - so not terribly taxing - any thoughts would be appreciated!
Cheers
Mark
ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0
Posted on: 04-19-2008, 03:42 PM
View Complete Forum Thread with Replies
Sponsored Links:
Difference Between Timer.stop() And Timer.reset()?
Hello,
What is the difference between Timer.stop() and Timer.reset() functions because it seems that the 2 functions do the same thing? I image Timer.stop() to stop the timer and when Timer.start() is called the timer starts from where it stopped. For example, if I have a 3 sec. timer (3000 ms). If I stop the timer after 2.5 sec. and start the timer again, I would expect the timer to expire/trip within .5 sec. Is this a correct assumption?
Thanks,
Nilang
View Replies !
View Related
Difference Between Timer.stop() And Timer.reset()?
Hello,
What is the difference between Timer.stop() and Timer.reset() functions because it seems that the 2 functions do the same thing? I image Timer.stop() to stop the timer and when Timer.start() is called the timer starts from where it stopped. For example, if I have a 3 sec. timer (3000 ms). If I stop the timer after 2.5 sec. and start the timer again, I would expect the timer to expire/trip within .5 sec. Is this a correct assumption? Code is attached below.
Thanks,
Nilang
Attach Code
// Declarations
var url:URLRequest;// Url
var snd:Sound;// Sound Object
var sndChan:SoundChannel;// Sound Channel Object
var sndPosition:Number;// File offset
var tmr:Timer;// Timer object
var isPlaying:Boolean;// Flag to indicate whether file is
// playing or not
var isPlayDelayed:Boolean;// Flag to indicate whether starting
// of file has been delayed or not
var hours:Number = 0;// Hours
var minutes:Number = 0;// Minutes
var seconds:Number = 0;// Seconds
var milli:Number = 0;// Milliseconds
var pauseTime:Number = 0;// Time when paused
var pauseLength:Number = 0;// Length of pause
var buttonPressTime:Number = 0;//
var timing:Boolean = false;// Flag
// Initialize variables.
isPlaying = false;
isPlayDelayed = true;
sndPosition = 0;
// Get url
url = new URLRequest("sound1.mp3");
// Create new Sound object
snd = new Sound();
// Create new SoundChannel Object
sndChan = new SoundChannel();
// Create a new Timer Object with timeout of 5 sec.
// and repeat it only once.
tmr = new Timer(5000, 1);
// Load the audio file
snd.load(url);
// Enable Event Listeners
tmr.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
stop_btn.addEventListener(MouseEvent.CLICK, onStop);
pause_btn.addEventListener(MouseEvent.CLICK, onPause);
play_btn.addEventListener(MouseEvent.CLICK, onPlay);
this.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
// Start time
pauseElapsedTime(false);
// Start Timer
tmr.start();
// Function that handles timer events
function onTimerComplete(e:TimerEvent):void
{
// Play audio file when timer complete event is received
sndChan = snd.play(sndPosition);
// Set the isPlaying to true to indicate file is playing
isPlaying = true;
// Set the isPlayDelayed to false
isPlayDelayed = false;
trace("onTimerComplete: " + e);
trace("target: " + e.target);
trace("current target: " + e.currentTarget);
}
// Function that captures frame events
function onEnterFrameHandler(e:Event):void
{
// Local variables
var totalTime:Number = (getTimer()/1000)-pauseLength;
var goTime:Number = totalTime-buttonPressTime;
// If flag is true, then calculate the time that has elapsed
if( timing )
{
// Calculate time
hours = Math.floor(goTime/3600);
minutes = Math.floor((goTime/3600-hours)*60);
seconds = Math.floor(((goTime/3600-hours)*60-minutes)*60);
milli = Math.floor((goTime-(seconds+(minutes*60)+(hours*3600)))*100);
// Display elapsed time
timeText.text = format(hours) + ":" + format(minutes) + ":" + format(seconds) + "." + format(milli);
}
}
// Function that captures mouse click events when user clicks stop button
function onStop(me:MouseEvent):void
{
// Reset time
restartElapsedTime();
if (isPlaying)
{
// If audio file is playing then stop the audio file, set file offset to zero,
// and set isPlaying flag to false
sndChan.stop();
sndPosition = 0;
isPlaying = false;
isPlayDelayed = true;
}
else if (isPlayDelayed)
{
// If audio playback is delayed then reset the timer and
// set isPlaying to false
tmr.reset();
isPlaying = false;
}
}
// Function that captures mouse click events when user clicks pause button
function onPause(me:MouseEvent):void
{
// Pause time
pauseElapsedTime(true);
if (isPlaying)
{
// If audio file is playing then stop the audio file,
// set file offset to current offset, and set isPlaying flag to false
sndChan.stop();
sndPosition = sndChan.position;
isPlaying = false;
}
else if (isPlayDelayed)
{
// If audio playback is delayed then stop the timer and
// set isPlaying to false
tmr.stop();
isPlaying = false;
}
}
// Function that captures mouse click events when user clicks play button
function onPlay(me:MouseEvent):void
{
// Start time
pauseElapsedTime(false);
if (!isPlaying)
{
if (!isPlayDelayed)
{
// If the audio file is not playing and playback is not delayed, then
// play the audio file from the last offset and set isPlaying to true
sndChan = snd.play(sndPosition);
isPlaying = true;
}
else
{
// If the audio file is not playing and playback is delayed, then
// start the timer and set isPlaying to false
tmr.start();
isPlaying = false;
}
}
}
// Function that sets the time to 00:00:00.00 (default)
function restartElapsedTime():void
{
timeText.text = "00:00:00.00";
buttonPressTime = (getTimer()/1000)-pauseLength;
pauseElapsedTime(true);
}
// Function that pauses time
function pauseElapsedTime(b:Boolean):void
{
if( b )
{
pauseTime = getTimer()/1000;
}
else
{
pauseLength = ((getTimer()/1000)-pauseTime)+pauseLength;
}
timing = !b;
}
// Function adds 0 to the front of the number when it is < 10 and
// returns a String
function format(n:Number):String
{
if( n < 10 )
{
return ("0"+n);
}
return n.toString();
}
View Replies !
View Related
Timer - Not Timer Class, But Time Taken To Do Certain Events
So i'm making a little game and want to know how to start a timer and display the number of milliseconds between the start and finish of two different events. I do not want to run a function after 5000 milliseconds or whatever, just record the length of time taken to say click two buttons.
Basically a stopwatch.
I saw the getTimer(); function but i'm not sure of how to use it or if that is what i need.
Once again, thanks for your time.
View Replies !
View Related
Running Timer And Timer With Offset
Hello all,
I'm trying to set up two timers in that dare feed the time in minutes and seconds from the server.
Basically, I've got a master running timer in seconds and minutes but also want to have a second cloned timer that is feed some seconds via flashVars to add to the the master time.
Both of these timers once set by the sever time feed in, and with one being offset, are called every second by a setInterval function call so they count up.
Setting up the master running timer isn't a problem but with my coding skills is a little messy..
Code:
_global.masterRunningTimeInMinutes = 59;
_global.masterRunningTimeInSeconds = 55;
if (_global.masterRunningTimeInSeconds>0 && _global.masterRunningTimeInSeconds<59) {
_global.masterRunningTimeInSeconds++;
} else {
_global.masterRunningTimeInSeconds = 0;
_global.masterRunningTimeInSeconds++;
if (_global.masterRunningTimeInMinutes>=0 && _global.masterRunningTimeInMinutes<=58) {
_global.masterRunningTimeInMinutes++;
} else if (_global.masterRunningTimeInMinutes>=59 && _global.masterRunningTimeInSeconds>=0) {
_global.masterRunningTimeInMinutes = 0;
}
}
if (_global.masterRunningTimeInSeconds<10) {
_global.masterRunningTimeInSeconds = "0"+_global.masterRunningTimeInSeconds;
}
_root.runningTime.text = "v"+_global.masterRunningTimeInMinutes+":"+_global.masterRunningTimeInSeconds;
Ideally I wanted to to get the master running time to display MM:SS so if anyone can help with that that would be great!
I suppose the real problem I'm having is is setting up the pattern for determining the cloned time with the offset.
This is becoming a real headache but if anyone can help I would really appreciate it! Basically I just wanted to have the running time with the cloned time always a certain amount of seconds ahead.
Many thanks, amp3
View Replies !
View Related
Timer.
Hi, I am having problems with a timer.
Using the code from one of the movies here on Flashkit, I wanted to add 100th seconds to the movie. Here's the code:
millitime = getTimer()/60000-(int(getTimer()/60000));
seconds = (int(millitime*60));
milli = (int(millitime*6000));
mymin = (int(getTimer()/60000));
if (seconds <10) {
seconds = "0" + seconds;
}
if (Number(mymin)<1) {
mintime = "00";
} else {
mintime = (int(getTimer()/60000));
if (Number(mymin)<10) {
mintime = "0" + mintime;
}
}
minfield = mintime;
As you can see 'milli' is the amount of milliseconds in a minute, and I am unsure as to how to get this to read 00 - 99 in a single field and then restart after 1 second. HELP?
G
View Replies !
View Related
Timer
Let's say I have this timer:
timer = int ((getTimer ())/60000)
When I go to the next frame/stage I want the timer to "reboot". So I thought, why not do this:
timer = int ((getTimer ())/60000)
timer2 = int ((getTimer ())/60000)
And in the next frame/stage:
timer = int ((getTimer ())/60000 - timer2)
But all I get is the same value. At first I thought that If a put a, for example, getTimer value in a var, then it remains constant, keep the same value that the timer had in that specific time. And therefor should my script work, but it doesn't. Why?
View Replies !
View Related
Timer
Hello, i have a question.
How can i insert a visible countdownb timer (20 seconds) in my flash movie and when the timer = 0 i want a certain script to be executed????
sorry for my bad English
View Replies !
View Related
Help With Timer
Hi,
I have posted this problem before but I still can't fix it.
problem:
I want a clock in milliseconds to start on a specific frame and end it on another.
With getTimer() I can't get the clock to start on a frame. It always starts on frame 1 of the main timeline.
PLZ does anyone know a solution ?
View Replies !
View Related
Set A Timer
is there a way to tell a movieclip wait 10 seconds and then goto a certain frame number and play.
Like you just want the moieclip to wait 10 seconds before it restarts.
View Replies !
View Related
Timer
How could I program a timer to count down from 3 and then go to the next frame?
I'm sure it's simple, but I can't program for crap
View Replies !
View Related
Timer
Im using this code to time 30 seconds I know its wrong and would really appreciate help
In the first frame i have
startTime = getTimer ();
then the next
newTime = getTimer();
if ((newTime/1000)-(startTime/1000)>30) {
_root.gotoAndPlay("10");
} else {
gotoAndPlay (2);
}
_root.showTime = (newTime/1000)-(startTime/1000);
View Replies !
View Related
Timer
hi.
i want this function to repeat itself only every two seconds. how do i do that? a "do while" loop using getTimer?
function playBack (note) {
for (i=0; i<musicList.length; i++) {
fscommand ("player1.playNote", musicList[i]);
}
i'm pushing a bunch of notes to an array, and then i want them played back, but i want a little space between them, so they don't play back all at once (which is what they're doing now).
View Replies !
View Related
HELP WITH TIMER PLEASE
I am trying to make a timer that displays the elapsed time of a music track.
I need something that can divide _root.totalframes by _root.currentframe and then multiplies that percentage by the total length of the main timeline (where the sound is) and comes up with something formatted in minutes and seconds.
it must be constantly updating based on currentframe/totalframes because the user can jump to any point on the timeline (or any point in the song) whenever he/she wishes.
see what I'm saying?
View Replies !
View Related
Get Timer (help)
onClipEvent (enterFrame) {
wait = 2;
starTimer = true;
if (starTimer) {
if (startTime == undefined) {
startTime = getTimer()/1000;
}
curTime = getTimer()/1000;
if ((curTime-startTime)>=wait) {
starTimer = false;
delete startTime;
// actions to do after time is up
_root.any.play();
_parent.removeMovieClip("newcode");
} else {
_root.timeRemaining = wait-(curTime-startTime);
}
}
}
Hey guys,
here above i've a timer script, which after every 2 secs play a movie clip. the script is perfectly functioning properly. the only problem it has, it goes into the loop. what i mean, after every two secs, it plays the mc. though using the attachmovie method, i can remove it after the first two secs. but can i modify the script, not using the attach movie method, so that it plays only once, i.e only after two secs & then terminated.
also, the above script fails to run, if given onClipEvent(load). can i know the reason why.
please help, guys!.
ravi.tandon@learningmate.com
View Replies !
View Related
TIMER
How do i make a timer the format is like this:
Minutes:Seconds:miliseconds
I need it to start at 00:00:00 and go to 38:00:00 how do i do this??????
PLEASE HELP!!!!!!!!!!!
View Replies !
View Related
60 Sec. Timer
I would like to refresh a movie by including in it a timer that counts 60 seconds or any other time and then goes back to frame 1 or any other label. the problem i have with getseconds is that when it is 00 it counts from the begining, reaching only 60 when it is invoked at precisely 00.
View Replies !
View Related
Timer
hello i need some help on how to make a timer in flash 5. I have little actions scripting and i would like to once someone clicks a button it would wait 5 sec untill it loads the next frame. Hope some one can help. THanks
View Replies !
View Related
Timer
OK I've got a timer working the displays a different msg depending on the time of day Ie. 9:00 = Good morning etc
Now I want it to be a different msg on a different day and still at a certain time, Understand??
Any Ideas thanks
View Replies !
View Related
First Timer
hey people 1st time using this sight and flash... i've made an intro page using swish/flash mx and want it as the intro for my frontpage2002-created website, but how do i link the enter button so it takes me to the fp2002 index.htm? by the way i've never done this stuff b4 so it needs to be simple!!
View Replies !
View Related
How To Set A Timer
my question is:
if i want to start a timer after pressing a button and stop it by pressing another one.
do you have any idea about it.
i want to set a sensitive timer to the nearest milliseconds.
should I use the object actionscript "Date"??
Thx for your attention!
=)
View Replies !
View Related
Timer
Hi, I want a timer on my site which starts to, count from zero when a user enters the site. It has to be in hours, minutes, seconds and milliseconds.
Can anyone please please please help me???
Thanks
View Replies !
View Related
Timer
Hi all
I have this action placed on button from 1 to 8
on(press){
capture.n = 1;
}
To
on(press){
capture.n = 8;
}
How do I build a timer that calls a function every 3 seconds. from 1 to 8 on clipevent?
thanks
PilotX
View Replies !
View Related
Timer?
does anyone know the code is for a timer? a timer for a game i mean. so it starts when u do something and it stops when u do another thing.
View Replies !
View Related
Please Help With Timer
I am trying to use a count down timer in movie in Flash MX. However I wish for the timer to play a sound when there is an odd number od seconds on the display, and a different sound when there is an even number of seconds which is being displayed - I guess I need to create 2 seperate movie symbols to house the sounds but how would I addapt the current script below to trigger them??
Thanks
Current Script:
currentDate = new Date();
// note: flash starts to count months from 0, so January is month 0 and December month 11.
// the date here is expressed as follows: Year, Month, Day, Hour, Minutes, Seconds.
endDate = new Date(2002, 11, 31, 23, 59, 59);
days = (endDate-currentDate)/1000/60/60/24;
daysRound = Math.floor(days);
hours = (endDate-currentDate)/1000/60/60-(24*daysRound);
hoursRound = Math.floor(hours);
minutes = (endDate-currentDate)/1000/60-(24*60*daysRound)-(60*hoursRound);
minutesRound = Math.floor(minutes);
seconds = (endDate-currentDate)/1000-(24*60*60*daysRound)-(60*60*hoursRound)-(60*minutesRound);
secondsRound = Math.round(seconds);
// Check when to put day and when to put days
if (daysRound == 1) {
dy = " day ";
} else {
dy = "days";
}
if (secondsRound == 60) {
secondsRound = 0;
}
if (minutesRound == 60) {
minutesRound = 0;
}
if (secondsRound<=9) {
secondsRound = "0"+secondsRound;
}
if (minutesRound<=9) {
minutesRound = "0"+minutesRound;
}
if (daysRound<=-1) {
timeRemaining = "endDate introduced!";
} else {
timeRemaining = daysRound+dy+hoursRound+":"+minutesRound+":"+secon dsRound;
}
View Replies !
View Related
Timer
Hi, I just wanted to ask if I could get some help on how to create a game that has a timer. I want it to start at 60 secs and then I need it to count down so the user knows how much time they have left.
Would greatly appreciate any flash guru's help =)
Thanks
t.
View Replies !
View Related
Need To Set Up A Timer, Please Help
To use these coupons, simply print this page and bring the coupon in!
I have a flash file that reads in text from a text area. I need to figure out how to create an actionscript that will close the entire browser window if the text area is not being used for X amount of time. I am not very good yet with actionscript. I figure the basic code would go something like this:
1: person enters text, timer begins (set to zero)
2: if the timer hits X amount of time, send a command to the browser to close the window.
ActionScript gurus, please help me set this up.
Thanks in advance Folks!
View Replies !
View Related
Timer
im working on a simple target shooting game and i need a way to have the targets dissapear after about 5 seconds. any ideas on what i could try?
View Replies !
View Related
Timer
Yo everyone I want to make a timer
I.E
Dynamic Text box that starts say at 100 and goes down to 0 and when it hits Zero it will say a message.
Im not very advanced but i know alot of stuff in it just that i never bother using Timers becuase all games and movies i made never needed one really.
Please reply
View Replies !
View Related
Timer
i have an adventure game im working on. i need a timer which runs throughout the whole game to use as a way to find out how long the person took to win.
my first question is, how do you make the timer?
second, im using it with an adventure game. there are different segments, each segment is a small game,that the player has to go through. the segments would be on different scenes, or possibly different movies. is there a way to remember the current time when you finish a segment and start that exact time up again in the next scene?
lastly, could i make a frame in each scene as a pause frame. the player clicks it, the time stops where it is and starts from the same spot when they resume. is this even possible?
if you can answer any of these questions, please do. thanks in advance.
View Replies !
View Related
Set A Timer?
How can I set a timer that I can have tick down from 60 seconds no matter what fps i am using and use any font of my choice?? Please post an example .fla for me if possible. Also I need it so that it can be displayed over many frames... (i.e. frame 2,6,8,9 have the timer but frames 1,3,10 do not.)
Thanks
View Replies !
View Related
Timer...
Hello,
does anyone knows code to creater a timer? i would like to have something which would count and display hours, minutes, days, seconds and be precise to 0.001 second or miliseconds.
Thanks,
Miguel
View Replies !
View Related
Timer
Hi,
i just created a timer with 2 frames and to show hour, minute, seconds and miliseconds. However the miliseconds part doesn't work very well. Maybe i should do it with setInterval and with only one frame...and maybe a function. Could someone tell me how to do it?
In this moment my code is:
mydate = new Date( year, month, date, hour, min, sec, ms );
function doubleDigit(nValue) {
return (String(nValue).length<2) ? "0"+nValue : String(nValue);
}
nHours = mydate.getHours();
nMinutes = mydate.getMinutes();
nSeconds = mydate.getSeconds();
msec = mydate.getMilliseconds();
sHours = doubleDigit (nHours);
sMinutes = doubleDigit (nMinutes);
sSeconds = doubleDigit (nSeconds);
timecode = sHours + ":" + sMinutes + ":" + sSeconds+ ":" + msec;
Thanks,
Miguel
View Replies !
View Related
Timer
(i already posted this question in dutch but just in case here's another one)
I need a timer fot my game. A timer which counts down from 5min for example. I want something to happen when the timer hits 0. I know how to do that, but i don't know how to make the timer.
(It needs to be displayed in a dynamic textbox)
thank you!
View Replies !
View Related
Timer Help...
i am trying to find a timer that tells the person the amount of time they have been playing a game. where it starts from 0 and goes, 1, 2, 3... can u help me? ( i am using flash 5)
View Replies !
View Related
Timer
I need a piece of code that will after an x amount of time clear a dynamic text field to nothing.
The user clicks the wrong button, a suggestion appears in a text field, and i want this to clear afer say 10 seconds.
Cheers
lee
View Replies !
View Related
Timer HelP?
do i need to use the getTimer() to count time or how else can do it?
on(keyPress "n"){
_root.mc.timerStart = "yes"
}
onClipEvent(enterFrame){
if (timerStart == "yes"){
startTimer()///this is part i need help with
if (timer < t){
mc.gotoandPlay(1);
}
}
}
View Replies !
View Related
Bar Timer?
Ok... I wanted to make a 10 second timer made out of bars. For example those preloaders that shows how much completed using a bar that goes to 100%. For the timer I wanted is it counts up to 10 then after that it stops for now. And plz tell me that how to restart the timer again. Thanx
View Replies !
View Related
Timer
Im making a game where when u click on a button, a timer start and when u release it stops. I need the timer to have seconds minutes and hours. thanks in advance
View Replies !
View Related
Timer
hi there,
i want the playhead jump from frame 1 to frame 2 after a certain time.
i had this problem before, though i cant remember the code, wasn`t that something like gettimer or so? hows the code?
thanx u777
View Replies !
View Related
Timer
I am working with Flash MX v. 6.0 I have created a site for a company that exclusively runs in their facilities on large plasma monitors. This employee info site includes video, text and flash animation. What I would like to be able to do is have the site start at certain times say 10:30 and then run its loop Appox. 7 times and then shut down at 10:45 ( this is to coincide with breaks etc. ) The site is currently just using swf files, and the files are downloaded to the different locations via a VPN. Any suggestions would be appreciated.
James
View Replies !
View Related
Timer
I need some help with actionscripting a timer that counts up from the time its loaded in hours:mins:secs just like that...
View Replies !
View Related
Timer
i am pretty new to action scripting and was wondering if someone could give me the script to make a timer that counts down from 3 mins(3:00 to 0:00) if any one could tell me how to make this it would be greatly appreciated.
View Replies !
View Related
|