4 Replies Latest reply on Apr 29, 2009 2:08 AM by stace20

    load SWFLoader from button on SWF

    stace20 Level 1

      Hello there - I have 2 SWFloaders in my Flex app, that correspond to a content box for lessons, and a virtual tutor box for videos.  All of the content are AS3 swfs.  I want to place buttons in my content so that I can change out the SWFs in either of the SWFLoaders in the Flex app.  Does anybody know the code I would need in order to do that in my Flex app, as well as in my FLA files?  A really crude demo of our curriculum page is here (just working on coding at this point, design is only bones!)

       

      sample.jpg

       

      Here is the code I'm using to load the SWFLoaders in the Flex app:

       

       

      <mx:Script>

      <![CDATA[

      import mx.events.ListEvent;


      private function tree_itemClick(evt:ListEvent):void {

      var t:Tree = evt.currentTarget as Tree;

      var dataObj:Object = evt.itemRenderer.data;

      if (dataObj.hasOwnProperty("@mainswf")) {

      swfLoader.source = dataObj.@mainswf;

      } else if (t.dataDescriptor.isBranch(t.selectedItem)) {

      swfLoader.source = null;

      panel.status = "";

      }

      }


      private function tree_labelFunc(item:Object):String {

      var suffix:String = "";

      if (tree.dataDescriptor.isBranch(item)) {

      suffix = " (" + item.children().length() + ")";

      }

      return item.@label + suffix;

      }


      private function swfLoader_complete(evt:Event):void {

      panel.status = (swfLoader.bytesTotal/1024).toFixed(2) + 'KB';

      }

      ]]>

      </mx:Script>



      <mx:Script>

      <![CDATA[

      import mx.events.ListEvent;


      private function tree_itemClick2(evt:ListEvent):void {

      var t:Tree = evt.currentTarget as Tree;

      var dataObj:Object = evt.itemRenderer.data;

      if (dataObj.hasOwnProperty("@tutorswf")) {

      swfTeacher.source = dataObj.@tutorswf;

      } else if (t.dataDescriptor.isBranch(t.selectedItem)) {

      swfTeacher.source = null;

      panel.status = "";

      }

      }


      private function tree_labelFunc2(item:Object):String {

      var suffix:String = "";

      if (tree.dataDescriptor.isBranch(item)) {

      suffix = " (" + item.children().length() + ")";

      }

      return item.@label + suffix;

      }


      private function swfTeacher_complete(evt:Event):void {

      panel.status = (swfTeacher.bytesTotal/1024).toFixed(2) + 'KB';

      }

      ]]>

      </mx:Script>

       

      If anybody has any ideas how to do this I'd be ecstatic to hear it!

       

      warm wishes

      Stacey

        • 1. Re: load SWFLoader from button on SWF
          ntsiii Level 3

          You could have you loaded swf dispatch a bubbling event in the buttonClick handler, and listen for that event in the main app.

           

          Tracy Spratt

          • 2. Re: load SWFLoader from button on SWF
            ntsiii Level 3

            Also, if the SWFs you are loading are not Flex SWFs, you should use the Flex Component Kit to export them for use in Flex.

             

            Tracy

            1 person found this helpful
            • 3. Re: load SWFLoader from button on SWF
              stace20 Level 1

              Hi Tracy thanks for your replies.  I found somebody to help me on getacoder.com - this code works perfectly.

               

              The flex code:

               

              <mx:Script>

              <![CDATA[

               

              public function FromSwf1(value:String):void

              {

              swfLoader.source = value;

              }

               

              public function FromSwf2(value:String):void

              {

              swfTeacher.source = value;

              }

               

               

              ]]>

              </mx:Script>

               

               

              The AS3 code:

               

              var flexApp = root.parent.parent['parentApplication'];

              btn1.addEventListener(MouseEvent.CLICK, CallFlexFunction);

              //stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);

              function CallFlexFunction(e:MouseEvent) {

              flexApp.FromSwf1("assets/ninos/alphabet.swf");

              }

               

              btn2.addEventListener(MouseEvent.CLICK, CallFlexFunction2);

              //stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);

              function CallFlexFunction2(e:MouseEvent) {

              flexApp.FromSwf2("assets/ninos/clase_de_ninos.swf");

              }

              • 4. Re: load SWFLoader from button on SWF
                stace20 Level 1

                One question: I do have the Flex component for Flash installed - is there a reason to use this when publishing my SWFs? Will that give me some kind of added functionality or ability for Flex to do more with them? My swf files will mainly just contain buttons that open up swf videos and some audio.