简体   繁体   中英

TextFormat not working as intended initially

One of my text items (Score counter for a game) does not follow the format I have set when said text label has the value 0 but as soon as it updates to 1 or higher, the text is formatted correctly. When the score is 0 , the text is black and in Times New Roman font but then changes color, font and font size when it updates to 1 or higher. The problematic item in question is scoreText.text . This are the snippets of code that relates to the scoreText label:

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

If anyone could help pinpoint where I've went wrong I'd be grateful.

Thanks

Try this

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

Try setting defaultTextFormat of your TF, it'll save you jumps with text format.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM