Link to home
Start Free TrialLog in
Avatar of dlearman1
dlearman1Flag for United States of America

asked on

How to access loaded StyleSheet from master (main) swf using Flash AS3

I have a class CSSLoader that loads css data into a stylesheet, '_flashStyles'.  This works as planned.  I have a master swf that controls a site.  Master instantiates a new CSSLoader and loads the external css.  This also works. Various pages are loaded into the master swf when called by the site navigation.  These pages have text to be styled by the  loaded stylesheet, but I'm unable to apply the stylesheet to the text.  I've tried approaches like 'myText.styleSheet = CSSLoader._flashStyles without success.  can any body point me in the right direction?  This is an AS3 question.
Avatar of iamlfetch
iamlfetch

have you got got styles working before, incase you didnt know this but it has to be done in the correct order and you must use htmlText instead:

var label:TextField = new TextField();
label.styleSheet = CSSLoader._flashStyles;
label.htmlText = "<h1>header eg</h1>";
addChild(label);

not sure how you are defining _flashStyles, i would use something like:
package
{
    import flash.text.StyleSheet;
    public class CSSLoader
    {
        public static var _flashStyles:StyleSheet = StyleSheetMaker('link');
       public static function StyleSheetMaker ()
        {
            .....
          }
    }  
}
Avatar of dlearman1

ASKER

Thanks for your comment.  I have attached the related code.  Sorry about throwing so much code at ypu, but I didn't see a way to shorten it.

The basic idea is to have a reusable Class CSSLoader located in an directory outside the current project directory (com.pricelearman.loaders).  Individual projects (websites) would than have a Master (Main) Class which would instantiate CSSLoader to load the css files for that particular project.

Where I am now:  Master can instantiate CSSLoader and successfully load the css data from an external file (_css/flashStyles).  My problem is once loaded I cannot see how to access the loaded stylesheet for use within Master or within additional SWF's that might be loaded into Master.

What I'm trying to do is build a framework for creating a single frame, full-Flash website.




// Class to load CSS
//
package com.pricelearman.loaders {
	
    import flash.events.*;
    import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.*;
	import flash.text.StyleSheet;
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.display.Loader
	
 
	public class CSSLoader extends EventDispatcher {
		
		public var loadFrom:String;
		
		public static const CSSLOAD_COMPLETE:String = "CSSLoadComplete";
		public var _loadFrom:String;
		public var _isVerbose:Boolean;
		public var _flashStyles:StyleSheet;
		public var _cssInfo:URLLoader;
		
		
		// Constructor
		//
		public function CSSLoader() {
			
			createURLLoader();
 
			loadCSS(loadFrom);
			
		}	// End Constructor
		
		
		public function createURLLoader():void {
			
			trace("createURLLoader has launched");
			trace("");
		
			_cssInfo = new URLLoader();
			_cssInfo.dataFormat = URLLoaderDataFormat.TEXT;
			_cssInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			_cssInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
			
		}	// end createURLLoader
		
		
		public function loadCSS(loadFrom:String):void {
		
			_loadFrom = loadFrom = "_css/flashStyles.css";
			
			trace("loadCSS at CSSLoader.as has launched");
			trace("");
		
			try {_cssInfo.load(new URLRequest(loadFrom)) } 
				catch (err:Error) {
				trace("Unable to load content:\n"+err.message);
			};
				
		}	// end loadCSS
			
		
		public function onComplete(evt:Event):StyleSheet {
			
			trace("onComplete has launched");
			trace("");
			
			_flashStyles = new StyleSheet();
			_flashStyles.parseCSS(evt.target.data);
			
			trace("Styles loaded = "+_flashStyles.styleNames);
			trace("");
			
			
			_cssInfo.removeEventListener(Event.COMPLETE, onComplete);
			_cssInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			
			return _flashStyles;
			
			dispatchEvent(new Event(_cssInfo.CSSLOAD_COMPLETE));
			
		}	// end onComplete
		
		
		private function onIOError(evt:IOErrorEvent):void {
			
			trace("onIOError has launched");
			trace("");
			
			trace("A loading error has occurred:\n", evt.text);
			
		}	// end onIOError
							
	}	// end CSSLoader
	
}	// End Package
 
 
// Master (Main) class for website application.  Instantiates CSSLoader //and attempts to use the stylesheet.  
 
package protoSite {
	
	import flash.display.*;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.net.URLRequest;
	import flash.events.*;
	import flash.geom.Point;
	import flash.net.*;
	import flash.text.*;
	import flash.text.TextField;
	import flash.text.StyleSheet;
	import com.pricelearman.loaders.FontLoader;
	import com.pricelearman.loaders.CSSLoader;
	import com.pricelearman.core.TraceDisplayList;
	import com.pricelearman.loaders.DisplayLoader;
	
	
	
	
	public class Master extends MovieClip {
		
		//public var cssLoader:CSSLoader;
		public var flashStyles:StyleSheet;
		public var _displayLoader:DisplayLoader;
		public var loadedPage:Sprite;
		//public var fontSource:String = "embeddedFonts.swf";
		//public static const FONTLOAD_COMPLETE:String = "fontLoadComplete";
		
		private var _wrapper:Sprite;
		private var _progressHolder:Sprite;
		private var _footer:Sprite;
		private var _header:Sprite;
		private var _page:Sprite;
		private var _navigation:Sprite;
		private var _dispObj:*;
		private var _indentLevel:Number;
		private var _thisStageWidth:Number = 950;
		private var _thisStageHeight:Number = 525;
		private var _progressDisplay:Sprite;
		private var _progressText:TextField;
		private var _loadedAsset:*;
		private var _testText:TextField;
		private var _testFormat:TextFormat;
		private var _fontLoader:FontLoader;
		private var _cssLoader:CSSLoader;
		//private var _context:LoaderContext;
		//private var _fontLoader:Loader
		//private var _fontURL:URLRequest;
		private var _fontSource:String;
		
		
		public function Master(fontSource:String='embeddedFonts.swf') {
			
			trace("Master Class Instantiated");
			trace("");
			
			_fontSource = fontSource;
			
			verifyChildren();
			
			init();
			
			doLoadFonts(_fontSource);
			
			//doLoadCSS();
			
			//doLoadPage();
			
		}	// End Constructor
	
	
		private function verifyChildren():void {
			
			trace("Number of Children of Stage = "+stage.numChildren);
			trace("Child of Stage is: "+stage.getChildAt(0));
			trace("");
			
			trace("stage refers to: "+stage);
			trace("this refers to: "+this);
			trace("root refers to: "+root);
			trace("");
			
			
			// only one child allowed in Document Class root
			//
			if (stage.numChildren > 1) {
				
				throw (new Error("Document Class is allowed only one child instance"));
																			   
			} else {
				
				trace("Master Class is active");
				trace("");
				
			}
			
		}	// End verifyChildren
		
	
		private function init():void {
			
			trace("init launched");
			trace();
			
			_wrapper = new Sprite();
			addChild(_wrapper);
			_wrapper.x = 0;
			_wrapper.y = 0;
			
			trace("wrapper xPos = "+_wrapper.x);
			trace("wrapper yPos = "+_wrapper.y);
			trace("");
			
			_footer = new Sprite();
			_wrapper.addChild(_footer);
			_footer.x = 0;
			_footer.y = 600;
			
			trace("footer xPos = "+_footer.x);
			trace("footer yPos = "+_footer.y);
			trace("");
			
			_header = new Sprite();
			_wrapper.addChild(_header);
			_header.x = 0;
			_header.y = 0;
			
			trace("header xPos = "+_header.x);
			trace("header yPos = "+_header.y);
			trace("");
			
			_page = new Sprite();
			_wrapper.addChild(_page);
			_page.x = 0;
			_page.y = 130;
				
			trace("page xPos = "+_page.x);
			trace("page yPos = "+_page.y);
			trace("");
			
			_navigation = new Sprite();
			_wrapper.addChild(_navigation);
			_navigation.x = 0;
			_navigation.y = 80;
			
			trace("nav xPos = "+_navigation.x);
			trace("nav yPos = "+_navigation.y);
			trace("");
	
			traceChildren(this.stage, 0);
			
	
		}	// End init
		
		
		private function doLoadFonts(fontSource:String):void {
			
			trace("loadFonts launched");
			trace("");
			
			_fontSource = fontSource;
			_fontLoader = new FontLoader(_fontSource);
			_fontLoader.addEventListener(FontLoader.FONTLOAD_COMPLETE, onFontLoadComplete);
	
		}	// end doLoadFonts
	
 
		public function onFontLoadComplete(evt:Event):void {
			
			trace("");
			trace(">>>>> onFontsComplete launched");
			trace("");
				
			doLoadCSS();
			doFontTest();
			
		}	// end onComplete
		
 
 
		private function doLoadCSS():void {
			
			trace("");
			trace(">>>>> onLoadCSS launched");
			trace("");
			
			_cssLoader = new CSSLoader();
			_cssLoader.loadCSS("_css/flashStyles.css");
			_cssLoader.addEventListener(CSSLoader.CSSLOAD_COMPLETE, onCSSComplete);
			
		}	// end loadCSS
		
		
		public function onCSSComplete(evt:Event):void {
			
			trace("");
			trace(">>>>> onCSSComplete launched");
			trace("");
			
		}	// end onComplete
		
 
		function showDisplayList():void {
		
		var displayList:TraceDisplayList = new TraceDisplayList();
		displayList.showChildren(stage, 0);
		
		}	// end showDisolayList
		
 
		
		private function padIndent(indents:int):String {
			var indent:String = "";
			for (var i:uint = 0; i < indents; i++) {
				indent += "    ";
			}
			return indent;
		}
 
		public function traceChildren(dispObj:*, indentLevel:Number):void {
			
			_dispObj = dispObj;
			_indentLevel = indentLevel;
			
			for (var i:uint = 0; i < _dispObj.numChildren; i++) {
				var obj:DisplayObject = _dispObj.getChildAt(i);
				if (obj is DisplayObjectContainer) {
					trace(padIndent(_indentLevel), obj.name, obj);
					traceChildren(obj, _indentLevel + 1);
				} else {
					trace(padIndent(_indentLevel) + obj);
				}
			}
		}
 
	
		public function doLoadPage():void {
			
			trace("doLoadPage has Launched");
			trace("");
			
			//_displayLoader = new DisplayLoader("http://www.nathanderksen.com/book/trailer.swf", _wrapper, new Point(385, 50), 0, true, true);
			_displayLoader = new DisplayLoader("home.swf", _wrapper, new Point(0, 0), 0, true, true);
			//_displayLoader.addEventListener("displayObjectLoaded", onLoadDisplayComplete, false, 0, true);
			
			_progressDisplay = _displayLoader.progressDisplay;
			addChild(_progressDisplay);
			
			var _progressText:TextField = _displayLoader.progressText;
			addChild(_progressText);
			
		}	// end doLoadDisplay
		
		
		private function onLoadDisplayComplete(evt:Event):void {
			
			trace("Remove Child Launched");
			trace("");
			
			_loadedAsset = _displayLoader.asset;
			
			removeChild(_progressDisplay);
			removeChild(_progressText);
			
		}
 
		
		// ************************************************************************************************
		// Test for embedded fonts
		// Comment out this code prior to final compile
		// ************************************************************************************************
		
		private function doFontTest():void {
			
			trace("");
			trace(">>>>> doFontTest Launched");
			trace("");
			
			_testFormat = new TextFormat();
			_testFormat.font = getFont("Sloop-ScriptOne");
			
			trace("");
			trace(">>>>> _testFormat.font = "+_testFormat.font);
			trace("");
			
			_testFormat.size = 48;
			_testFormat.color = 0x00FF00;
				
			_testText = new TextField();
			_testText.x = 100;
			_testText.y = 100;
			_testText.width = 400;
			_testText.height = 200;
			_testText.multiline = true;
			_testText.embedFonts = true;
			_testText.rotation = 45;
			//_testText.styleSheet = _cssLoader._flashStyles;
			_testText.text = "<span class='head'>If the font is embeded</span><span class='body'> this text will be rotated 45 degrees</span>";
			_testText.setTextFormat(_testFormat);
			
			_wrapper.addChild(_testText);
		
		}	// end doFontTest
 
		
	}	// End Master Class
	
}	// End Package

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of iamlfetch
iamlfetch

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial