4 Replies Latest reply on May 31, 2011 8:01 AM by Ned Murphy

    Populate var using XML

    LeviArr12398

      Hi,

       

      I have just started using Flash (im useing Flash CS3) and I am trying to get this project to work. I have a scrolling thumbnail gallery, and I want to be able to adjust how many thumbnails are initally loaded based on a value in my XML file (so that the gallery can later be modified without have to actually go into the .fla )

       

      When I do this I get

       

      var myXML:XML;
      var myLoader:URLLoader = new URLLoader();

      myLoader.load(new URLRequest("galaxy.xml"));
      myLoader.addEventListener(Event.COMPLETE, processXML);

      function processXML(e:Event):void {
           myXML = new XML(e.target.data);
           trace(myXML.thumbnails.thumbnail); // output is 9, which is correct
      }

       

       

       

      but if I move that trace to here...

       

      var myXML:XML;
      var myLoader:URLLoader = new URLLoader();

       

      myLoader.load(new URLRequest("galaxy.xml"));
      myLoader.addEventListener(Event.COMPLETE, processXML);

       

      function processXML(e:Event):void {
          myXML = new XML(e.target.data);
         }
      trace(myXML.thumbnails.thumbnail); //output is TypeError: Error #1009: Cannot access a property or method of a null object reference.

       

       

      I have been trying to figure this out for a couple of days. I understand that this does not work because I am trying to trace the "thumbnail" in my XML before it is loaded, but I have been unable to find a solution that works for me. Basiclly I want to be able to define 3 or 4 variables in my XML and use them in my AS3 later.

      The important variables I wish to define are:

      var thumbsize

      var n:Number

      var stageVar:Number

       

      Here is the full code.

       

       

      this.scaleMode = StageScaleMode.NO_SCALE;
      this.align = StageAlign.TOP;

      var myXML:XML;
      var myLoader:URLLoader = new URLLoader();

      myLoader.load(new URLRequest("galaxy.xml"));
      myLoader.addEventListener(Event.COMPLETE, processXML);

      function processXML(e:Event):void {
           myXML = new XML(e.target.data);
           trace(myXML.thumbnails.thumbnail);
      }
      //trace(myXML.thumbnails.thumbnail)

      var thumbsize = 100;//(myXML.thumbnails.thumbsize);// spaceing, 100 is tight together, 101 has 1 pix space
      var n:Number = 8;//(myXML.thumbnails.thumbnail);//number of thumbnails to create
      var stageVar:Number =  800;// (myXML.stage.stadgewidth)
      Stage.width = 800;//(myXML.stagesize.stadgewidth); //800
      var id:Number;
      var a:Number = 0;
      var basename:String = "universe";
      var speed:Number = 10;
      var touchingBar:Boolean = false;

      maskShape.width = 800;//800 or n * thumbsize
      maskShape.x = holder.x;
      maskShape.y = holder.y;
      maskShape.height = thumbsize * 2;// multiplied by 2 to cover the reflection

      var blank:Sprite = new Sprite();//populates and is deleted to prevent error when trying to remove old image when a new one is clicked
      loadArea.addChildAt(blank, 1);

      holder.mask = maskShape;


      while ( a < n ) {

           with (holder) {

                var thumb_holder:MovieClip = new thumb();
                thumb_holder.x = a * thumbsize;
                thumb_holder.id = a;
                addChild(thumb_holder);

                var thumb_holder2:MovieClip = new thumb();
                thumb_holder2.x = (a + n) * thumbsize;
                thumb_holder2.id = a;
                addChild(thumb_holder2);

                var thumb_holder3:MovieClip = new thumb();
                thumb_holder3.x = (a - n) * thumbsize;
                thumb_holder3.id = a;
                addChild(thumb_holder3);

           }
           a = a + 1;
      }



      this.addEventListener(Event.ENTER_FRAME, movingCode);

      function movingCode(event:Event):void {

           holder.x = holder.x + speed;


           //repeating thumbnails
           if (holder.x >= thumbsize * n && speed > 0) {
                holder.x = 0;
           }
           if (holder.x < thumbsize * -n && speed < 0) {
                holder.x = 0;
           }
           //change the speed of the thumbnails

           if (touchingBar == false) {

                if (root.mouseX < stageVar * .23) {
                     speed = 10;
                }
                if (root.mouseX < stageVar * .15) {
                     speed = 20;
                }
                if (root.mouseX > stageVar * .85) {
                     speed = -10;
                }
                if (root.mouseX > stageVar * .94) {
                     speed = -20;
                }
                //stop the thumbnail scroll
                if (root.mouseX > stageVar * .4 && root.mouseX < stageVar * .8) {
                     speed = 0;
                }
           }
           if (touchingBar == true) {
                speed = 0;
           }

      }

       

       

       

       

      I am sure this is a totally noob thing, and has been asked a hundred times before me, but any help at all will be fantastic.

       

      Thanks!

        • 1. Re: Populate var using XML
          Espis1-kP5SjS Level 1

          When you moved the trace outside the function, it became a part of the main program which is executed before the function had a chance to load and execute.  Hence, the XML file did not exist at the time the trace statement was executed.  You will only get a correct trace when it is inside the function.

          • 2. Re: Populate var using XML
            Ned Murphy Adobe Community Professional & MVP

            Code doesn't wait to execute, so the code after that function will execute long before the file ever gets loaded.  Just as the first trace worked for you, you need to assign values to the variables that are assigned from the xml inside the function that gets executed when loading is complete.  Anything that relies on the xml data needs to be held off from executing originating from within that function.

            • 3. Re: Populate var using XML
              Ned Murphy Adobe Community Professional & MVP

              One other thing, as a general practice, you should always assign the loading listeners before you execute the load command.

              1 person found this helpful
              • 4. Re: Populate var using XML
                kglad Adobe Community Professional & MVP

                loading external files is asynchronous.  ie, immediately after your load statement, loading is NOT complete.

                 

                so, you should use a function call to execute code that depends on anything loaded:



                 

                var myXML:XML;
                var myLoader:URLLoader = new URLLoader();

                 

                myLoader.load(new URLRequest("galaxy.xml"));
                myLoader.addEventListener(Event.COMPLETE, processXML);

                 

                function processXML(e:Event):void {
                    myXML = new XML(e.target.data);

                traceF();
                   }

                 

                function traceF():void{
                trace(myXML.thumbnails.thumbnail);

                }

                 


                1 person found this helpful