views:

13

answers:

1

I have a sprite that contains a textfield. Then, I want to create a second sprite ("containerSprite") which is the same size as the textfield. That's not difficult and works fine.

Now I want to add a third sprite ("innerSprite") to containerSprite. I need this third sprite because I'm going to use it for drag and drop purposes. I add a textfield to it, and I want the textfield to be the same width as both containerSprite and innerSprite. Depending on how much text is in the textfield, I need innerSprite to resize its height accordingly.

This should be simple. It isn't working. What am I doing wrong?

Thanks,

David

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);
    }        

}
}
A: 

Ultimately, Sprites adjust their size to match the content placed within them. If you are resizing a sprite that has DisplayObject children, you're going to end up scaling those children instead of just resizing the container.

If the TextField's width is set to the width you want the InnerSprite to be, and you add the TextField to that Sprite, your Sprite will become that width (as long as the TextField is at 0, 0). Similarly, as the TextField's vertical size increases/decreases, so will the Sprite that contains it.

Tegeril
So I just sized innerText to the original textfield (tf) and then let the containing sprite take care of itself. That did it. Thanks.
David