dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: making long-running scripts iterative?

  1. #1
    Senior Member maksum's Avatar
    Join Date
    Oct 2002
    Location
    Portland, OR
    Posts
    118

    making long-running scripts iterative?

    If you have read this article you would have seen the authors suggestion to "make sure that any functions that could run long are iterative".

    I want to be clear on exactly what this means and hope someone can clarify it for me.

    I ask because I have run into issues when I know that a script is pretty darn long but I know that it will finish at somepoint - I just have a lot of calculations to make and scenarios to go through, but Flash will crap out on me and give up.

    Is the author suggesting that you just put a timed interval on the function each time every ".X" seconds or something?

    That's what I'm guessing, but I'm just not sure. Any information, insights, or even code examples would be greatly appreciated.

    Mike



    Pasted below is the complete paragraph that discusses this issue:

    Long-running scripts can cause the dreaded “A script is running slowly” dialog to pop up. This is incredibly unprofessional in a game, and if the user hits “Yes” on the dialog, it will break the game, because it kills all scripts from then on. For the downloadable version, it’s possible to set the timeout before this dialog comes up by hacking the SWF. Better still, though, is to simply make sure that any functions that could run long are iterative. A level generation function will need to return back to the system every once in a while so Flash doesn’t think that it has hung. Remember that there are machines of all kinds out there – have such a function return to Flash no later than half a second per increment, just to be safe.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    The article is suggesting that in place of a loop or recursive function (or other code which takes a long time), you replace it with an onEnterFrame handler, or a function called via setInterval which only does a little bit of the processing at a time.

    Here's an example.

    Let's say you have a loop in the code which repeats a million times, like so:

    code:

    for (x = 1; x <= 1000000; ++x)
    {
    performfunction(x);
    }



    This would take a long time and lock up Flash. Flash will not execute animation while the loop is running. If the loop takes longer than 10-15 seconds, you'll get the dreaded dialog the article talks about.

    Instead of doing the loop, you do a little bit at a time, for example via an onEnterFrame handler, like so:

    code:

    x = 0;

    _root.onEnterFrame = function()
    {
    ex = Math.min(1000000, x + 10000);
    while (x < ex)
    {
    performfunction(x);
    ++x;
    }
    }


  3. #3
    Senior Member maksum's Avatar
    Join Date
    Oct 2002
    Location
    Portland, OR
    Posts
    118
    Thanks! So what about the time interval option? (as opposed to on enter frame) Is one recommended over the other?

    Mike

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