dcsimg
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: force update during processor intensive routine

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    11

    force update during processor intensive routine

    hello all,
    i have a swf with several processor intensive subroutines within a main init() routine at start up (about a 5 secs startup wait). it needs a preloader not due to asset loads but to the processor intensive routines.

    i'm trying to create a preloader that will display progress as the routines complete but when i try to write text to text field as each subroutine completes either the textField.text is not updated or the text field display is not updated after each subroutine finishes. this prevents me from tracking the progress of the start up.

    is there a way to force an update to the display list in the middle of a processor intensive routine? i believe flash is supposed to update after each frame but i'm not seeing this.

    also, the text field i'm displaying *before* this processor intensive routine *does* display when running locally but *does not* display before the routine when running online. not sure why.

    thx in advance,
    michael

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    You could dispatch events throughout your routine or create a variable inside your routine that holds the percentage done and have an ENTER_FRAME event monitor that variable.

    If you post some code (or example code) we can provide a little better direction.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    thx sstalder,
    i wound up solving the problem by putting setTimer() calls between the subroutines. that forced an update to allow me to track progress.

    m

  4. #4
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    I would recommend using events, that's what they are for. Using a timer kind of defeats the purpose.

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    yup. i agree triggering an event is a better/cleaner way to go but will it force a display update mid frame? will code be able to display something upon receiving an event within a frame's routine? my sense is no.

  6. #6
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    The event will force your update whenever it's dispatched in your code.

  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    thx beathoven, if so, then that's the way to go.

  8. #8
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    actually, after testing, a dispatched event does *not* force an update. so it seems like the setTimer() strategy is all i have.

  9. #9
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Can you post the code that you are using? I think you may just be missing something in the routine of things.

  10. #10
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Yeah, post code.

  11. #11
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    sstalder and beathoven, here you go. use loadType to toggle between event and timer techniques to force a screen update.

    PHP Code:
    package evt_test {
        
    import flash.display.*;
        
    import flash.text.*;
        
    import fl.transitions.*;
        
    import fl.transitions.easing.*;
        
    import flash.events.*;
        
    import flash.filters.BevelFilter;
        
    import flash.utils.*;

        public class 
    Evt_test extends Sprite {
            private var 
    txt:TextField;
            private var 
    ballAry1:Array = new Array();
            private var 
    ballAry2:Array = new Array();
            private var 
    intervalId:uint 0;
            private var 
    loadType:String "event"// change this to "timer" to see the display happen mid routine

            
    public function Evt_test() {
                
    this.addEventListener("continueLoad"continueLoad_evt);  

                
    creatTxtFld();

                
    // the only reason for this is to force the player to display the stage before the processor intensive routines start
                
    intervalId setTimeout(init1);

            }

            private function 
    init():void {
                
    proccesorIntensiveRoutine(0300xff0000ballAry1);

                
    txt.text "message to display in the middle of the processor intensive routines";

                
    // this event trigger does not force the above line to display between processor intensive subroutines
                
    if (loadType == "event") {
                    
    dispatchEvent(new Event("continueLoad"));  
                }
                
                
    // this setTimeout call does force the above line to display between processor intensive subroutines
                
    if (loadType == "timer") {
                    
    intervalId setTimeout(continueLoad_timeout10);
                }
            }

            private function 
    continueLoad_evt(evt:Event):void {
                
    proccesorIntensiveRoutine(480300x0000ffballAry2);
                
                
    txt.text "load complete";
            }

            private function 
    continueLoad_timeout():void {
                
    proccesorIntensiveRoutine(480300x0000ffballAry2);
                
                
    txt.text "load complete";
            }


            private function 
    proccesorIntensiveRoutine(x:Numbery:Numberclr:uintballAry:Array):void {
                for (var 
    i:Number=0i<3000i++) {
                    
    createBall(xyclrballAry);
                }

            }

            private function 
    creatTxtFld():void {
                var 
    tf:TextFormat = new TextFormat();
                   
    tf.size 18;
                
    txt = new TextField();
                
    txt.defaultTextFormat tf;
                
    txt.autoSize TextFieldAutoSize.LEFT;
                
    txt.20;
                
    txt.5;
                
    txt.text "load started";
                
    addChild(txt);
            }

            var 
    newX:Number;
            var 
    newY:Number;
            private function 
    createBall(x:Numbery:Numberclr:uintballAry:Array):void {
                var 
    ball:Sprite = new Sprite();
                
    ballAry.push(ball);
                
    ball.graphics.lineStyle(1clr1);
                
    ball.graphics.beginFill(clr1);
                
    ball.graphics.drawCircle(888);
                var 
    el:Number ballAry.length-2;
                if (
    ballAry.length==1) {  
                    
    el=0;
                    
    newX=x;
                    
    newY=y;
                } else {
                    
    newX = (ballAry[el].x+ballAry[el].width);
                }

                if ((
    el+1)%28 == 0) {
                    
    newX=x;
                    
    newY+=ballAry[el].height;
                }
                
    ball.newX;
                
    ball.newY;
                
    addChild(ball);
            }


        }

    Last edited by sstalder; 05-01-2009 at 01:18 PM. Reason: Please use the code wrappers when posting code.

  12. #12
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Basicaly your processor intensive routine, you're trying to use it to go through 9000 iterations if I understand right. And the way you have it setup right now you are calling this function three times to display messages in between.

    I would simply call processorIntensiveRoutine once.

    In your for statement change it to

    Code:
    for (var i:int=0; i<3000; i++) {
                    createBall(x, y, clr, ballAry);
                                    
                                    if (i == 3000) {txt.text = "Message to display at this point"}
                                    if (i == 6000) {txt.text = "Message to display at this point"}
                                    if (i == 9000) {txt.text = "Load Complete"; functionsToExecuteWhenDoneWithProcess();}
    
                }
    Btw, I changed the variable type of i to int because it is the fastest way to go through iterations, it could actually speed up the process by more than 50%. Unless you needed to pass i as a Number somewhere in your iteration, it is much faster to use int. You can read up more on AS3 optimization here: http://www.rozengain.com/blog/2007/0...optimizations/
    Last edited by Beathoven; 05-01-2009 at 02:03 PM.

  13. #13
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    So your cleaned up file would look like this:

    Code:
    package evt_test {
        import flash.display.*;
        import flash.text.*;
        import fl.transitions.*;
        import fl.transitions.easing.*;
        import flash.events.*;
        import flash.filters.BevelFilter;
        import flash.utils.*;
    
        public class Evt_test extends Sprite {
            private var txt:TextField;
            private var ballAry1:Array = new Array();
            private var ballAry2:Array = new Array();
    
            public function Evt_test() {
    
                creatTxtFld();
    
            }
    
            private function init():void {
                proccesorIntensiveRoutine(0, 30, 0xff0000, ballAry1);
    
            }
    
            private function proccesorIntensiveRoutine(x:Number, y:Number, clr:uint, ballAry:Array):void {
                for (var i:int=0; i<9000; i++) {
                    createBall(x, y, clr, ballAry);
    
                    if (i == 3000){txt.text = "Some message";}
                    if (i == 6000){txt.text = "Some message";}
                    if (i == 9000){txt.text = "Complted"; executeFinalFunctions}
                }
    
            }
    
            private function creatTxtFld():void {
                var tf:TextFormat = new TextFormat();
                   tf.size = 18;
                txt = new TextField();
                txt.defaultTextFormat = tf;
                txt.autoSize = TextFieldAutoSize.LEFT;
                txt.x = 20;
                txt.y = 5;
                txt.text = "load started";
                addChild(txt);
            }
    
            var newX:Number;
            var newY:Number;
            private function createBall(x:Number, y:Number, clr:uint, ballAry:Array):void {
                var ball:Sprite = new Sprite();
                ballAry.push(ball);
                ball.graphics.lineStyle(1, clr, 1);
                ball.graphics.beginFill(clr, 1);
                ball.graphics.drawCircle(8, 8, 8);
                var el:Number = ballAry.length-2;
                if (ballAry.length==1) {  
                    el=0;
                    newX=x;
                    newY=y;
                } else {
                    newX = (ballAry[el].x+ballAry[el].width);
                }
    
                if ((el+1)%28 == 0) {
                    newX=x;
                    newY+=ballAry[el].height;
                }
                ball.x = newX;
                ball.y = newY;
                addChild(ball);
            }
    
    
        }
    }
    Lemme know if that makes sense.

  14. #14
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    beathoven,
    thx for the example. the code i posted is just a test i threw together that represents the issue rather than the code itself which is much more complicated and unwieldy. the processor intensive code in my actual app is thousands of lines and it's too complicated to lace in message lines. i have to force a display in the main init() after each complicated subroutine at this point. but you can see that an event trigger didn't force a display.

    thx for the int tip. i have lots of for loops using Number that i'll switch to int. also, thx for the optimization page. i'll look that over.

    m

  15. #15
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Just a tip for a more optimized for loop:
    PHP Code:
    var i:int 0end:int 9000;
    for (; 
    i<endi++) { 
    I would suggest using this format, especially for large loops.

  16. #16
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by mammy View Post
    beathoven,
    thx for the example. the code i posted is just a test i threw together that represents the issue rather than the code itself which is much more complicated and unwieldy. the processor intensive code in my actual app is thousands of lines and it's too complicated to lace in message lines. i have to force a display in the main init() after each complicated subroutine at this point. but you can see that an event trigger didn't force a display.

    thx for the int tip. i have lots of for loops using Number that i'll switch to int. also, thx for the optimization page. i'll look that over.

    m
    The reason your code is not reacting to anything is because you are never dispatching the event itself in your code. You would add a line like this where you define your variables:

    Code:
    var dispatcher:EventDispatcher = new EventDispatcher();
    and then add this at the end of one of your subroutines:

    Code:
    dispatcher.dispatchEvent(new Event("continueLoad");
    Or am I missing the part where you are dispatching the event?

  17. #17
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    yup, this line listens for the event:
    this.addEventListener("continueLoad", continueLoad_evt);

    and this line dispatches the event:
    dispatchEvent(new Event("continueLoad"));

    which calls:
    continueLoad_evt()

  18. #18
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Joining this thread late and haven't read it properly, but I've also had the problem of the display not updating - even after an event trigger - if there is something intensive going on in the code (like a massive number of iterations, or a recursive loop). The solution I use is to dispatch timer events and force an update to the screen using updateAfterEvent() in the timer's event handler.

  19. #19
    Junior Member
    Join Date
    Apr 2009
    Posts
    11
    Ric, updateAfterEvent() doesn't come into play here since the timer itself is the solution to force display. no need to add updateAfterEvent() to the timer event trigger.

    and since updateAfterEvent() can only be used with mouse and timer events i can't place it in between subroutines to force display.

    wish flash had an updateAfterEvent() call that doesn't require and mouse/timer event like Director does. that'd be the cleanest solution but it doesn't exist in flash.
    Last edited by mammy; 05-02-2009 at 08:19 AM.

  20. #20
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by mammy View Post
    yup, this line listens for the event:
    this.addEventListener("continueLoad", continueLoad_evt);

    and this line dispatches the event:
    dispatchEvent(new Event("continueLoad"));

    which calls:
    continueLoad_evt()
    You are dispatching your event within your init function. Your init function will call your processor intensive routine but will keep proceeding with the code right away. You need to dispatch your event from within your iterations and see if that works.

    So something like this in the processorIntensive routine:

    Code:
    private function proccesorIntensiveRoutine(x:Number, y:Number, clr:uint, ballAry:Array):void {
                for (var i:int=0; i<9000; i++) {
                    createBall(x, y, clr, ballAry);
    
                    if (i == 3000){txt.text = "Some message"; dispatchEvent(new Event("continueLoad"));}
                    if (i == 6000){txt.text = "Some message"; dispatchEvent(new Event("continueLoad"));}
                    if (i == 9000){txt.text = "Complted"; executeFinalFunctions}
    
                    }
    }

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