Loading SWF using LoaderMax in MXML


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
  5. creationComplete="onCreationComplete(event)">
  6. <fx:Script>
  7. <![CDATA[
  8. import com.greensock.*;
  9. import com.greensock.events.LoaderEvent;
  10. import com.greensock.loading.*;
  11. import com.greensock.loading.display.FlexContentDisplay;
  12. import customEvents.CustomDataEvent;
  13. import mx.controls.Alert;
  14.  
  15. private var swfContainer:FlexContentDisplay;
  16. private var swfRaw:MovieClip;
  17. private var swfLoader:SWFLoader;
  18.  
  19. private function onCreationComplete(e:Event):void {
  20. // Tells LoaderMax that it will be working in Flex.
  21. LoaderMax.contentDisplayClass = FlexContentDisplay;
  22.  
  23. // This statement allows LoaderMax to parse SWF URLs if you were to
  24. // attempt to use a generic loader component. Handy if you plan
  25. // on loading multiple types of objects dynamically through XML.
  26. LoaderMax.activate([SWFLoader]);
  27.  
  28. // The next three lines load my SWF and configure it. When it is loaded,
  29. // the initSwf event listener will be triggered.
  30. var url:String = "Component.swf";
  31. swfLoader = new SWFLoader(url,{x:50, y:50, container:swfDisplayArea, onComplete:initSwf, noCache:true});
  32. swfLoader.load();
  33. }
  34.  
  35. // This is called once the SWF is loaded. It will call this.
  36. // This function sets a 'raw data' variable so the root of the
  37. // SWF can be easily accessed within this file. Then it tries
  38. // to grab some data from the SWF, and creates an event listener
  39. // that will be dispatched when the SWF's button is clicked.
  40. public function initSwf(event:LoaderEvent):void
  41. {
  42. swfRaw = event.target.rawContent;
  43.  
  44. Alert.show(swfRaw.myTestData);
  45. swfRaw.addEventListener(CustomDataEvent.SAVE_DATA, onSaveData);
  46. }
  47.  
  48. // When the button in the SWF is clicked, a CustomDataEvent is dispatched
  49. // to be caught here. This listener will display a message containing data
  50. // that is stored in the CustomDataEvent's data object.
  51. private function onSaveData(e:CustomDataEvent):void {
  52. Alert.show("Save data event retrieved! Data inside: " + e.data.testString);
  53.  
  54. // Now, to pass a command back to the SWF, you simply need to make a method
  55. // call to your swfRaw variable. In this example, when this event listener
  56. // is fired off, testFunction will cause the button in the SWF to disappear.
  57. swfRaw.testFunction();
  58. }
  59.  
  60. ]]>
  61. </fx:Script>
  62. <fx:Declarations>
  63. <!-- Place non-visual elements (e.g., services, value objects) here -->
  64. </fx:Declarations>
  65.  
  66. <s:Group id="swfDisplayArea"
  67. useHandCursor="false"
  68. buttonMode="true"
  69. mouseChildren="true"
  70. width="500"
  71. height="500"
  72. y="0"
  73. x="0">
  74. </s:Group>
  75. </s:Application>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.