2009-06-19 172 views
0

我遇到了getCharBoundaries的一個奇怪問題,我無法弄清楚從函數返回的座標是什麼座標空間。我試過的是什麼,我無法讓它匹配什麼我期望。 所以我做了一個新項目,並添加了簡單的代碼來突出顯示文本字段中的最後一個字符,並突然它運行正常。然後我試圖將導致我出現問題的TextField複製到新項目中。現在,同樣奇怪的偏移量在x軸上出現了50px。其他一切都是現貨。 因此,在比較兩個TextFields之後,我簡直看不到其屬性或轉換的差異。getCharBoundaries上的神祕座標偏移量

所以我希望現在有人可能會影響getCharBoundaries返回的座標。

我正在使用Flash CS4。

回答

2

我剛剛有完全相同的問題,並認爲我會提供我的發現是幫助。在此線程的幫助下,我試圖找到所有關於我使用的textfield的「默認」。我發現當我將TextFormatAlign(或IDE中的'align')和TextFieldAutoSize屬性切換爲'LEFT'而不是'CENTER'時,它解決了這個問題。

也許在遊戲中有點晚,但值得了解任何人遇到同樣的問題。這是我能找到的唯一提供正確標誌的線索......

+0

這樣的聲音可能是答案。如果有人遇到這個問題,我會給你答案的。 – 2011-04-27 15:44:00

0

對我的作品(TM)(Flex Builder中AS3項目):

[Embed(systemFont="Segoe UI", fontWeight="bold", fontName="emb", 
    mimeType="application/x-font")] 
private var EmbeddedFont:Class; 

public function ScratchAs3() 
{ 
    stage.scaleMode = 'noScale'; 
    stage.align = 'tl'; 

    var m:Matrix = new Matrix(.8, .1, -.1, 1.1, 26, 78); 

    var t:TextField = new TextField(); 
    t.autoSize = 'left'; 
    t.wordWrap = false; 
    t.embedFonts = true; 
    t.defaultTextFormat = new TextFormat("emb", 100, 0, true); 
    t.transform.matrix = m; 
    t.text = "TEST STRING."; 
    addChild(t); 

    var r:Rectangle = t.getCharBoundaries(8); 
    var tl:Point = m.transformPoint(r.topLeft); 
    var tr:Point = m.transformPoint(new Point(r.right, r.top)); 
    var bl:Point = m.transformPoint(new Point(r.left, r.bottom)); 
    var br:Point = m.transformPoint(r.bottomRight); 
    graphics.beginFill(0xFF, .6); 
    graphics.moveTo(tl.x, tl.y); 
    graphics.lineTo(tr.x, tr.y); 
    graphics.lineTo(br.x, br.y); 
    graphics.lineTo(bl.x, bl.y); 
    graphics.lineTo(tl.x, tl.y); 
} 

要字面上回答你的問題,它返回的座標TextField的座標系中,而不是它的父,它是由的DisplayObject影響.transform.matrix,它是.x,.y,.scaleX,.scaleY,.width,.height和.rotation屬性的後盾。

+0

我得到了用新的TextField工作,我只想知道什麼會影響返回的x座標。 我已經檢查過縮放,並且確實會影響座標,但這不是原因。 – 2009-06-19 09:19:07

2

那麼getCharBoundaries返回文本域座標系中的邊界。原始位置是文本字段的頂部角落。
getCharBoundaries不考慮滾動。你需要檢查它的父(textarea)是否有滾動條,如果是這樣的話重定位。一個快速的方法是使用localtoglobal和globaltolocal。使用第一個從文本框座標系轉換到應用程序座標系,然後使用第二個從應用程序座標系轉換爲textarea文本框的父級座標系。我微調我的方法來獲得字符邊界我今天將發佈在我的博客 http://flexbuzz.blogspot.com/

+0

好的建議,但它不是問題。但就像我寫的那樣,它是TextField上的一個屬性。從來沒有找到哪個。 – 2009-10-06 21:29:49

0

什麼都這是解決方案是簡單的增加一個新的文本字段,從來沒有發現什麼財產搞砸一切。

0

在大多數情況下,第一個答案是正確的。但是,如果您的字段已成爲另一個影片剪輯的父元素,它可能仍會返回錯誤的y座標。試試這個代碼:

//if this doesn't work: 

myTextFormat = new TextFormat(); 
myTextFormat.align = TextFormatAlign.LEFT; 
myFieldsParent.myField.autoSize = TextFieldAutoSize.LEFT; 
myFieldsParent.myField.setTextFormat(myTextFormat); 



//try this: 
var x = myFieldsParent.myField.getCharBoundaries(o).x; 
var y = myFieldsParent.myField.getCharBoundaries(o).y; 

var myPoint:Point = new Point(myField.getCharBoundaries(o).x,myField.getCharBoundaries(o).y); 

var pt:Point = new Point(myFieldsParent.myField.getCharBoundaries(o).x, myFieldsParent.myField.getCharBoundaries(o).y); 
pt = myFieldsParent.myField.localToGlobal(pt); 

//pt is the variable containing the coordinates of the char in the stage's coordinate space. You may still need to offset it with a fixed value but it should be constant. 

的,所以我很抱歉,如果我想的東西我已經適應了從代碼這個例子中嵌入到項目中,我沒有測試此代碼...