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