Advertisement

07.08.2008 at 06:50PM PDT, ID: 23548885
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Help showing progress in AS3 preloader

Asked by dlearman1 in Scripting Languages, ActionScript

Tags:

Having barely scratched the surface of AS2, I decided to just jump into AS3.  This preloader should load any DisplayObject and show a progress info feedback to the viewer.  It does load the asset correctly.  But I get several 2007 & 1009 errors relating to the progress info function.  I'm hoping someone can tell me what I'm doing (or not doing) to cause these errorsStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
package com.pricelearman.loaders {
	
		
	import flash.display.*;
    import flash.events.*;
    import flash.net.URLRequest;
	import flash.geom.*;
	import flash.text.*;
 
	public class PreLoadDisplayObject extends Sprite {
		
		public var loadFrom:String;
		public var loadInto:DisplayObjectContainer;
		public var loadLevel:Number;
		public var isMonitored:Boolean;
		public var isVerbose:Boolean;
		public var origin:Point;
		
		private var _loadFrom:String;
		private var _loadInto:DisplayObjectContainer;
		private var _origin:Point;
		private var _loadLevel:Number;
		private var _loader:Loader;
		private var _loaderInfo:LoaderInfo;
		
		private var _isVerbose:Boolean = false;
		private var _dispObj:DisplayObject;
		private var _indentLevel:Number;
		
		private var _isMonitored:Boolean = false;
		private var _loadProgressText:String = "";
		private var _progressLabel:TextField;
		private var _progressFormat:TextFormat;
		private var _bytesLoaded:Number = 0;
		private var _bytesTotal:Number = 0;
		private var _barWidth:Number;
		
		
		
		// Constructor
		//
		public function PreLoadDisplayObject(loadFrom:String,
											 loadInto:DisplayObjectContainer,
											 origin:Point,
											 loadLevel:Number,
											 isMonitored:Boolean,
											 isVerbose:Boolean
											 ) {
			
			createLoader();
                                                      
      		if (isMonitored) createProgressInfo();
 
			loadDisplayObject(loadFrom, loadInto, origin, loadLevel, isMonitored, isVerbose);
			
		}	// End Constructor
		
		
		private function createLoader():void {
		
			_loader = new Loader();
			_loaderInfo = _loader.contentLoaderInfo;		// shortcut variable
		
			_loaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			_loaderInfo.addEventListener(Event.OPEN, onOpen, false, 0, true);
			_loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress, false, 0, true);
			_loaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatusEvent, false, 0, true);
			_loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true);
			_loaderInfo.addEventListener(Event.INIT, onInit, false, 0, true);
			_loaderInfo.addEventListener(Event.UNLOAD, onUnloadContent, false, 0, true);
			
		}	// end createLoader
		
		
		private function createProgressInfo():void {
			
			_progressFormat = new TextFormat();
			_progressFormat.font = "sans";
			_progressFormat.size = 14;
			_progressFormat.color = 0x999999;
			
			_progressLabel = new TextField();
			_progressLabel.autoSize =TextFieldAutoSize.LEFT;
			_progressLabel.border = true;
			_progressLabel.background = true;
			_progressLabel.selectable = false;
			_progressLabel.text = "Loading ... ";
			_progressLabel.setTextFormat(_progressFormat);
			
		}	// end createProgressInfo
		
		
		private function loadDisplayObject(loadFrom:String, 
										   loadInto:DisplayObjectContainer,
										   origin:Point,
										   loadLevel:Number,
										   isVerbose:Boolean,
										   isMonitored
										   ):void {
			
			_loadFrom = loadFrom;
			_loadInto = loadInto;
			_origin = origin;
			_loadLevel = loadLevel;
			_isMonitored = isMonitored;
			_isVerbose = isVerbose;
			
			try {
				_loader.load(new URLRequest(_loadFrom));
				} catch (err:Error) {
				trace("Unable to load content:\n"+err.message);
				}
				
			// test if _progressLabel is already a decendent of the
			// loader object, if not add it
			//
			if (!contains(_progressLabel)) {
				addChild(_progressLabel);
			}
			
			_progressLabel.x = (stage.width/2) - (_progressLabel.width/2);
			_progressLabel.y = (stage.height/2) - (_progressLabel.height/2);
			
		}	// end loadDisplayObject
			
		
		
		private function onOpen(evt:Event):void {
			
			if(isVerbose) {
				trace("Loading has begun");
			}
 
			_progressLabel.visible = true;
			_loadInto.visible = false;
			evt.target.content.stop();
			
		}	// end onOpen
		
		
		private function onProgress(evt:ProgressEvent):void {
			
			var _percentLoaded:int = Math.round((evt.bytesLoaded / evt.bytesTotal) * 100);
			
			showProgress(_percentLoaded, _progressLabel);
			
		}	// end onProgress
		
		
		private function onInit(evt:Event):void {
			
			trace("Loaded Asset Properties are now accessible");
			
			if(_isVerbose) {
				trace("Content Initialized Properties");
				trace("  url: ", evt.target.url);
				trace("  Same Domain: ", evt.target.sameDomain);
				trace("");
				
				if(evt.target.contentType == "application/x-shockwave-flash") {
					trace("  SWF Version: ", evt.target.swfVersion);
					trace("  ActionScript Version: ", evt.target.actionScriptVersion);
					trace("  Frame Rate: ", evt.target.frameRate);
					trace("");
				}
			}
			
			_loaderInfo.removeEventListener(Event.INIT, onInit);
			
		}	// end onInit
				
				
		private function onComplete(evt:Event):void {
			
			_loaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			_loaderInfo.removeEventListener(Event.OPEN, onOpen);
			_loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
			_loaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatusEvent);
			_loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
			
			// variable is typed as * because content property returns an Object of type
			// DisplayObject. Calling a method on a DisplayObject will throw an error, if the
			// method isn't defined in the DisplayObject Class.
			//
			var loadedAsset:* = evt.target.content;
		
			_loadInto.addChildAt(loadedAsset, _loadLevel);
			loadedAsset.dispatchEvent(new Event("displayObjectLoaded"));
			
			loadedAsset.x = _origin.x;
			loadedAsset.y = _origin.y;
			
			removeChild(_progressLabel);
			//_loadInto.visible = true;
			//loadedAsset.play();
			
			trace("Loading is Complete");
			trace("");
/*			
			if(verbose) {
				
				traceChildren(stage, 0);
				
			}
*/		
		}	// end onComplete
		
		
		private function showProgress(_percentLoaded, _progressHolder):void {
			
			_barWidth = _percentLoaded;
			_progressLabel.text = "Loading ...  "+String(Math.ceil(_percentLoaded))+" %";
			
	
			_progressHolder.clear();
			
			// draw border
			//
			_progressHolder.moveTo(0, 0);
			_progressHolder.lineStyle(1, 0x000000, 100);
			_progressHolder.beginFill(0xCCCCCC, 80);
			_progressHolder.lineTo(100, 0);
			_progressHolder.lineTo(100, 20);
			_progressHolder.lineTo(0, 20);
			_progressHolder.lineTo(0, 0);
			_progressHolder.endFill();
			
			// draw bar
			//
			_progressHolder.moveTo(0,  9);
			_progressHolder.lineStyle(2, 0xFF0000, 100);
			_progressHolder.lineTo(_barWidth, 9);
			_progressHolder.endFill();
			
		}	// end showProgress
		
		
		private function onHTTPStatusEvent(evt:HTTPStatusEvent):void {
			
			if(_isVerbose) {
				trace("HTTP Status Code: "+evt.status);
			}
			
			//if(evt.status == "URLNotFound") {
				//_errorMessage.text = "Error: Media Not Found /n Please report this error to the Webmaster";
				//_progressHolder.clear();
				
			//} else if(evt.status == "LoadNeverCompleted") {
				//_errorMessage.text = "Error: Transfer Failed /n Please report this error to the Webmaster";
				//_progressHolder.clear();
			//}
			
		}	// end onHTTPStatus
		
		
		private function onIOError(evt:IOErrorEvent):void {
			
			if(_isVerbose) {
				trace("A loading error has occurred:\n", evt.text);
			}
			
			_progressLabel.text = "Error: A Loading Error Has Occurred /n Please report this error to the Webmaster";
			//_progressHolder.clear();
			
		}	// end onIOError
		
		
		private function onUnloadContent(evt:Event):void {
			
			_loaderInfo.removeEventListener(Event.UNLOAD, onUnloadContent);
			
			if(_isVerbose) {
				trace("Unload handler:\n", evt);
			}
			
		}	// end onUnloadContent
		
 
		private function padIndent(indents:int):String {
			var indent:String = "";
			for (var i:uint = 0; i < indents; i++) {
				indent += "    ";
			}
			return indent;
		}		
			
	}	// end PreLoader
	
}	// End Package
Attachments:
 
jpg of woman laying on leaves
jpg of woman laying on leaves
 
 
Loading Advertisement...
 
[+][-]07.09.2008 at 05:41AM PDT, ID: 21962877

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Scripting Languages, ActionScript
Tags: Flash AS3 preload
Sign Up Now!
Solution Provided By: blue-genie
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.09.2008 at 09:39AM PDT, ID: 21965627

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.09.2008 at 10:28AM PDT, ID: 21966107

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]07.09.2008 at 11:47AM PDT, ID: 21966943

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_EXPERT_20070906