Reputation: 347
I'm trying to develop a game in Actionscript 3, but sudden and frequent lagspikes make the game very uncomfortable to play.
Currently the entire game is simply moving a square, so there are no heavy calculations that should cause this.
I have tried using both Timer and Enter_Frame events. They both are pretty unstable. I have also tried different framerates.
Is there anything I can do to prevent this?
//var timer = new Timer;
//timer.addEventListener(TimerEvent.TIMER, time);
//timer.start();
stage.addEventListener(Event.ENTER_FRAME, time);
function time(evt:Event):void
{
now=(getTimer());
dt = now-then;
trace(dt);
then = now;
}
Gives following output (attempted FPS: 100):
3 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 9 11 30 56 49 5 10 11 10 11 8 11 10 9 11 9 11 10 10 10 10 9 10 10 10 14 7 9 14 6 10 10 11 9 10 10 11 9 10 10 11 10 9 11 9 10 10 11 9 11 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 29 7 5 5 14 4 6 10 10 10 10 10 10 10 10 10 10 10 10 11 9 10 10 10 10 10 10 10 10 10 10 10 10 10 9 11 10 10 9 12 8 11 10 10 10 10 10 10 12 8 9 11 10 10 9 11 10 10 11 10 9 10 10 11 10 9 10 11 10 10 9 11 10 10 10 10 10 9 11 10 10 9 11 10 9 10 10 11 9 10 10 10 10 11 9 10 11 9 11 9 10 31 6 5 4 6 8 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 9 11 11 9 10 10 10 10 10 10 11 9 10 10 10 10 10 10 10 10 10 10 10 10 10 10 11 9 10 10 11 9 10 11 9 10 11 9 10 10 10 9 11 14 6 9 10 11 10 11 10 9 10 11 10 9 11 9 15 5 11 35 15 42 64 37 15 6 5 10 11 9 10 11 9 10 11 9 10 10 10 10 10 10 10 10 10 10 10 28 63 65 63 8 15 56 57 13 6 13 12 6 11 36 69 20 25 12 6 6 5 5 33 36 57 20 34 21 12 9 13 6 6 5 9 6 9 7 7 10 10 10 11 10 10 10 10
I have uploaded a swf to demonstrate the problem (60 FPS here). Here is the link. (move with keys). As you will experience, the square moves smoothly for most of the time, but frequent lagspikes makes it uncomfortable.
EDIT: It seems like the application runs much smoother when all the code is in the FLA timeline compared to when most of it is in different AS files. Any possible reason for that?
Upvotes: 0
Views: 124
Reputation: 712
Use Adobe Scout to troubleshoot performance issues. It's a free one-stop-shop for this kind of ordeal. It exactly maps out what components, and how long they are taking up computing resources inside each frame.
Upvotes: 2
Reputation: 5255
First of all 100fps is a very high frame rate. As you can see it is never able to reach the desired fps.
You should go for a lower frame rate that is actually reached and not something over the top. A lot of games use 60, but 30 is also common.
As you experienced yourself: a frame rate is not stable. As soon as you do anything, calculations have to be made which may or may not take longer than one frame to calculate which results in a lag.
The solution is what you do in your question: make your game based on a time difference between frames. If 3 ms passed since the last frame, calculate what state your game should be in after 3ms. If a different time passed, say 5ms, use that and so on.
This allows the game to run with a consistent speed, even though the update of the display lags behind.
Upvotes: 0
Reputation: 1582
Stick to 30fps.
Not sure if the below will help make a difference to the performance. but try doing as:
var now = 0;
var then = 0;
var dt = 0;
stage.addEventListener(
Event.ENTER_FRAME,
function(e:Event):void
{
now = getTimer();
dt = now-then;
//trace(dt);
then = now;
}
);
Upvotes: 0