开发者

Why does Font.registerFont throw an error the second time a swf is loaded?

I've found an issue (in flash cs5) when using TLFTextfields and fonts shared at runtime, and I wondered if anyone has a solution.

Here's the scenario.

  • Fonts.swf: library for fonts (runtime shared DF4)
  • Popup.swf: popup with a TLFTextfield (with text in it) using a font from Fonts.swf
  • Main.swf : the main swf with a button that loads and unloads Popup.swf (using Loader or SafeLoader give the same results)

Nice and simple,

Run main.swf, click button and the popup appears with the text. Click the button again to remove the popup. All well and good now click the button again and I get the following error.

ArgumentError: Error #1508: The value specified for argument font is invalid.
at flash.text::Font$/registerFont()
at Popup_fla::MainTimeline()[Popup_fla.MainTimeline::MainTimeline:12]

I'm presuming it's happening because the font is already registered (checked when clicking before the load).

Does anyone know how to get past this?

If you are wondering here's my Main.as

package
{
    import fl.controls.Button;

    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MouseEvent;
    import flash.events.UncaughtErrorEvent;
    import flash.net.URLRequest;
    import flash.text.Font;

    public class Main extends Sprite
    {
        public var testPopupBtn:Button;
        protected var loader:Loader;


        public function Main()
        {
            trace("Main.Main()");
            testPopupBtn.label = "open";
            testPopupBtn.addEventListener(MouseEvent.CLICK, testClickHandler);

        }

        protected function testClickHandler(event:MouseEvent):void
        {
            trace("Main.testClickHandler(event)");
            if(loader)
            {
                testPopupBtn.label = "open";
                this.removeChild(loader);
                //loader.unloadAndStop();
                loader = null;
            }else{
                testPopupBtn.label = "close";
                trace("Registered Fonts -->");
                var fonts:Array =  Font.enumerateFonts(false);
                for each (var font:Font in fonts) {
                    trace("\t开发者_开发知识库",font.fontName, font.fontStyle, font.fontType);

                }
                trace("<--");

                loader = new Loader();
                loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
                this.addChild(loader);
                try{
                    loader.load(new URLRequest("Popup.swf"));
                }catch(e:*){
                    trace(e);
                }

            }
        }

        private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
        {
            trace("Main.uncaughtErrorHandler(event)", event);
        }

    }
}


To get rid of this problem, you could add an empty and/or invisible TLFTextField, embedding the font you are loading, to the stage in frame 1 of your Main.fla . In the library, it can link to the same Font.swf you used before, but the font must be referenced in the Main MovieClip. Remove the font from the popup.fla library and ignore the export warning.

Another way would be to load (not dynamically link) the Font.swf into your Main file, and register the font by manually calling Font.registerFont (myFont).

Either way, embedding the font into the popup causes your error, but loading it without embedded fonts, and registering those in the Main.fla will do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜