简体   繁体   English

AS3计时器-将零添加到一位数字倒计时,更改字体颜色

[英]AS3 Timer - Add zero to single digit countdown, change font colour

I need help with a timer. 我需要计时器帮助。 I'd like to create a bomb-like digital countdown timer for a game. 我想为游戏创建一个类似于炸弹的数字倒数计时器。

  1. Using a digital font 使用数字字体
  2. always double digits eg 10, 09...01, 00 (to look like a bomb timer). 始终为两位数,例如10、09 ... 01、00(看起来像炸弹计时器)。
  3. And finally during the last few seconds, turning the font red to increase the drama. 最后在最后几秒钟内,将字体变成红色以增加戏剧性。

What I currently have below is a basic countdown timer, 20-0. 我目前在下面是一个基本的倒数计时器20-0。 The countdown variable starts at 20, is reduced by one every 1000ms and this number is shown in the text field. 倒数计时变量从20开始,每1000毫秒减少1,该数字显示在文本字段中。 But the font is generic, once the count gets below ten the numbers don't have a zero in front of them, and I have no idea how to change the font colour in the final seconds. 但是字体是通用的,一旦计数低于10,数字前面就不会有零,而且我不知道如何在最后几秒钟更改字体颜色。

public class UsingATimer extends Sprite
{
    //The Timer object
    public var timer:Timer= new Timer(1000, countdown);
    public var countdown:Number = 20;

    //The Text objects
    public var output:TextField = new TextField();
    public var format:TextFormat = new TextFormat();

    public function UsingATimer()
    {
        //Initialize the timer
        output.text = countdown.toString();
        timer.addEventListener(TimerEvent.TIMER, countdownHandler);
        timer.start();

        //Set the text format object
        format.font = "Helvetica";
        format.size = 200;
        format.color = 0x000000;
        format.align = TextFormatAlign.RIGHT;

        //Configure the output text field   
        output.defaultTextFormat = format;
        output.autoSize = TextFieldAutoSize.RIGHT;
        output.border = false;
        output.text = "20";

        //Display and position the output text field
        stage.addChild(output);
        output.x = 200;
        output.y = 100;

    }
    public function countdownHandler(event:TimerEvent):void
    {
        countdown--;
        output.text = countdown.toString();
    }

}

If there are no basic digital fonts I'll have to embed one, which I should be okay with but any help with the other two problems would be greatly appreciated. 如果没有基本的数字字体,我将不得不嵌入其中一种,我应该可以,但是对其他两种问题的任何帮助将不胜感激。

Sorry if the code is all over the place, I'm a beginner. 很抱歉,如果代码无处不在,我是一个初学者。

Thanks for your time. 谢谢你的时间。

I think you've mostly got it already. 我想您已经基本掌握了。 What you can do is inside your countdown handler, just check if the value is less then 10, if so append a 0 in front before displaying. 您可以在倒计时处理程序内部进行操作,只需检查该值是否小于10,如果是,则在显示之前在前面附加一个0。

countdown--;
if(countdown < 10){
    output.text = "0" + countdown.toString();
} else {
    output.text = countdown.toString();
}

Then to change the colour, it would be the same logic, check for what number you want it to change colour on, then change the colour in your TextFormat object and apply it to your TextField. 然后,要更改颜色,这将是相同的逻辑,检查要更改颜色的数字,然后更改TextFormat对象中的颜色并将其应用于TextField。

For a digital font, you could search for and embed one in to flash, but if all you need are numbers/limited characters, you could also make one as well with movie clips/graphics since they are relatively straight forward. 对于数字字体,您可以搜索并嵌入到Flash中,但是如果您只需要数字/受限字符,则由于它们相对简单,因此也可以对影片剪辑/图形进行制作。 Depends on how fancy you need it I guess as well as how flexible. 我想取决于您是否需要它,以及它有多灵活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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