簡體   English   中英

AS3計時器-將零添加到一位數字倒計時,更改字體顏色

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

我需要計時器幫助。 我想為游戲創建一個類似於炸彈的數字倒數計時器。

  1. 使用數字字體
  2. 始終為兩位數,例如10、09 ... 01、00(看起來像炸彈計時器)。
  3. 最后在最后幾秒鍾內,將字體變成紅色以增加戲劇性。

我目前在下面是一個基本的倒數計時器20-0。 倒數計時變量從20開始,每1000毫秒減少1,該數字顯示在文本字段中。 但是字體是通用的,一旦計數低於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();
    }

}

如果沒有基本的數字字體,我將不得不嵌入其中一種,我應該可以,但是對其他兩種問題的任何幫助將不勝感激。

很抱歉,如果代碼無處不在,我是一個初學者。

謝謝你的時間。

我想您已經基本掌握了。 您可以在倒計時處理程序內部進行操作,只需檢查該值是否小於10,如果是,則在顯示之前在前面附加一個0。

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

然后,要更改顏色,這將是相同的邏輯,檢查要更改顏色的數字,然后更改TextFormat對象中的顏色並將其應用於TextField。

對於數字字體,您可以搜索並嵌入到Flash中,但是如果您只需要數字/受限字符,則由於它們相對簡單,因此也可以對影片剪輯/圖形進行制作。 我想取決於您是否需要它,以及它有多靈活。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM