2010-10-07 26 views
1

我有一個包含文本字段的精靈。然後,我想創建一個與文本字段大小相同的第二個精靈(「containerSprite」)。這並不困難,工作正常。TextField不出現在Sprite中

現在我想添加第三個精靈(「innerSprite」),以containerSprite。我需要這第三個精靈,因爲我打算將它用於拖放目的。我添加了一個文本框,並且我希望textfield的寬度與containerSprite和innerSprite的寬度相同。根據文本字段中的文本數量,我需要innerSprite相應地調整其高度。

這應該很簡單。它不工作。我究竟做錯了什麼?

感謝,

大衛

package 
{ 
import flash.display.Sprite; 
import flash.text.TextField; 
import flash.text.TextFieldAutoSize; 
import flash.text.TextFieldType; 

public class SpriteAndTextField extends Sprite 
{ 
    private var innerText:TextField; 
    private var innerSprite:Sprite; 
    private var containerSprite:Sprite 

    public function SpriteAndTextField() 
    { 
     var tf:TextField = new TextField(); 
     tf.type = TextFieldType.INPUT; 
     tf.width = 300; 
     tf.height = 200; 
     tf.x = 0; 
     tf.y = 100; 
     tf.selectable = true; 
     tf.border = true; 
     tf.background = true; 
     tf.backgroundColor = 0xCCCCCC; 
     tf.multiline = true; 
     tf.wordWrap = true; 
     tf.text = "Some text here." 
     addChild(tf); 

     //containerSprite 

     containerSprite = new Sprite(); 
     containerSprite.x = tf.x + tf.width + 10; 
     containerSprite.y = tf.y; 
     containerSprite.graphics.beginFill(0xFFFFFF,1) 
     containerSprite.graphics.drawRect(0, 0, tf.width, tf.height); 
     containerSprite.graphics.endFill(); 
     containerSprite.name = "containerSprite"; 
     addChild(containerSprite); 

     //now add another sprite, with a textfield. We want the sprite and the textfield it contains to be the same width 
     //as the containerSprite. The innerSprite's height should be determined by however much text is in its child 
     //textfield 
     innerSprite = new Sprite(); 
     //not setting x and y, so it should appear at 0,0 of its parent containerSprite 
     innerSprite.width = containerSprite.width; 
     containerSprite.addChild(innerSprite); 

     //add textfield to inner sprite 
     innerText = new TextField(); 
     innerText.selectable = true; 
     innerText.border = true; 
     innerText.background = true; 
     innerText.backgroundColor = 0xFFFF00; 
     innerText.multiline = true; 
     innerText.wordWrap = true; 
     innerText.text = "The TextField class is used to create display objects for text display and input. All dynamic and input text fields in a SWF file are instances of the TextField class." 
     innerSprite.addChild(innerText); 
    }   

} 
} 

回答

0

最終,精靈調整其大小,以放置在他們的內容相匹配。如果您調整具有DisplayObject子項的精靈,則最終將縮放這些子項,而不是調整容器大小。

如果將TextField的寬度設置爲您希望InnerSprite的寬度,並且將TextField添加到該Sprite,則Sprite將變爲該寬度(只要TextField爲0,0)。同樣,當TextField的垂直大小增加/減少時,包含它的Sprite也會如此。

+0

所以,我只是將innerText設置爲原始文本字段(tf),然後讓包含的精靈自行處理。那樣做了。謝謝。 – David 2010-10-07 19:00:00

相關問題