Return to Snippet

Revision: 41385
at February 17, 2011 04:40 by jeffnehlsen


Updated Code
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" 
			   creationComplete="onCreationComplete(event)">
	<fx:Script>
		<![CDATA[
			import com.greensock.*;
			import com.greensock.events.LoaderEvent;
			import com.greensock.loading.*;
			import com.greensock.loading.display.FlexContentDisplay;
			import customEvents.CustomDataEvent;
			import mx.controls.Alert;
			
			private var swfContainer:FlexContentDisplay;
			private var swfRaw:MovieClip;
			private var swfLoader:SWFLoader;
			
			private function onCreationComplete(e:Event):void {
				// Tells LoaderMax that it will be working in Flex.
				LoaderMax.contentDisplayClass = FlexContentDisplay;
				
				// This statement allows LoaderMax to parse SWF URLs if you were to 
				// attempt to use a generic loader component.  Handy if you plan
				// on loading multiple types of objects dynamically through XML.
				LoaderMax.activate([SWFLoader]);
				
				// The next three lines load my SWF and configure it.  When it is loaded,
				// the initSwf event listener will be triggered.
				var url:String = "Component.swf";
				swfLoader = new SWFLoader(url,{x:50, y:50, container:swfDisplayArea, onComplete:initSwf, noCache:true});
				swfLoader.load();         
			}
			
			// This is called once the SWF is loaded.  It will call this.
			// This function sets a 'raw data' variable so the root of the
			// SWF can be easily accessed within this file.  Then it tries
			// to grab some data from the SWF, and creates an event listener
			// that will be dispatched when the SWF's button is clicked.
			public function initSwf(event:LoaderEvent):void
			{
				swfRaw = event.target.rawContent;
				
				Alert.show(swfRaw.myTestData);
				swfRaw.addEventListener(CustomDataEvent.SAVE_DATA, onSaveData);
			}
			
			// When the button in the SWF is clicked, a CustomDataEvent is dispatched
			// to be caught here.  This listener will display a message containing data
			// that is stored in the CustomDataEvent's data object.
			private function onSaveData(e:CustomDataEvent):void {
				Alert.show("Save data event retrieved! Data inside: " + e.data.testString);
				
				// Now, to pass a command back to the SWF, you simply need to make a method
				// call to your swfRaw variable.  In this example, when this event listener
				// is fired off, testFunction will cause the button in the SWF to disappear.
				swfRaw.testFunction();
			}
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<s:Group id="swfDisplayArea"  
			 useHandCursor="false" 
			 buttonMode="true"  
			 mouseChildren="true" 
			 width="500"
			 height="500"
			 y="0"
			 x="0">
	</s:Group>
</s:Application>

Revision: 41384
at February 17, 2011 04:30 by jeffnehlsen


Initial Code
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" 
			   creationComplete="onCreationComplete(event)">
	<fx:Script>
		<![CDATA[
			import com.greensock.*;
			import com.greensock.events.LoaderEvent;
			import com.greensock.loading.*;
			import com.greensock.loading.display.FlexContentDisplay;
			import customEvents.CustomDataEvent;
			import mx.controls.Alert;
			
			private var swfContainer:FlexContentDisplay;
			private var swfRaw:MovieClip;
			private var swfLoader:SWFLoader;
			
			private function onCreationComplete(e:Event):void {
				// Tells LoaderMax that it will be working in Flex.
				LoaderMax.contentDisplayClass = FlexContentDisplay;
				
				// This statement allows LoaderMax to parse SWF URLs if you were to 
				// attempt to use a generic loader component.  Handy if you plan
				// on loading multiple types of objects dynamically through XML.
				LoaderMax.activate([SWFLoader]);
				
				// The next three lines load my SWF and configure it.  When it is loaded,
				// the initSwf event listener will be triggered.
				var url:String = "Component.swf";
				swfLoader = new SWFLoader(url,{x:50, y:50, container:swfDisplayArea, onComplete:initSwf, noCache:true});
				swfLoader.load();         
			}
			
			// This is called once the SWF is loaded.  It will call this.
			// This function sets a 'raw data' variable so the root of the
			// SWF can be easily accessed within this file.  Then it tries
			// to grab some data from the SWF, and creates an event listener
			// that will be dispatched when the SWF's button is clicked.
			public function initSwf(event:LoaderEvent):void
			{
				swfRaw = event.target.rawContent;
				
				Alert.show(swfRaw.myTestData);
				swfRaw.addEventListener(CustomDataEvent.SAVE_DATA, onSaveData);
			}
			
			// When the button in the SWF is clicked, a CustomDataEvent is dispatched
			// to be caught here.  This listener will display a message containing data
			// that is stored in the CustomDataEvent's data object.
			private function onSaveData(e:CustomDataEvent):void {
				Alert.show("Save data event retrieved! Data inside: " + e.data.testString);
			}
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<s:Group id="swfDisplayArea"  
			 useHandCursor="false" 
			 buttonMode="true"  
			 mouseChildren="true" 
			 width="500"
			 height="500"
			 y="0"
			 x="0">
	</s:Group>
</s:Application>

Initial URL

                                

Initial Description

                                

Initial Title
Loading SWF using LoaderMax in MXML

Initial Tags

                                

Initial Language
ActionScript 3