I have a load of styles that are CSS compiled to SWF. When the application loads, it looks in a database to see which of these styles it should use and uses the following code to load it:
public function set currentStyle(val:String):void
if (val != _currentStyle)
if (val.length > 0)
StyleManager.loadStyleDeclarations("styles/" + val + ".swf");
if (_currentStyle.length > 0)
StyleManager.unloadStyleDeclarations("styles/" + _currentStyle + ".swf");
This works fine when the style exists, but there are styles in the database which don't have equivalents in the Flex application. In this case, it uses a default style.
If I try to use this code with a style that doesn't exist, I get a runtime error in the debugger version of Flash Player saying it can't find the URL. The try...catch does not catch the error, as it seems to happen within an event handler.
Is there any way of checking for the existence of the style before loading it, or catching the failure?
I can't create a style for each entry in the database, because the list changes on a regular basis.
Thanks for your help.
StyleManager.loadStyleDeclarations() returns an IEventDispatcher. Add a listener for StyleEvent.ERROR and the exception will not be thrown.
Excellent, thanks for that!
For the benefit of others, here's what I did:
var styleEvent:IEventDispatcher = StyleManager.loadStyleDeclarations("styles/" + val + ".swf");
then in finishStyleLoad, I checked for the event type and unloaded the previous style if COMPLETE.