开发者

Help with this AS3 code

开发者 https://www.devze.com 2023-02-02 23:11 出处:网络
Hello im very new at AS3 so im a litle confused trying to get a simple modification to this code: import flash.display.Loader;

Hello im very new at AS3 so im a litle confused trying to get a simple modification to this code:

import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.text.StyleSheet;


var fonts:Array /* of String */ = ["fonts/DroidSerif.swf","fonts/Lobster.swf"];
var fontsLoaded:int = 0;
var textStyleSheet:StyleSheet;


    loadFonts();


function loadFonts():void
{
    for each (var fontURL:String in fonts) 
        {
            var fontLoader:Loader = new Loader();
            fontLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFontLoaded);
            fontLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onFontLoadError);
            fontLoader.load(new URLRequest(fontURL));
        }
}

function onFontLoadError(event:IOErrorEvent):void
{
    trace("ERROR: Could not find font "+event.target.loaderURL);
}

function onFontLoaded(event:Event):void
{
    fontsLoaded++;
    if (fontsLoaded == fonts.length) {
        onFontsLoadComplete();
    }}
function onFontsLoadComplete():void
        {
            trace("FONTS");
            trace(fonts.length + " fonts have been loaded");
            loadCSS();
        }

        function loadCSS():void
        {
            var loader:URLLoader = new URLLoader(); 
            loader.addEventListener(Event.COMPLETE, onCSSLoadComplete); 
            loader.load(new URLRequest("css/styles.css"));
        }

        function onCSSLoadComplete(event:E开发者_StackOverflow中文版vent):void
        {
            trace("CSS");
            textStyleSheet = new StyleSheet();
            textStyleSheet.parseCSS(event.target.data);
            loadXML();
        }

        function loadXML():void
        {
            var loader:URLLoader = new URLLoader(); 
            loader.addEventListener(Event.COMPLETE, onXMLLoadComplete); 
            loader.load(new URLRequest("xml/content.xml"));
        }

        function onXMLLoadComplete(event:Event):void
        {
            // get xml from event target data
            var xml:XML = XML(event.target.data);
            // convert the xml to a string
            var xmlString:String = xml;
            // send xml string to the displayHTML method
            trace("CARGO XML");
            displayHTML(xmlString);
        }

        function displayHTML(htmlText:String):void
        {
            var htmlTextBlock:HTMLTextBlock = new HTMLTextBlock();
            htmlTextBlock.blockWidth = 720;
            htmlTextBlock.textStyleSheet = textStyleSheet;
            htmlTextBlock.setHTML(htmlText);
            htmlTextBlock.x = 0;
            htmlTextBlock.y = 0;
            addChild(htmlTextBlock);
            trace("AJA");
        }

Now, lets concentrate in the last function, displayHTML(). As you can see and i barely understand this function creates a textfield and fill it with the html content. I dont want to create a new textfield, i already have one dinamic textfield on the movie, with the html option selected. The problem is that there is something that im doing wrong, i use this simple line: mydinamictext.htmlText = htmlText. But the result is a NON css formated text. Here is a screen capture, the one of top is with the textfield created in code, and the bottom one with my textfield:

Help with this AS3 code

So, how can i apply the css and the fonts to my dynamic textfield and not create a new one. THANKS ALOT!


You would need to apply the CSS styles to the textfield on the stage. You were originally setting the CSS to the dynamically created text box within the displayHTML method.

As mentioned above, you will need to use the following:

mydinamictext.textStyleSheet = textStyleSheet;

You should be able to just use that line of code right above where you had mydinamictext.htmlText = htmlText

Best of luck.

0

精彩评论

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

关注公众号