7 Replies Latest reply on Mar 6, 2007 2:40 PM by rj_resnick

    Progress Bar to show computation progression

      Hello friends,

      I'm developing a large-scale simulation that has some heavy computational requirements at specific times (e.g., when the user clicks the "end turn" button). I would like to set up a progress bar that shows the results of a series of computations being performed by external AS classes. The end turn button has the very simple code:

      on (release) {

      This has the nice effect of "Advancing" the simulation and then updating the control panel, as you can infer from the code, but it takes about 60 seconds before control is given back to the user.

      If I place a progress bar on the slide (it's a slide-based application) that has the "End Turn" button and set it to manual mode, subsequent calls to my_pb.setProgress() are not rendered on screen until the above code is totally complete.

      I have tried changing the above code to use setInterval as follows:

      on (release) {
      var timer:Number = setInterval(updatePB,100); // I'm running at 24 fps

      And the function updatePB is:

      function updatePB():Void {
      my_pb.label = _level0.presentation.World.pbLabel;

      Then inside of the computationally intensive code, I occasionally change the values of the two variables pbPctComplete and pbLabel. However, this effects no change - the progress bar never updates until the Advance() function is complete.

      All code is either inside of onclipevent handlers, as above, or in the first frame of an "Actionscript" layer in the corresponding slide as is the case with the function updatePB (in this case, _level0.presentation.World). The computationally intensive code is run from external actionscript classes.

      Any pointers would be greatly appreciated.

      Many thanks,
        • 1. Re: Progress Bar to show computation progression
          Flash renders it's frames in 'Race Track' fashion, that is, for every frame, it executes ALL of your AS, then updates its vectors, THEN draws the new frame. So, if you take 60 seconds to execute some code, flash will wait until you're done to render any new frames. Your interval function will be called, but you'll never see it, because flash is still waiting on your other AS before it will render any new frames.

          If you want to update the user about the progress of this large calculation, you're going to have to use a setInterval or onEnterFrame to break up the calculation itself. For example, you could use the generic setup below. You'd undoubtedly have to make some adjustments to break up the calculations, but that's really your only option other than popping up a modal graphic to tell the user to hang tight.
          • 2. Re: Progress Bar to show computation progression
            rj_resnick Level 1

            The simplicity of your solution brings tears to my eyes. This is something I'd been grappling with in many circumstances for many a long time.

            I didn't want to have to add new frames to handle this and your code effectively "simulates" new frames by breaking up calculations into what could be "frameable" computations, but instead handling it at the actionscript level.

            Between you and me, I like your solution a whole lot better.

            Thank you, thank you, thank you. I owe you one. If you're ever in Boston, I'll buy you a beer.
            • 3. Re: Progress Bar to show computation progression
              tonyhigham Level 1
              Thanks Richard. As a bonus, here's a more in depth explanation of the whole race track thing I was babbling about:

              • 4. Re: Progress Bar to show computation progression
                rj_resnick Level 1
                Hi again Tony, others,

                So, this strategy works perfectly in a typical Flash file.

                As I attempt to integrate it into an external class within a Slide-based Flash application, I run into trouble. I have a class called oil.Simulator.Simulation which extends MovieClip. The function that "Advances" the simulation is called "Advance". It simply sets up a function pointer for onEnterFrame to run. Interestingly, the function runs once, and once only. Here is the code - I've given you the whole class but focus in on the Advance() and _Advance() functions.

                Many thanks for any additional pointers you might have.


                • 5. Re: Progress Bar to show computation progression
                  rj_resnick Level 1
                  Interestingly, if I add the line:

                  onEnterFrame = this._Advance();

                  at the end of each case in the _Advance() function, I can make through it. Who or what is assigning onEnterFrame to null while I'm not looking?

                  • 6. Re: Progress Bar to show computation progression
                    tonyhigham Level 1
                    I am definitely not the world's foremost authority on classes in AS, but if I had to take a guess, it would be that when you set the onEnterFrame in Advance(), it gets treated as a local object and deleted after the function executes. To solve this problem, I THINK you could just set this.onEnterFrame instead. And if that works, you'll have to change your last line of _Advance as well. Hope that helps... ;)
                    • 7. Re: Progress Bar to show computation progression
                      rj_resnick Level 1
                      Thanks again Tony. Your suggestion didn't work, but interestingly, the same concept using setInterval() seems to work fine.

                      Best wishes,