Load AS2 SWF into your AS3 Project

There is a Flash game that I have been working on that I’ve had to load into an as3 interface and I noticed that my previous class will not work to do this.

There were a few modifications that I had to make… Here is the Actionscript 3 class:

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
// SWFLoader Class by Rick Nuthman
// 2.16.10
//
// Usage:
/*
var game:SWFLoaderAs2 = new SWFLoaderAs2("yourSwf.swf",20,0)
game.dispatcher.addEventListener("ready", gameLoaded);
function gameLoaded(event:Event){
   
    addChild(game);
}
*/


package com.frigidfish{

    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.events.EventDispatcher;       

    public class SWFLoaderAs2 extends Sprite {     
       
        public var dispatcher:EventDispatcher   = new EventDispatcher();
        private var contentLoader:Loader;      
       
       
        public function SWFLoaderAs2(pathToTheContent,x,y) {
           
            this.x = x;
            this.y = y;
            loadSWF(pathToTheContent);
        }
           
        private function loadSWF(pathToTheContent):void {
           
            var request:URLRequest  = new URLRequest(pathToTheContent);
            contentLoader           = new Loader();
            contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);
            contentLoader.load(request);
        }

        private function swfLoaded(e:Event):void {
           
            addChild(contentLoader);
            dispatcher.dispatchEvent(new Event("ready"));                  
        }
    }
}

usage:

1
2
3
4
5
6
var game:SWFLoaderAs2 = new SWFLoaderAs2("yourSwf.swf",20,0)
game.dispatcher.addEventListener("ready", gameLoaded);
function gameLoaded(event:Event){
   
    addChild(game);
}

This will certainly work to load an as2 swf into your as3 project,
but keep in mind that you will not be able to access methods and properties of your as2 swf.
I will be updating this class or adding another to make this possible. In the meantime,
Grant Skinner has created this to make the job a snap!

Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

Leave a Reply