How do I utilize flashVars in AS3 classed based program?

mcjim2k
mcjim2k used Ask the Experts™
on
I have recently purchased a flash scroll bar. It looks and works great; however, it did not come configured to use flashVars, and I really need that flexibility with the way I'm using the program. My Flash experience is extremely limited, this is really my first venture into using flash, but I am very familiar with OO programming.

The whole program came with the following files, example.fla, example.swf, Main.as and all of the *.as source files and was programed using Action Script 3. In one of the primary source files, two variables are statically defined, and I would like those to be dynamically defined using flashVars. These variables are the paths to the config and data XML files.

I'm attaching two different code segments, the first is the whole Main.as file, and the other is a *.as file in the source folder where I would like to dynamically define the variables. I've only attached the top part of the second file as the variables are defined at the very top. I have attempted to use, root.loaderInfo.parameters.data_file, but I get the compilation error:
1120: Access of undefined property root.

Does any one have any ideas on how to properly implement flashVars into this program?

Thanks a lot.
// Main.as
package {	
	import flash.display.MovieClip;
	
	public class Main extends MovieClip {		
		// Gather FlashVars
		var flashVars:String = LoaderInfo(root.loaderInfo).parameters.config_file;		
		public function Main() {		
			init();		
		}		
		private function init():void {		
			// Create a new HorizontalMenu, set x and y coordinates.
			var menu:HorizontalMenu = new HorizontalMenu();		
			// Add it to your movie clip
			this.addChild(menu);	
		}		
	}			
}

// HorizontalMenu.as
package {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
		
	public class HorizontalMenu extends MovieClip {			
		
		// The location of the config xml file
		public static const CONFIG_XML_URL:String = "config.xml";
		
		// The location of the data xml file
		public static const DATA_XML_URL:String = "data.xml";

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Engineering Manager / Architect
Commented:
I'd say that instead of setting the variable to something else in the class, to send it in the constructor method.

Check it out:

<code>

-V
// Main.as
package {       
        import flash.display.MovieClip;
        
        public class Main extends MovieClip {           
                // Gather FlashVars
                var configFile:String = LoaderInfo(root.loaderInfo).parameters.config_file;
                var dataFile:String = LoaderInfo(root.loaderInfo).parameters.data_file;
                public function Main() {                
                        init();         
                }               
                private function init():void {          
                        // Create a new HorizontalMenu, set x and y coordinates.
                        var menu:HorizontalMenu = new HorizontalMenu(configFile,dataFile);         
                        // Add it to your movie clip
                        this.addChild(menu);    
                }               
        }                       
}

// HorizontalMenu.as
package {
        
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.GradientType;
        import flash.display.MovieClip;
        import flash.display.PixelSnapping;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.geom.Matrix;
        import flash.geom.Rectangle;
                
        public class HorizontalMenu extends MovieClip {                 
                
                // The location of the config xml file
                public var CONFIG_XML_URL:String = "config.xml";
                
                // The location of the data xml file
                public var DATA_XML_URL:String = "data.xml";
                
                // Keeping "config.xml" and "data.xml" as the default values.

                public function HorizontalMenu(configFile,dataFile) {
                        // whatever there is right now
                        CONFIG_XML_URL = configUrl;
                        DATA_XML_URL = dataUrl;
                }

Open in new window

Author

Commented:
That's exactly what I needed, it's working perfectly now. Thanks a lot.
Antonio EstradaEngineering Manager / Architect

Commented:
Glad to help :)

-V

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial