Can I prevent Flash's Input Events from stacking up when my framerate slows?
My Flash game targets 24 fps, but slows to 10 on slower machines. This is fine, except Flash decides to throttle the queue of incoming MouseEvent and KeyboardEvents, and they stack up and the Events fall behind. Way behind. It's so bad that, at 10 fps, if I spam the Mouse and Keyboard for a few seconds not much happens, then, a开发者_运维百科fter I stop, the game seems to play itself for the next 5 seconds as the Events trickle in. Spooky, I know.
Does anyone know a way around this? I basically need to say to Flash, "I know you think we're falling behind, but throttling the input events won't help. Give them to me as soon as you get them, please."
You'll probably have better luck working to increase your framerate instead of trying to work around a side-effect of the low framerate problem. Have you used a profiler to identify why the framerate is so slow? Can you utilize invalidation to improve it? Are there bottlenecks that can be optimized?
I don't think flash is throttling the input events. I think it's more likely that your application truly cannot process them quickly enough. I think the only way to stop them from 'stacking up' in this way, would be to completely discard events. So you'd need something in your code like:
function onKeyPress() {
if(inputIsTooFarBehind())
return; // skip this keypress
// process keypress as normal
}
But there's a drawback...users will now have some of their commands discarded, and will simply click more and more to try and get their commands to work.
Perhaps you should work on designing the game in such a way that doesn't need such excessive key/mouse presses? Or go with Sam's suggestion to optimize the game itself to prevent it slowing down in the first place.
精彩评论