7 Replies Latest reply on Feb 26, 2010 10:50 AM by Flex harUI

    New Sprite Not Showing Up

    alice_data Level 1

      Hi,

       

        I have a map here that is initialized when the app gets compiled, and I also have a slider that I attempt to use and change the map accordingly based on the depth of colors of individual counties of users' choice. For example, if a user chooses 5, then there would be 5 colors on the map.

       

        All the calculation seems to be working great, other than the fact that I cannot see the "changed" map.

       

        Here is my SvgMap.as:

       

      package map
      {
           import colorize.*;
           import com.zavoo.svg.*;
           import flash.display.DisplayObject;
           import flash.display.Sprite;
           import flash.events.Event;
           import flash.net.URLLoader;
           import flash.net.URLRequest;
           import flash.net.URLVariables;
           import mx.core.UIComponent;

       

          public class SvgMap extends UIComponent {
             
          public var canvas:Sprite;
              [Embed(source="USA_Counties_with_FIPS_and_names.svg")]
              public var usaMap:Class;
          public var paths:SvgPaths;                 
          private var from:Array;   
          private var stateColors:Array;
          
          public function SvgMap():void {
                 
                      canvas = new usaMap();
                      canvas.width = 650;
                      canvas.height = 500;
                      addChild(canvas);                                                      
              }
         
           public function setParameters(passedArray:Array,                passedArray7:Array):void {
                  from = passedArray;           
                  stateColors= passedArray7;        changeMap(from,stateColors);                                          
              }        //Called from the slider class
              public function changeMap(passedArray:Array,passedArray7:Array):void{           
             
              canvas = new Sprite();
              addChild(canvas);             
              var from_string:String = from.join("-");
              var state_colors_string:String = stateColors.join("-");       
                 var loader:URLLoader = new URLLoader();
          var url:String = "http://localhost/generic.php";        //The PHP is without errors
               var request:URLRequest = new URLRequest(url);
           var variables:URLVariables = new URLVariables();
           request.method = "POST";
           variables.from = from_string;
           variables.state_colors = state_colors_string;
           request.data = variables;
           loader.load(request);       
           loader.addEventListener(Event.COMPLETE, onLoadComplete);                         
          }       
             
              public function onLoadComplete(event:Event):void {
                      var loader:URLLoader = URLLoader(event.target);
                      paths = new SvgPaths(loader.data);      
                      paths.drawToGraphics(canvas.graphics, 2, 10, 10);
                  }           
          }
      }

       

      Here is my main app:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%"
          height="100%" creationComplete="addsvgMap()">
          <mx:Script>
              <![CDATA[      
                 import mx.events.FlexEvent;        
                 import com.zavoo.svg.*;
                 import flash.display.Sprite;  
                 import colorize.*;   
                 import map.SvgMap;         
                 import flash.events.Event;
                 import flash.display.Graphics;   
                 import mx.events.SliderEvent;
                 import mx.core.IUIComponent;
         
                 public var svg:SvgMap = new SvgMap();      
                  public function addsvgMap():void{             horizontal_bar.addChildAt(svg,0);
                   }           
                                      
              ]]>
          </mx:Script>
         
          <mx:VBox id="vbox" x="20" y="10">           
              <mx:Canvas id="horizontal_bar" width="350" height="520" />     
          </mx:VBox>         
          </mx:Application>

       

      Can anyone please give me some pointers on why my map is not showing up?
      Thanks for your help.

        • 1. Re: New Sprite Not Showing Up
          Flex harUI Adobe Employee

          If you follow the "recipe" in the doc on creating custom components, you'll

          see that you need to report a valid measuredWidth/Height in order to be

          given a valid size.

          • 2. Re: New Sprite Not Showing Up
            alice_data Level 1

            Hi,

             

              I have taken a look at the document, and have modified my code of where the "sprite" by adding in the "width and height". In order to see if the original child has been removed, I have added some trace statements.

             

              Looks like that there is something going on with my "urlrequest", which is not something I had anticipated before.

              Thanks for your help.

            • 3. Re: New Sprite Not Showing Up
              Flex harUI Adobe Employee

              The docs should be encouraging you to add measure() and updateDisplayList()

              methods.

               

              Or try giving a width and height to SvgMap itself.

              • 4. Re: New Sprite Not Showing Up
                alice_data Level 1

                Hi,

                 

                  I did have height and width specified in SvgMap(), and here it is:

                 

                  public function SvgMap():void {
                           
                                canvas = new usaMap();
                                canvas.width = 650;
                                canvas.height = 500;
                                addChild(canvas);
                        }

                 

                As a matter of fact, with my actionscript class being able to remove the original child, which has a different parent than the newly created one. So, I guess that the new Sprite is there, but it is the "image" not showing up, right?

                 

                Thanks for your help.

                 

                Alice

                • 5. Re: New Sprite Not Showing Up
                  Flex harUI Adobe Employee

                  That code gives size to usaMap, not SvgMap.  However, UIComponents don't

                  clip so you should've seen something.  You can check the

                  transform.pixelBounds of the canvas to see if it thinks anything got drawn

                  in there.

                  1 person found this helpful
                  • 6. Re: New Sprite Not Showing Up
                    alice_data Level 1

                    Hi,

                     

                    Here is the code from another function that is supposed to load the new map:

                     

                    public function changeMap(passedArray:Array,passedArray7:Array):void{   
                                    
                            removeChild(this.canvas);              
                            canvas = new Sprite();       
                            addChild(canvas);     
                               
                            var from_string:String = from.join("-");
                            var state_colors_string:String = stateColors.join("-");       
                              
                             var loader:URLLoader = new URLLoader();          
                            var url:String = "http://localhost/generic.php?from=" + from_string + "&state_colors=" + state_colors_string;
                            var variables:URLVariables = new URLVariables();
                            variables.from= from_string;
                            variables.state_colors = state_colors_string;
                            var encode:String= encodeURI(url);   
                            var pattern:RegExp = /#/g;
                            var decode:String = encode.replace(pattern,"%23");   
                            var request:URLRequest = new URLRequest(decode);       
                            request.method = "GET";       
                            loader.load(request);       
                            loader.addEventListener(Event.COMPLETE, onLoadComplete);
                            }       
                           
                            public function onLoadComplete(event:Event):void {
                                    var loader:URLLoader = URLLoader(event.target);                         
                                    paths = new SvgPaths(loader.data);      //the canvas exists
                                    paths.drawToGraphics(this.canvas.graphics, 2, 10, 10);
                                    var trans:Transform = new Transform(canvas);
                                    trace(trans.pixelBounds);  
                            }           
                        }

                     

                    Here is the output that I got:

                     

                    Canvas Parent: SvgMap4
                    (x=73, y=50, w=5529, h=3496)

                     

                    So, I guess that something is there, but the graphics has never been loaded. It takes some time to load the image, but nothing is changed fro the initialized state. So, I guess this is not a sprite problem?

                     

                    Thanks for your help.

                    • 7. Re: New Sprite Not Showing Up
                      Flex harUI Adobe Employee

                      Well, your sprite has no content yet, so it will report zero width/height.

                      Check again after it has some content.