Good Morning,
I am querying开发者_如何转开发 the weather.com XML web service to obtain current temp, feels like temp and current condition icon. I have the first two values going to separate text fields on stage, and that's working great. However, I'm now trying to implement a switch statement such that the current condition icon value in the XML is read by my script and the corresponding PNG becomes the UILoader source value.
First and foremost, is UILoader the best component for use in displaying a PNG on stage?
Here's my code thus far:
var src: UILoader.source=uicondicon.source;
switch(src){
case "0 PNG":
src=trace(resultXML.cc.icon)=0;
break;
case "11 PNG":
src=trace(resultXML.cc.icon)=11;
break;
default src=trace(resultXML.cc.icon)=0:
}
When I compile the above code, I receive error 1084 expecting colong before src in the final line of code (my default assignment).
Given that there are 47 different icons that may need to be displayed, is this the best way to implement this functionality?
Finally, how do I make the UILoader component display only during a certain set of frames in the application?
Thanks much!!
1) The error you're getting is probably because the last line in your switch statement ends with a colon instead of a semicolon - "default src=trace(resultXML.cc.icon)=0:"
2) I've never actually used UILoader - I usually just use Loader:
var loader:Loader = new Loader;
var images:Array = [];
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
loader.load(new URLRequest("image url here.jpg"));
protected function completeHandler(e:Event):void {
// you can pull out the loader content and cast it as a bitmap...
images.push(loader.content as Bitmap);
// or you can simply add the loader to the stage
addChild(loader);
clearListeners();
}
protected function errorHandler(e:IOErrorEvent):void {
trace("Error loading image! Here's the error:\n" + e);
clearListeners();
}
protected function progressHandler(e:ProgressEvent):void {
trace("Load is " + (100 * e.bytesLoaded / e.byteTotal) + " percent complete...");
}
protected function clearListeners():void {
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
}
3) If you are loading multiple images at once, you should know that the order in which your loads are called has NOTHING TO DO with the order in which they're finished. So you can't assume that because you load image A then image B that image A will finish first. So you have to do some logic to keep track of your loaders - for instance, you could have a setup like this:
var urls:Array = [this array has all of your urls in it];
var loaders:Array = [];
var images:Array = [];
protected function initLoaders():void {
for (var i:int = 0; i < urls.length; i++) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
// you'll want to add your other handlers here too for IOErrorEvent and Progress.
loader.load(new URLRequest(urls[i]));
loaders[i] = loader;
}
}
protected function completeHandler(e:Event):void {
var loaderIndex:int = loaders.indexOf(e.currentTarget); // this tells you which loader finished
images[loaderIndex] = e.currentTarget.content as Bitmap; // I think this will work, you may have to play with Array syntax to make sure you're not adding tons of empty items and screwing up your order.
if (images.length == urls.length) {
trace("All images loaded!");
}
}
4) Finally, don't use frames. You want to write your code in an external class. If it has to be tied to a given frame, use visible = true or visible = false depending on the current frame. But all of this can and should be handled in code, since this is really business logic and shouldn't be tightly coupled to your view (ie the visual display that can be tied to frames).
Hope that helps!
精彩评论