A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: [F8] Game time is different in every browser

  1. #1
    Junior Member
    Join Date
    Jul 2006
    Posts
    16

    [F8] Game time is different in every browser

    So far, the race game I'm developing runs faster in IE than in Firefox. If I play the game in IE, I finish the course in 25 seconds, if I use Firefox, I finish in 34 seconds. Why is this, and is there a way to resolve it?

    Thanks in advance.
    Last edited by countdown321; 03-26-2008 at 03:49 PM.

  2. #2
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    You should probably try a frame-based timer rather than a real-time one then... That way, even if your game runs at 2 fps because of the slowdowns, it'll work.

    You know, something as simple as:

    PHP Code:
    _root.onEnterFrame = function(){
    Timer++

    ...And when you finish the race (or everytime you need to display it visually), transform the Timer variable into minutes/seconds...


    NOTE: your question works great with your username

  3. #3
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Marmotte, I would've thought it'd be more sensible to do it the other way round?
    http://www.birchlabs.co.uk/
    You know you want to.

  4. #4
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    Quote Originally Posted by VENGEANCE MX
    Marmotte, I would've thought it'd be more sensible to do it the other way round?
    Hey Vengeance! What do you mean?

    Using time-based instead of frame-based? I don't think so, because if you are getting many slowdowns in your game, then instead of finishing it in 10 seconds, you'll finish it in let's say 14 seconds even if you played exactely the same way...

    or Am I wrong?

    Oh btw countdown321, "Timer++" was just an example, ideally you should do "Timer--" (Flash calculates it faster)

  5. #5
    Senior Member hatu's Avatar
    Join Date
    Jan 2007
    Posts
    480
    Whoops didn't notice that you did mention converting it to seconds and minutes at the end. Could you get a realtime time display with this though?

    Isn't there still a problem though. 60 fps would run twice as fast as 30fps?
    Last edited by hatu; 03-26-2008 at 05:31 PM.

  6. #6
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I would use getTimer() for something like this. getTimer() gets the real time so even with lag or slowdown, the results would even be the same. Also, you could look up time-based movement instead of frame-based style.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  7. #7
    DOT-INVADER marmotte's Avatar
    Join Date
    May 2002
    Location
    Dot-Switzerland
    Posts
    2,601
    Quote Originally Posted by hatu
    I'm not sure that would work, atleast not alone. You're just counting frames instead of seconds.

    With this method wouldn't the timer run twice as fast at 60fps than at 30fps
    Well sure

    When I wrote "transform the Timer variable into minutes/seconds...", I meant that you have to take into account the FPS set for your game...

    For example, 1st frame of your game:
    PHP Code:
    var FPS:Number 30 // if your game runs at 30fps 
    Then of course, you have to calculate Math.round(Timer/FPS), that will give you the exact amount of seconds used in the game. If it needs to be more precise than seconds, just get rid of the Math.round()

  8. #8
    Senior Member AzraelKans's Avatar
    Join Date
    May 2002
    Location
    Hell... with frequent access to heaven ;)
    Posts
    409
    Actually Ive been using tonypa method and it works pretty well

    Simply do

    Code:
    var initTime:Number=getTimer();
    var gameTime:Number;
    
    var speed:Number=10 //speed in pixels per second.
    
    function tick(){
       var currTime:Number=getTimer();
       gameTime=(currTime-initTime)*0.01
       initTime=currTime;
    }
    
    onEnterFrame=function(){
     tick();
     myhorse._x+=speed*gameTime;
    }
    gameTime stores a float number that when multiplied makes the horse move at the exact same speed no matter what is your FPS so it will achieve the same distance at 10 seconds, at 1 fps, or 120 fps. go ahead try it.

    BTW this is the method they use in XNA and some 3d engines too (although they calculate gameTime for you). so get used to that variable.
    Last edited by AzraelKans; 03-26-2008 at 06:31 PM.

  9. #9
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    yea that is the best way to do any game IMO...it is also crucial when doing a multiplayer game as well. I just wish Box2D used something like this...

    Anyways, great advice.

  10. #10
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    Marmotte, your code still depends on the enterframe, and that's the whole problem. This topic has been up a couple of times as IE and FF handle Flash very differently. Latest example was a li'l game of Squize's (Valentine's Day I think), that didn't play as well in FF. You need something like Azraels code or IP's getTimer().
    Personally I prefer setting the framerate high, checking if my timeinterval of say 20 milliseconds has passed and if so run my game loop, so I don't place it on the variables directly as Azrael but rather just:

    if(timeinterval > 20 milliseconds){
    handle whatever key input has been put on queue
    do game actions
    paint to screen
    }

    This setup works like a charm, makes for sensible code and is cross-browser friendly, so everyone gets the same experience
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  11. #11
    Senior Member AzraelKans's Avatar
    Join Date
    May 2002
    Location
    Hell... with frequent access to heaven ;)
    Posts
    409
    Phreax:

    Thats a great idea and is exactly what I was looking for to prevent some issues Im having, but you would still to use the gameTime variable, at least in this case, since the problem was that the horse was finishing the race before or after in certain browsers. if it only used your code it could still be off heres how:

    Lets suppose in tick 1 the horse moves 10 pixels forward in 20 ms. Then in the next the computer hiccups (someone send an msn message with a mp3 or something) and he does it until 60 ms, that horse is going to be 50 pixels behind a horse with a machine with no hiccups even if they are moving at the same speed.

    gameTime is needed in order to be completely sure they mantain exactly the same distance. it kind of sucks to have to check all speeds against it, but if you implement it in a class and leave it handle it internally is not as painful.

    btw if the game looks a bit slow after using the gameTime variable you can try changing the number of ms in the formula

    from
    gameTime=(currTime-initTime)*0.01
    to:

    gameTime=(currTime-initTime)*0.5
    The speed will double, it will move X pixels each half a second. (or whatever number your likw)

    Btw the *0.01 is an optimization of /100 so you dont have to check for a zero divide.
    Last edited by AzraelKans; 03-26-2008 at 08:03 PM.

  12. #12
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    Oh that is news to me. I had all had the impression that the timer model was much faster and more stable than just enter frame...*shrug*

    Alright, so set it up so that you have a high frame rate, and do a check to make sure you do not run the game unless x time has passed...and then to get your game actors to all animate at the same speed regardless you do the following, which is a dumbed down version of the code I use in my game:

    Code:
    //game initialization
    var newTime:int, oldTime:int, timeStep:int;
    newTime = oldTIme = getTimer();
    timeStep = 0;
    Code:
    //your global update function
    function update(){
    newTime = getTimer();
    timeStep = (newTime - oldTime) * .001;
    oldTime = newTime;
    
    var i:int = 0;
    while(i++ < gameActors.length){
    gameActor.update(timeStep);
    }
    }
    Code:
    //your actors internal update function
    
    public function update(timeStep:int){
    _x += _vx * timeStep;
    _y += _vy * timeStep;
    _rotation += _vr * timeStep;
    }
    
    public function render(){
    _display.x = (_x - worldX) * _scale;
    _display.y = (_y - worldY) * _scale;
    _display.rotation = _rotation;
    }

  13. #13
    Junior Member
    Join Date
    Jul 2006
    Posts
    16
    Hello all,

    Thank you very much for your suggestions, I didn't know that there were lots of options for this problem. I'll try them all out and find out which works best for me.

    And Marmotte - uh, yeah, my username does work great with my question - LOL

    Cheers!

  14. #14
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Frankly a simple count of the frames is the best solution here.
    I highly doubt switching everything to time based animation is a worthwhile step at this point. Counting frames is accurate and requires nothing special.

    Just transform the frames into seconds/minutes when displaying it. No, it won't actually be an accurate count of the time played, however it will accurately represent the number of frames each person required to finish. It fixes the problem and wont be too noticeable.


    On the other hand, you could switch to time based animation, scrapping a large portion of what you have and potentially introducing bugs that you may not expect.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  15. #15
    The Cheeze to Your Macaroni ColbyCheeze's Avatar
    Join Date
    Dec 2007
    Location
    Texas
    Posts
    244
    Well this all depends on his problem and what he is trying to achieve. If all he wants to do is get accurate time recording as to fight cheating...then the simple solution of counting frames would be fine. On the other hand, if he is wanting his game to run the same graphically for any user regardless of framerate then the time based solution is best.

    For a project this close to completion, frame counting is great...however on a larger scale I think this thread was good for informing him about the best solution to programming any future games as well. Also if he wanted to do any type of multiplayer in his race game (which I know is probably not the case, but still) then the time based solution would work best.

    Anyway, it's all a matter of the need.

  16. #16
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    Time based is also better to prevent cheating, as it won't help to slow down the game - with framecounting it will. Obviously both approaches have their pros and cons and if we're talking perfect timing and all this, I don't think it's what Flash was intended for. For a quiz that you want 10 secs to answer a question, the time-based version is still the only thing working! And you really shouldn't be fooled - I told Squize his game was a bit dull and slow...and yes we're talking Squize here, so I knew something was off. That was in FF. The next day I tried it in IE and it played like Super Mario! Therefore - and until the browsers can agree on speeds I think it's safer to go time-based and set up your games accordingly.

    With all this said, they made Flash to depend on EnterFrame, so who am I to say otherwise
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  17. #17
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Great thread guys, and just in time for me to go back and fix my engine to time based, heh. Not really that necessary for me, but probably good practice. Certainly something for me to put in my flash bookmarks for any future games that do any time based scoring.


    Edit:

    Before I do this, let me understand, using this gametime variable will make a ball move across the screen at a fixed speed and will just 'jump' more or less between frames as the computer load increases and decreases, right? So instead of slowdown in busy situations you just have objects that will travel further in each frame?

    Sounds like the biggest issue would be to make sure it does not slow down so much you start missing collision checks...
    Last edited by Alluvian; 03-27-2008 at 03:38 PM.

  18. #18
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    First of all, this "jump" usually won't show. Second, with Azraels example, you get what you describe, if you put it all in a time-controlled loop, you just get the same experience across all platforms and CPUs (unless they really suck and can't keep up with the game logic).

    What it actually does is:
    You speed up flash by setting the framerate high (say 60 fps)
    You then set the timeinterval to be maybe 50 milliseconds, which would be 20 fps
    On each enterframe it will then test if 50 ms has passed, which means, that it will run the game code approx. every third time. Now approx. is the keyword, as this is not necessarily accurate. It will however be the same in all browsers.

    Azraels code handles the game logic on every enterframe but the move is calculated based on the time which makes for an accurate time-based projection but may "jump" a bit more!

    So the reason I use the first method is that it's an easier way of handling the code and I'm really into comfort
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  19. #19
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Ressurecting this topic because it helped me a ton and I want to verify something and caution some people.

    If you are Using AzraelKans (taken from TonyPA I think) post and the gametime variable, make sure you take out any framecounters that affect gameplay. For instance, I had some particles that decayed after 100 frames.

    Without gametime, they all went the same distance given the same per-frame speed, but as the game got more complex, they slowed down, which really sucked for gameplay, and made it feel really really sluggish of course.

    With gametime the particles now had a speed based on TIME, so as the game bogged down, the particles would start to travel further, because they were going further on each frame due to the gametime and the reduction in fps. So if you have any countdowns based on frames they should be changed to time based and instead of incrementing or decrementing a counter, make a comparison to gettimer. If using AK's code, basically compare the time to currTime so you don't have to keep running the gettimer statement.

    Does anyone who use gametime ever find a need to put a cap on the value just in case of extreme slowdown for any reason? I could see a severe hiccup causing missed collisions.

    If gametime was capped at some value, that hiccup would just mess up the socring slightly and not break the game all together. I guess it depends on what you find is more important, score, or game stability in times of severe cpu lag.

  20. #20
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Another question regarding the gametime method.

    Does anyone have a nice bit of code to handle particle emitter timing?

    The particles in my game are part of gameplay, so the amount being emitter per second is important. Right now they just emit one particle every frame, which is not correct of course, and causes the rate of emission to be based on the intervals between frames.

    I know that the gametime variable and a variable for 'particles per second' is enough information to solve the problem, but I am not sure how to do it exactly.

    It will have to involve a loop since a major slowdown could involve multiple particles being emitted per frame.

    Something like
    PHP Code:
    //gametime already calculated
    //some decimal remainder value from the last frame
    var flowrate:Number 10// number of particles to emit per second
    var particlesthisframe = ????  //calculation involving remainder, gametime, and flowrate
    remainder += particlesthisframe Math.floor(particlesthisframe)
    for (var 
    1<= particlesthisframei++) {
        
    //emit a particle

    I think I am just having a hard time determining what exactly that gametime variable is...


    [edited because I stupidly used second instead of 'frame']
    Last edited by Alluvian; 04-10-2008 at 06:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center