0

以为这会奏效...

var bFormat:TextFormat, bStartText:TextField, bQuitText:Textfield;

bFormat = getFormat();

bStartText = getText("Start");
bStartText.defaultTextFormat = bFormat;

bQuitText = getText("Quit");
bQuitText.defaultTextFormat = bFormat;

stage.addChild(bStarText);
stage.addChild(bQuitText);

function getFormat():TextFormat {
  var bFormat:TextFormat = new TextFormat();
  bFormat.font = "Arial";
  bFormat.color = 0X000000;
  bFormat.size = 28;
  bFormat.align = "center";
  return bFormat;
}

function getText(sText):TextField {
  var bText:TextField = new TextField();
  bText.text = sText;
  bText.x = -4;
  bText.y = 4;
  return bText;
}

两个文本字段都显示在舞台上,但是我没有得到 getFormat() 中指定的任何格式。我已将 getFormat() 中的代码放在主代码中(而不是作为它自己的函数),它工作正常。我是否错误地通过了它?

4

1 回答 1

3

defaultTextFormat更改之前需要设置text。由于您text在 getText 中设置 TextField ,因此它没有任何效果。

试试这个 :

var bFormat:TextFormat, bStartText:TextField, bQuitText:Textfield;

bFormat = getFormat();

bStartText = getText("Start",bFormat);

bQuitText = getText("Quit",bFormat);

stage.addChild(bStarText);
stage.addChild(bQuitText);

function getFormat():TextFormat {
  var bFormat:TextFormat = new TextFormat();
  bFormat.font = "Arial";
  bFormat.color = 0X000000;
  bFormat.size = 28;
  bFormat.align = "center";
  return bFormat;
}

function getText(sText:String, tf:TextFormat):TextField {
  var bText:TextField = new TextField();
  bText.defaultTextFormat = tf;
  bText.text = sText;
  bText.x = -4;
  bText.y = 4;
  return bText;
}

如果您想更改已设置的文本字段的格式,您可以setTextFormatTextField课堂上使用。

于 2013-03-18T20:24:09.407 回答