TextField没有出现在Sprite中

时间:2010-10-07 18:03:50

标签: actionscript-3

我有一个包含文本字段的精灵。然后,我想创建一个与文本字段大小相同的第二个精灵(“containerSprite”)。这并不难,而且工作正常。

现在我想在containerSprite中添加第三个精灵(“innerSprite”)。我需要第三个精灵,因为我将把它用于拖放目的。我向它添加了一个文本字段,我希望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);
    }        

}
}

1 个答案:

答案 0 :(得分:0)

最终,Sprites调整其大小以匹配其中的内容。如果要调整具有DisplayObject子级的精灵的大小,则最终会缩放这些子级而不是仅调整容器大小。

如果TextField的宽度设置为您想要InnerSprite的宽度,并且将TextField添加到该Sprite,则您的Sprite将变为该宽度(只要TextField为0,0)。同样,当TextField的垂直大小增加/减少时,包含它的Sprite也会增加/减少。