Infinite Loops (Action Script 3)

淺唱寂寞╮ 提交于 2019-12-13 08:39:45

问题


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?


回答1:


Flash works on a "Frame" system, essentially a timer by frame rate. Put all your game code in a new function and call that function using an event listener.

stage.addEventListener(Event.ENTER_FRAME, function(e:Event):void {
  // Your code here
  // Called every frame
});



回答2:


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.




回答3:


Another point, is that you don't want a truly infinite loop. In general, you want a conditional situation, so that the user can exit. In C, you'd do:

extern quit;

void init()
{
    quit = 0;
}

//inside main()
while(!quit)
{
    //game functions
    //advance_frame();
}

You would break that loop if the player tries to exit, dies, or wins the game, with each having a unique value, such as quit == 1 for normal exit, == 2 for death, and == 3 for winnig; then a different loop (inside main) for each of those.

Infinite loops are in general, rare, and should be used with extreme care. Usually you would want a statement that causes them to exit, in any case.

Even in C, you need to advance the frame manually. Only game-based scripting languages tend to 'automate' this somehow, and Macromedia Flash was never designed with the intent of powering a game engine.

Is game design in Flash truly being taught at University now?! If so, that's horrible.



来源:https://stackoverflow.com/questions/48707587/infinite-loops-action-script-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!