Search code examples
cssapache-flexurlactionscript

How can I test a SWF URL before Loading Styles From if (or catching the error)?


I am trying to use the following code to load styles from an external SWF, but I keep getting an ActionScript error when the URL is invalid:

Error: Unable to load style(Error #2036: Load Never Completed. URL: http://localhost/css/styles.swf): ../css/styles.swf.
at <anonymous>()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManagerImpl.as:858] 


private function loadStyles(): void
{
    try
    {
        var styleEvent:IEventDispatcher = 
            StyleManager.loadStyleDeclarations("../styles.swf");

        styleEvent.addEventListener(StyleEvent.COMPLETE, 
                                                    loadStyle_completeHandler);

        styleEvent.addEventListener(StyleEvent.ERROR, 
                                                    loadStyle_errorHandler);
    }
    catch (error:Error)
    {
        useDefault();
    }
}

private function loadStyle_completeHandler(event:StyleEvent): void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_completeHandler);

    goToNextStep();
}

private function loadStyle_errorHandler(event:StyleEvent): void
{
    IEventDispatcher(event.currentTarget).removeEventListener(
        event.type, loadStyle_errorHandler);

    useDefault();
}

I basically want to go ahead an use the default styles w/o the user seeing the error if this file can't be loaded - but I can't seem to find any way to do this.


Solution

  • I debugged the source, and found that the ERROR Event is being triggered twice. So, I simply set a flag the first time the ERROR event handler is triggered, and check that flag for a value of true before continuing:

    private var isErrorTriggered:Boolean; // Default is false
    
    private function loadStyle_completeHandler(event:StyleEvent):void
    {
        IEventDispatcher(event.currentTarget).removeEventListener(
            event.type, loadStyle_completeHandler);
    
        goToNextStep();
    }
    
    private function loadStyle_errorHandler(event:StyleEvent):void
    {
        if (isErrorTriggered)
            return;
    
        isErrorTriggered = true;
    
        useDefault();
    }