Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm new to Flash Actionscript because my programming class uses it. I normally use C++ (or a variant of it) and have dabbled in Java, so Actionscript was mostly familiar to me.

However, whenever I use a while loop, AS3 crashes after 15 seconds. I need to use a while loop otherwise the scope of the entire code will end and the game will stop running on code I presume. In my normal programming language, while (true) will hang the game unless I have Waitframe(); somewhere in the code to let it progress a frame. But I search, and Actionscript has no such thing, and all I've found are "Infinite loops are the devil aaaaaaaa".

Soooo, how am I supposed to be able to make a game with this? I want my game to last more than 15 seconds, yet AS3 "helpfully" terminates the script should it "hang", despite me doing stuff (although that stuff doesn't really show up at all, presumably because the script hangs). Did I miss a wait function that allows for prolonged while loop usage, or am I doing it wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

Unlike C/C++ environment, Flash Player (which initially was much less into the script and much more into animation) is not a real-time environment and works in phases, often referred to as frames (a basic Flash Player cycle consists of advancing the playhead to the next frame of any present timeline/MovieClip).

So, each phase goes in two steps:

  1. Script execution.
  2. Rendering the stage.

So, if you want to control something so that user sees as smoothly animated, you need to run a script every frame. Lets say, you have a box you want to move smoothly to the right.

var Box:DisplayObject;

addEventListener(Event.ENTER_FRAME, onFrame);

function onFrame(e:Event):void
{
    Box.x++;
}

Normally, Flash Players tries to keep the FPS as close to the settings as it is possible. However, if your scripts (which is a handlers for various events, not only ENTER_FRAME, but also keyboard events, mouse/touch input, network events, etc.) run for too long and/or rendering is too heavy, Flash Player will not be able to keep FPS right.

That's also why there's a limit on script execution phase, set to 15 seconds by default. Flash Player suggests to abort the script running for too long as possibly malfunctional.

Then, the solution to your problem depends on what you are trying to do there, but in any case you must finish things and let Flash Player to proceed in order for your app to function normally.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...