3 Replies Latest reply on May 23, 2006 12:25 AM by Slafon

    Listener seems to still be alive. Slowing Down Flash

    Slafon
      I have two classes: MyDataLoader and FastOrSlow.
      At construction, MyDataLoader creates a URLLoader and loads the content of a text file. It also creates
      a listener to monitor the completion of the download.
      The other class (the main one), FastOrSlow, instantiates 20 objects of type MyDataLoader and then does some CPU-intensive computation.
      Now here's the problem: if I run the code for FastOrSlow on my local machine, then I get an FPS rate of 32 frames per second.
      If I run it from my website, I get an FPS of 12 frames per seconds, even though all downloads are complete.
      My guess is that it has to do with listeners still being active, or not garbage collected (I use weak references though...).

      Remote demo can be found here
      If you need the source, see below or go to here

      Any help would be greatly appreciated.

      -Stefan

      Here's the code for class MyDataLoader:
      package {
      import flash.events.*;
      import flash.net.URLLoader;
      import flash.net.URLRequest;

      public class MyDataLoader {
      private var my_loader: URLLoader;
      private var fast_or_slow: FastOrSlow;

      public function MyDataLoader(fast_or_slow: FastOrSlow) {
      this.fast_or_slow = fast_or_slow;
      my_loader = new URLLoader();
      var request:URLRequest = new URLRequest('data.txt');
      request.method = URLRequestMethod.POST;
      my_loader.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
      my_loader.load(request);
      }

      private function onComplete(evt: Event): void {
      // Data was downloaded. Increase counter.
      fast_or_slow.cnt++;
      // Remove this listener.
      my_loader.removeEventListener(Event.COMPLETE, onComplete);
      }
      }
      }


      And for class FastOrSlow:
      package {
      import flash.display.*;
      import flash.events.*;
      import flash.text.TextFormat;
      import flash.util.getTimer;
      import MyDataLoader;

      [SWF( backgroundColor='0', frameRate='100', width='300', height='100')]
      public class FastOrSlow extends Sprite {

      private var fps_text: TextField;
      private var fr: int;
      private var ms: int;
      public var cnt: Number; // Number of completed events.

      public function FastOrSlow() {
      // Create FPS and event counters.
      var tf: TextFormat = new TextFormat();
      tf.size = 10;
      tf.bold = true;
      tf.font = 'Verdana';
      tf.color = 0xFFFFFF;
      fps_text = new TextField();
      fps_text.autoSize = 'left';
      fps_text.defaultTextFormat = tf;
      addChild(fps_text);
      ms = getTimer();
      fr = 0;
      cnt = 0;
      // Create URLLoader and its associated Listener.
      for (var i: Number = 0; i< 20; i++) {
      var my_data_loader: MyDataLoader = new MyDataLoader(this);
      }
      // Set up the loop.
      stage.addEventListener( Event.ENTER_FRAME, onEnterFrame );
      }

      private function onEnterFrame( event: Event ): void {
      // Do something CPU-intensive to decrease fps.
      var r: Number = 0;
      for (var i: Number = 0; i < 2e6; i++) {
      r = r + i * r;
      }
      // Fps computation.
      fr++;
      if (getTimer() > ms + 1000) {
      fps_text.text = 'FPS:'+ fr.toString() + ' Completed:' + cnt.toString();
      fr = 0;
      ms = getTimer();
      }
      }
      }
      }