5 Replies Latest reply on May 13, 2013 4:11 PM by kglad

    Import/embed XML file for export?

    LQ_Yisheng

      How do I import or embed an XML file so I can export a 100% independent SWF? My project reads an external XML file for text fields and images. When I export the SWF, none of the images/text will show unless I have the XML file in the same directory. I want a 100% independent SWF without the need for the XML.

       

      Here's what I have so far, this is from a tutorial:

       

      =============================

      import com.greensock.*;

      import flash.events.Event; 

      import flash.events.MouseEvent;

      import flash.events.KeyboardEvent;

      import flash.display.MovieClip; 

      import flash.media.Sound;

      import flash.media.SoundChannel;

      import flash.utils.ByteArray;

       

       

      var imageArray:Array = new Array();

      var painterArray:Array = new Array();

      var titleArray:Array = new Array();

      var dateArray:Array = new Array();

       

       

      //Loader event for the XML

      var loader:URLLoader = new URLLoader();

      loader.addEventListener(Event.COMPLETE, onLoaded);

       

       

      var xml:XML;

       

       

      loader.load(new URLRequest("mday.xml"));

       

       

      function onLoaded(e:Event):void {

          //load XML

          xml=new XML(e.target.data);

          var il:XMLList=xml.images;

          listLength=il.length();

          //fill up the array from XML

          populateArray();

      }

       

       

      function populateArray():void {

          //takes the properties defined in the XML and stores them into arrays

          var i:Number;

          for (i = 0; i < listLength; i++) {

              imageArray[i]=xml.images[i].pic;

              titleArray[i]=xml.images[i].title;

              painterArray[i]=xml.images[i].painter;

              dateArray[i]=xml.images[i].date;

          }

      }

       

      =====================

       

      The AS is attached to a keyframe, not in its separate class.

       

      Thanks.

        • 1. Re: Import/embed XML file for export?
          Mournie

          Hi,

           

          if you want to have an completely independent SWF, you cannot work with an external XML file.

           

          Try to store the content of the XML file in you XML variable inside the code.

           

          var xml:XML = "

          <tag>

               <subtag1>value</subtag1>

               <subtag2>value</subtag2>

               <subtag2>value</subtag3>

          </note>";

           

          But then you will lose the dynamic nature of this. You always need to change the XML content inside flash and re-export it for every change.

           

          Regards Michael

          1 person found this helpful
          • 2. Re: Import/embed XML file for export?
            LQ_Yisheng Level 1

            I tried that but now I'm having some null errors on textfields. I'll post my entire code here:

             

            ===================================

            infoBox.alpha=1;
            theImage.alpha=1;
            loadingBar.visible = false;

            //variables to hold the final coordinates of the image tween
            var finalX:Number;
            var finalY:Number;
            //variable to hold the number of images in the XML
            var listLength:Number;

            //keeps track of what image should be displayed
            var currPainting:Number=0;

            //arrays to hold the contents of the XML, using this to allow
            var imageArray:Array = new Array();
            var painterArray:Array = new Array();
            var titleArray:Array = new Array();
            var dateArray:Array = new Array();

            //Loader event for the XML

            var xml:XML =
            <xml>
            <images>
                   
            <pic>images/3.png</pic>
                   
            <en>Painter</en>
                   
            <cn>画家</cn>
                   
            <misc></misc>
            </images>
            </xml>

            var loader:URLLoader = new URLLoader();
            loader.addEventListener(Event.COMPLETE, loadXML);

            function loadXML():void {
                //load XML
                var il:XMLList=xml.images;
                listLength=il.length();

                //fill the empty arrays with XML items
                populateArray();
            }

            startButton.addEventListener(MouseEvent.CLICK, onStartClick);

            function onStartClick(event:MouseEvent):void{
                populateArray();

                beginImage();
            }  

            function populateArray():void {
                //takes the properties defined in the XML and stores them
                //into arrays
                var i:Number;
                for (i = 0; i < listLength; i++) {
                    imageArray[i]=xml.images[i].pic;
                    titleArray[i]=xml.images[i].cn;
                    painterArray[i]=xml.images[i].en;
                    dateArray[i]=xml.images[i].misc;
                }
            }

            function beginImage():void {

                //load description
                infoBox.theArtist.text=painterArray[currPainting];
                infoBox.theTitle.text=titleArray[currPainting];
                infoBox.theDate.text=dateArray[currPainting];

                theImage.scaleX=1;
                theImage.scaleY=1;

                var imageLoader = new Loader();

                //catches errors if the loader cannot find the URL path
                imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchFunction);
                //actually loads the URL defined in the image array
                imageLoader.load(new URLRequest(imageArray[currPainting]));
                //adds a listener for while the image is loading
                imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imgLoading);
                //adds a listener for what to do when the image is done loading
                imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);

                function catchFunction(e:IOErrorEvent) {
                    trace("Bad URL: " + imageArray[currPainting] + " does not exist");
                    //take out the bad URL from the array
                    imageArray.splice(currPainting,1);
                    titleArray.splice(currPainting,1);
                    painterArray.splice(currPainting,1);
                    dateArray.splice(currPainting,1);

                    //check to see if there are images left,
                    //else restart the slideshow
                    if (imageArray.length==0) {
                        populateArray();
                    } else {
                        beginImage();
                    }
                }

                function imgLoaded(event:Event):void {
                    loadingBar.visible = false;

                    //add the image and get the dimensions to center the image
                    theImage.addChild(imageLoader);
                    //take the contents of the loaded image and cast it as bitmap data
                    //to allow for bitmap smoothing
                    var image:Bitmap = imageLoader.content as Bitmap;
                    image.smoothing=true;
                    theImage.x = (stage.stageWidth/2) - (imageLoader.content.width / 2);
                    theImage.y = (stage.stageHeight/2) - (imageLoader.content.height / 2);

                    finalX = (stage.stageWidth/2) - (imageLoader.content.width * .8 / 2);
                    finalY = (stage.stageHeight/2) - (imageLoader.content.height * .8 / 2);

                    //start tween function
                    easeIn();

                }
            }

            function easeIn():void {
                TweenLite.to(theImage, 11.6, {scaleX:.8, scaleY:.8, x:finalX, y:finalY, onComplete:hideStuff});
                TweenLite.to(theImage, 1, {alpha:1, overwrite:0});
                TweenLite.to(infoBox, 1, {alpha:1});
            }

            function hideStuff():void {
                TweenLite.to(theImage, 1, {alpha:0, onComplete:nextImage});
                TweenLite.to(infoBox, 1, {alpha:0});
            }

            function nextImage():void {
                //take out the image that was just displayed by deleting the prev XML node
                imageArray.splice(currPainting,1);
                titleArray.splice(currPainting,1);
                painterArray.splice(currPainting,1);
                dateArray.splice(currPainting,1);

                //remove the picture
                theImage.removeChildAt(0);

                //start over
                if (imageArray.length==0) {
                    //fill up array again
                        populateArray();
                } else {
                    beginImage();
                }
            }

            ========================================

             

            I'm getting the 2007 error "Parameter text must be non-null" for this script. I think it has to do with the arrays, as if I trace(painterArray) I get "undefined".

             

            Also, as you can see it also depends on external image files. How do I edit it so that it uses image files from the library instead, for the 100% independent SWF?

             

            Thanks.

            • 3. Re: Import/embed XML file for export?
              kglad Adobe Community Professional & MVP

              you need to call loadXML().

              1 person found this helpful
              • 4. Re: Import/embed XML file for export?
                LQ_Yisheng Level 1

                Wow thanks. That was a simple fix, guess I overlooked that.

                Does anyone know how I would embed all the images though? I've already imported all the images into the library. Right now it still needs to have an external directory with images for the SWF to work.

                • 5. Re: Import/embed XML file for export?
                  kglad Adobe Community Professional & MVP

                  assign a class to each image and use the "new" constructor to create an instance of each image.

                   

                  if you add each image's class name to imageArray, you would use something like:

                   

                  var C:Class=Class(getDefinitionByName(imageArray[currPainting]));

                  var image:BitmapData=new C();

                  var imageBMP:Bitmap=new Bitmap(image);

                  theImage.addChild(imageBMP);