Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Created February 12, 2012 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeprobert/1806782 to your computer and use it in GitHub Desktop.
Save leeprobert/1806782 to your computer and use it in GitHub Desktop.
Flash application class for loading Flex SWF files and calling methods off them
package {
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.SWFVersion;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import mx.managers.SystemManager;
public class Main extends MovieClip {
protected var swfLoader:Loader = new Loader();
public function Main():void
{
this.addEventListener(Event.ADDED_TO_STAGE, init);
}
//----------------------------------------------------------------
private function init(evt:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
var swfURL:String = "Executive_Channel_768_1360.swf";
//var swfURL:String = "normalFlash.swf";
trace("Loading swf : "+swfURL);
swfLoader.contentLoaderInfo.addEventListener(Event.INIT,swfLoaded);
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.applicationDomain = ApplicationDomain.currentDomain;
swfLoader.load(new URLRequest(swfURL),loaderContext);
}
//----------------------------------------------------------------
private function swfLoaded(event:Event):void
{
var content:DisplayObject = swfLoader.content;
this.addChild(content);
var sysmgr:SystemManager = (content as SystemManager);
if(sysmgr)
{
trace("This is a Flex application!");
/*
If this is a Flex Application SWF then we
must wait for the application to instantiate
otherwise the application is null.
This process will only ever complete for a Flex App.
*/
sysmgr.addEventListener("applicationComplete", swfAppComplete);
}
else
{
trace("This is a normal Flash SWF!");
/*
do normal flash tasks here ...
*/
}
}
//----------------------------------------------------------------
private function swfAppComplete(event:Event):void
{
/*
NOTE : this method is only called from the FlexEvent.APPLICATION_COMPLETE
so the chances are that this is definitely a Flex application.
*/
if(SystemManager(event.currentTarget).application != null)
{
var flexApplication:* = SystemManager(event.currentTarget).application;
trace("flexApplication : "+flexApplication);
trace(flexApplication.SetProperties("assets/xml/eurostar.xml","old state"));
}
}
}
}
@leeprobert
Copy link
Author

Be aware that the Flash movie will need to include the Flex framework SWC file so that it can use the mx.managers.SystemManager class. This is then used to detect if the loaded content is indeed a Flex Application and allows you to switch from logic for processing a normal Flash SWF and the Flex Application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment