Pages

Tuesday, January 3, 2012

AS3: Swf Loader

A new class for loading swf files with events.

SOURCE CODE
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package
{
 import flash.events.Event;
 import flash.events.HTTPStatusEvent;
 import flash.events.IEventDispatcher;
 import flash.events.IOErrorEvent;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
 
 /**
  * @file CSwfLoader.as
  * @author Abhishek Kumar
  */
 public class CSwfLoader
 {
  public var prepare:Function;
  public var progress:Function;
  public var proceed:Function;
  
  public var instance:*;
  
  public function CSwfLoader()
  {
   trace("CSwfLoader -> constructor");
  }
  
  public function retrieve(src:String):void
  {
   instance.unloadAndStop(true);
   instance.load(src);
   attachListeners(instance);
  }
  
  private function attachListeners(dispatcher:IEventDispatcher):void
  {
   trace("CSwfLoader -> attachListeners");
   dispatcher.addEventListener(Event.COMPLETE, completeHandler);
   dispatcher.addEventListener(Event.OPEN, openHandler);
   dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
   dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  }
  
  private function detachListeners(dispatcher:IEventDispatcher):void
  {
   trace("CSwfLoader -> detachListeners");
   dispatcher.removeEventListener(Event.COMPLETE, completeHandler);
   dispatcher.removeEventListener(Event.OPEN, openHandler);
   dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
   dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  }
  
  private function completeHandler(event:Event):void
  {
   trace("CSwfLoader -> completeHandler:", event);
   detachListeners(instance);
   if(proceed != null) 
    proceed();
  }
  
  private function openHandler(event:Event):void
  {
   trace("CSwfLoader -> openHandler:", event);
   if(prepare != null) 
    prepare();
  }
  
  private function progressHandler(event:ProgressEvent):void
  {
   trace("CSwfLoader -> progressHandler loaded:", event.bytesLoaded, "total:", event.bytesTotal);
   if(progress != null)
    progress(event.bytesLoaded, event.bytesTotal);
  }
  
  private function securityErrorHandler(event:SecurityErrorEvent):void
  {
   trace("CSwfLoader -> securityErrorHandler:", event);
  }
  
  private function httpStatusHandler(event:HTTPStatusEvent):void
  {
   trace("CSwfLoader -> httpStatusHandler:", event);
  }
  
  private function ioErrorHandler(event:IOErrorEvent):void
  {
   trace("CSwfLoader -> ioErrorHandler:", event);
  }
 }
}

No comments:

Post a Comment