Here is a class that you can use to load swf’s into an object that you can display or do whatever you’d like with:
// SWFLoader Class by Rick Nuthman
// 2.10.10
//
// Usage:
/*
var mySWF:Object;
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
addChild(mySWF.swf);
}
*/
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 SWFLoader{
public function SWFLoader() {
}
public static function create(url, x=0,y=0):Object {
var loaded:Event = new Event("ready");
var dispatcher:EventDispatcher = new EventDispatcher();
var swf:Sprite = new Sprite ;
var mLoader:Loader = new Loader ;
var obj:Object = new Object;
obj.swf = swf;
obj.dispatcher = dispatcher;
swf.x = x;
swf.y = y;
var mRequest:URLRequest = new URLRequest(url);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);
mLoader.load(mRequest);
function swfLoaded(event:Event) {
var target = event.currentTarget.content;
swf.addChild(target);
dispatcher.dispatchEvent(loaded);
}
return obj;
}
}
}
// 2.10.10
//
// Usage:
/*
var mySWF:Object;
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
addChild(mySWF.swf);
}
*/
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 SWFLoader{
public function SWFLoader() {
}
public static function create(url, x=0,y=0):Object {
var loaded:Event = new Event("ready");
var dispatcher:EventDispatcher = new EventDispatcher();
var swf:Sprite = new Sprite ;
var mLoader:Loader = new Loader ;
var obj:Object = new Object;
obj.swf = swf;
obj.dispatcher = dispatcher;
swf.x = x;
swf.y = y;
var mRequest:URLRequest = new URLRequest(url);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);
mLoader.load(mRequest);
function swfLoaded(event:Event) {
var target = event.currentTarget.content;
swf.addChild(target);
dispatcher.dispatchEvent(loaded);
}
return obj;
}
}
}
You don’t need to make an instance of the class, just use the static function ‘create’ like so:
var mySWF:Object;
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
// You can replace 0,0 in the previous line with
// initial x,y coordinates for your swf
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
addChild(mySWF.swf);
}
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
// You can replace 0,0 in the previous line with
// initial x,y coordinates for your swf
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
addChild(mySWF.swf);
}
I have been using this class to load a bunch of swfs into an array of objects.. I built a preloader that says something like “loading foo.swf…” for each swf as they are loading. Then when the “ready” event has fired for all of the swfs I add them all to a container sprite and display them.
This will help me when I get around to making my website template in Flash!
Thanks!
Nice class, might use it someday 🙂