2012-03-16 57 views
0

基本上发生了什么是我正在获取查看器的地理位置数据并创建显示此数据(城市,州)的文本字段。数据拉出后,文本被测量以适应文本框,并且如果城市的长度太长,则调整文本的大小以填充文本框。无法找出连续循环?

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

// 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中提取的数据制作的,所以我将它添加到了地理定位脚本中。

+0

你在追踪最终文字大小的最终结果? (它不在代码中)。你确定它不只是坐在一个OnFrame监听器中吗? (它只是每次播放头永远都会触发) – 2012-03-16 23:01:42

+0

trace(“Font size shrinked to:”+ tf.shrinkToFit()); 它在if语句中将golocation xml数据转换为数组,然后在它创建文本框 – 2012-03-16 23:04:09

+1

之后,这与shrinky循环无关。这是在缩小循环之外。为什么这会被反复调用?你一遍又一遍地加载文件吗?我怀疑你坐在某个刚刚播放的帧中(这是帧的作用),因此,你每秒看到它x次(与电影运行速度一样快)。确保这些都是在加载时完成的,除非您需要播放此MC,否则请在其中调用stop()。 – 2012-03-17 00:37:53

回答

0

我认为你可能通过在XML对象的负载处理程序范围内调用createTextField来创建错误。如果是这种情况,那么你有一些选择。无论哪种方式,您都将textfield创建代码放在错误的地方,因为如果您的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"]);   
    } 
} 

指根(或任何阶段,你正在处理)为您创建调用textfield

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";   
    } 
} 
+0

您的第二个代码中的_root.trace不会输出任何内容。如果我保留代码并从跟踪中删除_root,则问题仍然存在。大声笑,但我把停止();在代码的最后,它解决了我的问题。 – 2012-03-19 15:22:56