开发者

Flash game timer interval changing to half

We have a flash game being developed in actionscript 3 and it runs much more slowly on our remote server.

The game specs:

  • 6-8 different movie clips, all 1 frame static png files from 10x10 to 100x100
  • anywhere from 10 to 60 instances of those MCs on screen at any given time
  • event loop is run from a flash Timer at 25ms
  • all interaction is via the keyboard

The strange thing is, we publish the swf file and run it locally and everything is fast. We copy the swf to a remote server and everything runs much more slowly. The music plays at normal speed, but the movieclip开发者_开发技巧s all move around the screen at about half speed.

Why would the file run differently when fetched from a remote server? Isn't the swf always being run locally anyway? Also, we ran it locally and remotely with the task manager running and it does not seem to consume much CPU or memory.

EDIT: Ok, this is really weird. I added a framecounter textfield to the game. It reports the number of ms since the last timer tick. It consistently reports 50ms. My timer is clearly set to 25. This explains the half speed, but what explains this? I also added a check on the timer.Interval property to see if it had changed and it hasn't; it's still set to 25 while the game is running.

DOUBLE EDIT!: I found this article: http://www.bit-101.com/blog/?p=910, and switched my code to use ENTER_FRAME instead of timer, and it works. I get the full 40fps, give or take a few %. Still not sure what's wrong with the timer though; if it was being delayed by the time spent in my loop it would fluctuate, but it doesn't. It's 50ms every time no matter how much is going on.

Here's the code I'm using(more or less):

t = new Timer(25);
t.addEventListener(TimerEvent.TIMER, timerTick);
t.start();

and in the timer tick code I'm using getTimer() to clock the time between ticks. Comes out to 50ms every time.


I've had some problems with the Timer class before, using it to run my game loop. What's the fps of your SWF? I've found the 2 of them to be linked.

Previously, I thought I could use a SWF FPS of 2, while setting the Timer to give me 30FPS. What was happening was that the SWF would frame tick, it would check the Timer class. The timer might fire an event. Then, a half second later (the SWF was set to 2FPS), the SWF would tick again. The Timer class would then try to fire 15 events (Timer was set to 30FPS). There also seems to be a limit in the number of events it could fire at once as it would only ever fire 5 of them at a time.

Switch to ENTER_FRAME - it also lets you throttle your frame rate if you need to.

Also, if you're not already using it, learn to use delta time to control the movement of your game objects.

private var m_lastTime:int = 0;

private function _onEnterFrame( e:Event ):void
{
    var currTime:int        = getTimer();
    var deltaTime:Number    = ( currTime - this.m_lastTime ) * 0.001;
    this.m_lastTime         = currTime;

    // then, if you want your object to move at 10 pixels per second, it's:
    myObj.x += 10 * deltaTime;
}

Then, no matter the speed of your computer, your objects will move at a constant speed.

If you're making a physics game, then you'll also need to think about fixing your timestep to get more consistent results: http://gafferongames.com/game-physics/fix-your-timestep/

Bonus link: http://www.8bitrocket.com/2008/04/08/tutorial-creating-an-optimized-as3-game-timer-loop/


An SWF is always executed locally by the player. Possible differences that come to my mind are:

  • locally you use the desktop Flash Player, not the plugin/activeX
  • the swf is not in the same HTML page, the online version may include other flash animations (ads)
  • the online html page sets the flash's wmode to transparent whereas the local one sets it to window
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜