# Simple Custom Event Question

**URL:** <https://forum.kirupa.com/t/simple-custom-event-question/297165>\
**Category:** flash\
**Created:** [September 26, 2009, 5:04pm UTC](https://forum.kirupa.com/t/simple-custom-event-question/297165 "2009-09-26T17:04:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonsong82](https://avatars.discourse-cdn.com/v4/letter/w/22d042/32.png) [@wonsong82](https://forum.kirupa.com/u/wonsong82)\
**Post date:** [September 26, 2009, 5:04pm UTC](https://forum.kirupa.com/t/simple-custom-event-question/297165/1 "2009-09-26T17:04:57Z")

</div>

this is a custom class that loads the fonts.

[AS]  
[//FontLoader.as](https://FontLoader.as)  
package won.text  
{  
import flash.events._;  
import [flash.net](http://flash.net)._;  
import flash.display.\*;

```
public class FontLoader
{
    private var i:int;
    private var l:Array;
    private var c:Function;            
    private var p:Number = 0; 
    
    public function FontLoader($list:Array, $param:Object)
    {
        i=-1;
        l=$list;
        c = ($param.onComplete) ? $param.onComplete : function(){};                            
    }
    
    public function load():void
    {
        loadFont();
    }
    
    public function get percent():Number
    {
        return p;
    }
                    
    private function loadFont(e:Event=null):void
    {
        i++;
        (i&gt;l.length-1) ? exit() : process();
    }
    
    private function process():void
    {            
        var loader:Loader = new Loader();            
        loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, calculateProgress);
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFont);
        loader.load(new URLRequest(l*));                    
    }
    
    private function calculateProgress(e:ProgressEvent):void
    {            
        p = (i/l.length) + ((e.bytesLoaded/e.bytesTotal)/l.length);
    }
    
    private function exit():void
    {
        c();
    }    
}

```

}  
[/AS]

How do i add “addEventListener” to this custom class?  
i want to add Two addEventListener,

1. when the 'calculateProgress is function is modifying ‘p’ property, i want the flash document to get that number for each update.
2. when the loading is complete, i want to trigger the oncomplete function. i did this by passing the function to the object parameter, but i would like to use addEventListener.  
here the following code is the way i achieved this.

[AS]  
//Flash Document  
import won.text.\*;

var list:Array = [“pastelar.swf”,“frankGothicBook.swf”];  
var loader:FontLoader = new FontLoader(list, {onComplete:done});  
loader.load();

addEventListener(Event.ENTER\_FRAME, loop);  
function loop(e:Event):void  
{  
trace(loader.percent);  
}

function done():void{  
removeEventListener(Event.ENTER\_FRAME, loop);  
}

[/AS]

but instead doing that, i wanna be able to this.  
[AS]  
import won.text.\*;

var list:Array = [“pastelar.swf”,“frankGothicBook.swf”];  
var loader:FontLoader = new FontLoader();  
loader.addEventListener(FontLoaderEvent.PROGRESS, displayPercentLoaded);  
loader.addEventListener(FontLoaderEvent.COMPLETE, init);  
loader.load(list);

function (e:FontLoaderEvent):void  
{  
trace(e.target.percent);  
}

function init(e:FontLoaderEvent):void  
{  
trace(“fontloaded”);  
}  
[/AS]

i tried to search online, read books, etc for custom events, but  
it was hard to grasp the concept.  
can someone please help?
