TextFormat最初未按预期工作

时间:2013-05-12 20:32:23

标签: actionscript-3 flash flash-cs6

我的一个文字项目(游戏的分数计数器)不符合我所设置的格式,当所述文字标签的值为0时,但只要它更新为1或更高,文本格式正确。当分数为0时,文本为黑色并采用Times New Roman字体,但在更新为1或更高时更改颜色,字体和字体大小。有问题的项目是scoreText.text。这是与scoreText标签相关的代码片段:

public class Main extends MovieClip {

static var scoreText:TextField = new TextField();

public var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);

public function Main()
{
        addChild(gameLayer);
        addChild(backgroundLayer);
        addChild(interfaceLayer);
        interfaceLayer.addChild(mainMenu);
        soundControl = intro.play(0, 100);
        mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}

public function startGame(e:Event)
{
    scoreText = new TextField();
    scoreText.text = String(0);
    interfaceLayer.addChild(scoreText);
    scoreText.x = 75;
    scoreText.y = 0;
    scoreText.selectable = false;

    scoreText.setTextFormat(scoreFormat);

    resetScore();
}

static function updateScore(points)
{
    score += points;
    scoreText.text = String(score);
    var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
    scoreHeader.setTextFormat(scoreFormat);
    scoreText.setTextFormat(scoreFormat);
}

static function resetScore()
{
    score = 0;
    scoreText.text = String(score);
}

如果有人能帮助查明我出错的地方,我将不胜感激。

由于

2 个答案:

答案 0 :(得分:0)

试试这个

public class Main extends MovieClip {

static var scoreText:TextField = new TextField();
static var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);


public function Main()
{
        addChild(gameLayer);
        addChild(backgroundLayer);
        addChild(interfaceLayer);
        interfaceLayer.addChild(mainMenu);
        soundControl = intro.play(0, 100);
        mainMenu.playBtn.addEventListener(MouseEvent.CLICK, startGame);
}

public function startGame(e:Event)
{
    scoreText.setTextFormat(scoreFormat);
    scoreText.text = String(0);
    interfaceLayer.addChild(scoreText);
    scoreText.x = 75;
    scoreText.y = 0;
    scoreText.selectable = false;
    resetScore();
}

static function updateScore(points)
{
    score += points;
    scoreText.text = String(score);
    scoreHeader.setTextFormat(scoreFormat);
}

static function resetScore()
{
    score = 0;
    scoreText.text = String(score);
}

答案 1 :(得分:0)

尝试设置TF的defaultTextFormat,它会保存您使用文本格式跳转。

public function startGame(e:Event)
{
    scoreText = new TextField();
    scoreText.defaultTextFormat=new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
    scoreText.text = String(0);
    interfaceLayer.addChild(scoreText);
    scoreText.x = 75;
    scoreText.y = 0;
    scoreText.selectable = false;
    resetScore();
}