我正在尝试从一组单词创建一个动态句子。这些词需要在一个句子中彼此相邻。我需要将这些单词放在影片剪辑中,以便我也可以拖动它们。
下面是我正在使用的代码,但我无法按照宽度顺序排列,以获得格式良好的句子。例如,在单词或不正确的空间之间会有很多额外的空间,它们会重叠。关于如何在x位置上一个接一个地获得它们的任何想法?
package com.sentances {
import flash.display.MovieClip;
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class Main_Sentances extends MovieClip {
public var Sentance: Array;
public var myText: TextField;
public var myMovie: MovieClip;
public var Words: Array = new Array("This", "is", "my", "sentance.");
public function Main_Sentances() {
// constructor code
MakeWords();
}
public function MakeWords() {
var xSpot = 0;
for (var i = 0; i < Words.length; i++) {
//trace(Words[i]);
myMovie = new MovieClip();
myMovie.name = Words[i];
addChild(myMovie);
myText = new TextField();
myText.selectable = false;
myText.border = true;
myText.defaultTextFormat = new TextFormat('Verdana', 50, 0xffffff);
myText.text = Words[i];
//trace(myText.name);
myText.addEventListener(Event.ADDED_TO_STAGE, handleChng);
myMovie.addChild(myText);
function handleChng(e: Event): void {
trace("childMC ADDED_TO_STAGE");
trace(myText.textWidth);
if(i==0)
{
myMovie.x = 0;
myMovie.width = myText.textWidth;
myMovie.height = myText.textHeight;
xSpot = myText.textWidth;
}
else
{
myMovie.width = myText.textWidth;
myMovie.height = myText.textHeight;
xSpot = xSpot + myText.textWidth;
myMovie.x = xSpot;
}
}
}
}
// End Package
}
}
答案 0 :(得分:2)
首先,您需要启用自动调整大小:
textField.autoSize = TextFieldAutoSize.LEFT;
然后,如果要读取生成的文本大小,可以使用:
var width:Number = textField.textWidth;
var height:Number = textField.textHeight;