13 Replies Latest reply on Jul 21, 2008 4:23 PM by kglad

    Long loops

    tatiana1 Level 1
      Hi, I'm trying to export a MC to JPG format using PHP. The procedure calls for a nested loop which reads each pixel and catenates its color value in an array in order to be sent to PHP.

      After the loop runs for a few seconds, I get the "Flash running slow" message .How can I avoid this and make sure Flash doesn't think I've entered a neverending loop?

      Furthermore, I have a ProgressBar component which measures the amount of data analyzed. Unfortunately, the ProgressBar doesn't appear until AFTER the loop has finished running.

      I'm used to programming in VB6 where a functions called DoEvents placed inside a loop allows the language engine to execute all background procedures and not think I've entered an endless loop. This also allows for screen and progress refreshes. Is there something similar in Flash?

      Here is my code:
        • 1. Re: Long loops
          kglad Adobe Community Professional & MVP
          in your publish settings you can adjust the default timeout duration of 15 seconds: file/publish setting/flash/script timout limit

          but you should also consider compressing those data. the compression method below is a refinement of a method of, i believe, david mennenoh ( http://dmennenoh.blogspot.com/2008/02/simple-array-compressor.html ). in many situations it cuts processing time by 25 to 75%:

          • 2. Re: Long loops
            Rothrock Level 5
            Yeah that doesn't work in Flash. If you could go to AS3 you could use the byteArray class which would be much faster.

            Also I think there are already some AS2 classes out there that allow you to do this so you might want to search for those instead of reinventing the wheel so to speak.

            If you do need to do this yourself, I would look at breaking this up into chunks. Have a setInterval that calls your function and maybe do 5 rows of your image at a time.

            Then whent he function is over you update your progress bar and the timer restarts on the "flash running slow" message.

            Also I'm not sure that pushing a bunch of strings onto an array is the best way to do this. There may be better ways, but I would experiment a bit to be sure.

            Final note, sometimes running a loop backward is faster so you might want to make your loops like:

            for(var a=w-1;a>=0;a--)

            It might help. Good luck!
            • 3. Re: Long loops
              Level 7
              That's my array compressor actually. :)

              Dave -
              Head Developer
              Adobe Community Expert

              • 4. Re: Long loops
                kglad Adobe Community Professional & MVP
                oh man. i'm sorry.

                i knew i might goof that up. i tried to google that to get credit where it was due but didn't find the reference i wanted and i didn't have more than a few minutes to search.

                i edited my message to correct that.
                • 5. Re: Long loops
                  tatiana1 Level 1
                  Thanks for your replies.

                  I'm wondering if there's a better approach to saving MC as a JPG. A thought came up: if I'm using AS2, can I call an AS3 procedure that does the job, so that I can use the ByteArray class?

                  If they do exist, do you have any idea what other methods I could use to do what I need to do, using only AS2 classes? I've searched around, but came up with nothing. It surprises me that Flash allows you to export an image from the stage when in the development environment, but it doesn't allow you to do the same thing dynamically from AS2.
                  • 6. Re: Long loops
                    kglad Adobe Community Professional & MVP
                    you can't use any as3 classes with as2 code.

                    you can check mario klingemann's bitmapexporter class which has image scan methods that speed processing.
                    • 7. Re: Long loops
                      tatiana1 Level 1
                      Hi, I tried the BitmapExporter and a whole slew of other dandy methods and they all work well on small pictures. Unfortunately, I'm dealing with pictures which can be as large as 1280x1280 pixels! After only a few seconds of processing, Flash tells me the script is running too slow. I read somewhere that the routine could be spread out over several frames in order to fool Flash into thinking that everything is running normally, but I haven't the faintest idea as to how to do it. Can anyone help me??? I'm stuck!!!
                      • 8. Re: Long loops
                        kglad Adobe Community Professional & MVP
                        did you change your publish settings?

                        and you can break your double for-loop into one for-loop that's called (up to) 1280 times using and onEnterFrame or setInterval() loop.
                        • 9. Re: Long loops
                          tatiana1 Level 1
                          What do you mean when you say "did you change your publish settings"? How should I change them?

                          Can you give me an idea of how to change the double 'for' loop into a setInterval - type of loop? If I use setInterval, how do I keep the user from clicking other things while the loop is going?
                          • 10. Re: Long loops
                            kglad Adobe Community Professional & MVP
                            in your publish settings you can adjust the default timeout duration of 15 seconds: file/publish setting/flash/script timout limit
                            • 11. Re: Long loops
                              tatiana1 Level 1
                              True, but I'd like to eliminate the problem at its root. If I increase the timeout duration, it may work fine for my PC, but it might not work for someone else's. I'd like to simply eradicate the problem by not making Flash think the loop is infinite.

                              Furthermore, I placed a ProgressBar within the loop to indicate the export progress, but it doesn't even show up on the screen. As a matter of fact, during the loop, the screen never refreshes. I even put an Invalidate command within the loop, but no go. Do you have any ideas? Thank you.
                              • 12. Re: Long loops
                                Rothrock Level 5
                                You could use the BitMapData class and cut the graphic up into chunks. Then it would be running on smaller graphics. You do one chunk then update your progress bar and start it again for the next chunk. I would imagine if you divided it up into 8 or so pieces that should make it work?

                                As for keeping the user from clicking on things while it runs, the simplest solution is to not give them anything to click on while it is running.

                                During a loop you won't get screen updates. That is the issue at hand here. When you have some code, all the code on that frame needs to execute before the screen is updated. If the code takes too long to execute the screen won't execute and you get the error message that a script is making Flash run slowly.

                                • 13. Re: Long loops
                                  kglad Adobe Community Professional & MVP
                                  you're not going to get the stage to update (and display a progress bar or anything else) unless you break the processing into loops like i explained above.