无法弄清楚连续循环?

时间:2012-03-16 22:52:38

标签: flash actionscript-2

基本上我们正在获取观众的地理位置数据并创建文本字段以显示此数据(城市,州)。拉取数据后,测量文本以适应文本框,如果城市的长度太长,则调整文本大小以填充文本字段。

当我追踪最终文本大小的最终结果时,我不断得到(相同)跟踪结果无限输出。

// Sets text size to fit the largest it can in a specified textfield w/h
TextField.prototype.shrinkToFit = function(iMinFontSize){

    var oFormat = this.getTextFormat();
    var iSize = oFormat.size;

    /* add an extra few pixels to text height to be sure (there seem to be some inherent margins)*/
    while(this.textHeight > this._height || this.textWidth + oFormat.leftMargin + oFormat.rightMargin + 4 > this._width){

        //Decrease fontsize on TextFormat and apply it to TextField again
        oFormat.size = --iSize;
        this.setTextFormat(oFormat);

        // break the loop if we've reached a specified minimum font size
        if(iMinFontSize != null && iSize == iMinFontSize) {
            break;
        }
    }
return iSize;
};

// Geolocation
var info_xml = new XML();
info_xml.ignoreWhite = true;
info_xml.onData = function(raw:String)
   {
      if (raw == undefined)
         {
            this.onLoad(false);
         }
      else
         {
            // Normally onData would just do this:
            // this.parseXML(raw);
            // But we need to replace that with our own parsing:

            // This is the start of the fake XML string we are going to be making up.
            var parsed:String = "<maxmind>\n";        

            // Split each of the "function" lines into it's own string.  
            var lines:Array = raw.split("\n");

            // Remove the last one because that is a blank line.
            lines.pop();

            // Replace all the Regex functions from the external class since the Regex class does not exist in AS2.
            // All we're doing is chopping out two parts of each string and creating a fake XML node.
            // We cut the function name and make that into the XML node name, then we cut the returned value and set that to be the nodes "value".
            for(var i:Number = 0; i < lines.length; i++)
               {
                  parsed += "   <" + lines[i].slice(9, lines[i].indexOf("()")) + " value=\"" + lines[i].slice(lines[i].indexOf("'") + 1, lines[i].lastIndexOf("'")) + "\" />\n";
               }

            // Now parse the string into a true XML object.
            this.parseXML(parsed + "</maxmind>");

            // Back to normal loading.
            this.loaded = true;
            this.onLoad(true);
         }
   }
//If Geolocation is successful, fill the textfield with the city/state
info_xml.onLoad = function(success:Boolean)
   {
      if (success)
       {
          //convert Geolocation xml data into an array
          var props:Array = this.firstChild.childNodes;

          //Create textfield for geolocation data
          createTextField("tf",1,141,177.30,141,45.35);
          tf.border = false;

          //Expand on several lines if needed (set to true)
          tf.multiline = false;
          tf.wordWrap = false;
          tf.autoSize = false;


          tf.setNewTextFormat(new TextFormat("_sans",40));
          tf.text += props[2].attributes.value + ", " + props[3].attributes.value;
          trace("Font size shrinked to: " + tf.shrinkToFit());

       }
      else
         {
            tf.text += "There was a problem loading the remote file.\n";         
         }
   };

info_xml.load('http://www.maxmind.com/app/geoip.js');

编辑:

我最初将以下内容作为我的代码,当启动相同的跟踪时,结果只显示一次。

顶部的地理位置数据

然后收缩循环

然后这个(这是我在网站上找到的一个示例用法):

this.createTextField("tf",10,20,20,400,150);
this.tf.border = true;

//Expand on several lines here- set to all to false to test single line only
this.tf.multiline = false;
this.tf.wordWrap = false;
this.autoSize = false;

this.tf.setNewTextFormat(new TextFormat("_sans",40));
this.tf.text = "Detroit, MI";
trace("Font size shrinked to: " + this.tf.shrinkToFit()); 

虽然有效,但我需要根据从地理位置xml中提取的数据来制作文本框,因此我将其添加到地理定位脚本中。

1 个答案:

答案 0 :(得分:0)

我认为您可能通过在XML对象的加载处理程序范围内调用createTextField来创建错误。如果是这种情况,那么你有一些选择。无论哪种方式,您都将文本字段创建代码放在错误的位置,因为如果您的XML加载失败,它将尝试写入仅在加载成功时创建的文本字段。

将文本字段代码移动到可以从onLoad处理程序

调用的单独函数中
function prepareTextField(strings:Array)
{
    //Create textfield for geolocation data
    createTextField("tf",1,141,177.30,141,45.35);
    tf.border = false;

    //Expand on several lines if needed (set to true)
    tf.multiline = false;
    tf.wordWrap = false;
    tf.autoSize = false;


    tf.setNewTextFormat(new TextFormat("_sans",40));
    tf.text += strings.join(", ");
    trace("Font size shrinked to: " + tf.shrinkToFit());
}

info_xml.onLoad = function(success:Boolean)
{
    if (success) {
        //convert Geolocation xml data into an array
        var props:Array = this.firstChild.childNodes;
        prepareTextField([props[2].attributes.value,props[3].attributes.value]);
    } else {
        prepareTextField(["There was a problem loading the remote file.\n"]);         
    }
}

在创建文本字段时引用根(或您正在处理的任何阶段)

info_xml.onLoad = function(success:Boolean)
{
    //Create textfield for geolocation data
    _root.createTextField("tf",1,141,177.30,141,45.35);
    _root.tf.border = false;

    //Expand on several lines if needed (set to true)
    _root.tf.multiline = false;
    _root.tf.wordWrap = false;
    _root.tf.autoSize = false;

    _root.tf.setNewTextFormat(new TextFormat("_sans",40));

    if (success) {
        //convert Geolocation xml data into an array
        var props:Array = this.firstChild.childNodes;
        _root.tf.text += props[2].attributes.value + ", " + props[3].attributes.value;
        _root.trace("Font size shrinked to: " + tf.shrinkToFit());
    } else {
        tf.text += "There was a problem loading the remote file.\n";         
    }
}