Why i like to use Casalib

I just started playing Actionscript 3.o. And I got some problem with Evnts. My code looks like something like this.

My movieClip Class is like this.
package{
import flash.display.MovieClip;
 import flash.events.Event;
 public class MC extends MovieClip {

  var val=1;
  var _o;
  public function MC() {
   _o=this;
   _o.addEventListener(Event.ENTER_FRAME,onLoop);
   }
  private function onLoop(e:Event) {
   trace(val);
   val++;
  }
 }
}
It's the code in the main.fla
b1_btn.addEventListener(MouseEvent.MOUSE_DOWN,clicked);
var m:MC=new MC();
addChild(m);

function clicked(e:MouseEvent) {
 removeChild(m);
 m=null;
}

The problem i get is with enterframe. After removing the Movieclip from the stage, enterframe don't stop it's working.
So i posted this in forums. It's the problem of garbage collection. Till now am playing with Actionscript 2.0 i never think about the garbage collection. The solution is to remove listeners manually or just use REMOVED_FROM_STAGE or REMOVED event.

So i have to change the MovieClip class to something like this.
package {
import flash.display.MovieClip; 
import flash.events.Event; 
public class Mextends MovieClip {
    var val=1;
    var _o;
   public function MC() {
      _o=this;
      _o.addEventListener(Event.ENTER_FRAME,onLoop,false,0,true);
      _o.addEventListener(Event.REMOVED,destroy);
    }
    private function destroy(e:Event) {
     _o.removeEventListener(Event.ENTER_FRAME,onLoop);
      _o.removeEventListener(Event.REMOVED,destroy);
    }
    private function onLoop(e:Event) {
    val++;
      trace(val);
    }
  }
}
This approach doesn't seem to me so good. So i searched in google and i get something more interesting .

It's casalib, an open source Actionscript Library for streamlining the development.

The most important thing interested me is the garbage collection using destroy() method. It will remove all events in the Movieclip.Then i just changed my code to some thing like this.

It's my MovieClip class
package {
  import org.casalib.display.CasaMovieClip;
 import flash.events.Event;
  public class MC extends CasaMovieClip {
    var val=1;
    var _o;
     public function MC() {
   _o=this;
   _o.addEventListener(Event.ENTER_FRAME,onLoop,false,0,true);
  }
  private function onLoop(e:Event) {
   trace(val);
   val++;
  
  }
 
 }
}
this is my code in Main.fla
b1_btn.addEventListener(MouseEvent.MOUSE_DOWN,clicked);
var m:MC=new MC();
addChild(m);
function clicked(e:MouseEvent) {
 m.destroy();
 m=null;
 trace(m);
}

And it works perfectly . Now am extensively using this library in my development. it's easy to remove nested movieclips and it's events.

Comments

Popular posts from this blog

Configure PostgreSQL and phpPgAdmin in WAMP

Angular - 4 year road map

Flash FLV player using PHP