如何将秒转换为分钟:as3中的秒数

时间:2014-10-31 12:34:06

标签: actionscript-3 flash-cs6

嗨所有我在flash cs6中的动作脚本3中的新功能我正在创建一个带有计时器的游戏

我希望将秒数转换为分钟:秒格式

示例:

120秒到2点

这是我的代码:

    var countDownDec:Number = 1;
    var totalSecs = 120;
    var countDownSecs = totalSecs;

    counter.text = countDownSecs;

    var time:Timer = new Timer(countDownDec*1000);

    time.addEventListener(TimerEvent.TIMER, tick);




    time.reset();
    countDownSecs = totalSecs;
    counter.text = countDownSecs;
    time.start();


    var frameLbl:FrameLabel;

    function tick(e:TimerEvent):void {

        time.start();

        if(countDownSecs == 0){

            time.stop();
            countDownSecs = totalSecs;
            gotoAndPlay('timesUp');
            TimesUp.play();

        }
        else{
            countDownSecs = countDownSecs - countDownDec;
            counter.text = countDownSecs;
        }
    }

请帮我解决问题

2 个答案:

答案 0 :(得分:1)

代码100%工作:

var count_down_interval:Number = 1

var total_seconds:Number = 120
var count_down_seconds:Number = total_seconds

var timer:Timer = new Timer(count_down_interval * 1000)
    timer.addEventListener(TimerEvent.TIMER, timer_on_Tick)

function timer_on_Tick(e:TimerEvent):void {

    if(count_down_seconds == 0){

        timer.stop()
        count_down_seconds = total_seconds          
        trace('game over')

    } else{

        count_down_seconds -= count_down_interval
        counter.text = convert_time(count_down_seconds)       

    }   

}

timer.start();
counter.text = convert_time(count_down_seconds)

function convert_time(time) {

    var  h, m, s:Number, t:String, a:Array

    if(isNaN(time)){        // 05:37 -> 337 seconds

        t = time
        a = t.split(':')

        if(a.length > 2){
            h = int(a[0]), m = int(a[1]), s = int(a[2])
        } else {
            h = 0, m = int(a[0]), s = int(a[1])         
        }       
        return h*3600 + m*60 + s

    } else {                // 337 -> 05:37

        t = ''
        h = int(time/3600)
        m = int((time-(h*3600))/60)
        if(time >= 3600) t += (h<10 ? '0' : '') + h + ':'       
        t += (m<10 ? '0' : '') + m + ':'
        t += (time % 60<10 ? '0' : '') + int(time % 60)

        return t

    }
}   

答案 1 :(得分:0)

除以60并向下舍入分钟,以秒为模(%)60秒

var totalSecs = 120;
var minutes = Math.floor(totalSecs / 60);
var seconds = totalSecs % 60;

var timeInMinutesSeconds = minutes + ":" + seconds;

编辑:

试试这个:

var countDownDec:Number = 1;
var totalSecs:int = 120;
var countDownSecs:int = totalSecs;
counter.text = getCountDownSecsText(countDownSecs);

var time:Timer = new Timer(countDownDec*1000);
time.addEventListener(TimerEvent.TIMER, tick);

var frameLbl:FrameLabel;

function tick(e:TimerEvent):void {
    if(countDownSecs == 0) {
        time.stop();
        countDownSecs = totalSecs;
        gotoAndPlay('timesUp');
        TimesUp.play();
    } else {
        countDownSecs = countDownSecs - countDownDec;
        counter.text = getCountDownSecsText(countDownSecs);
    }
}

private function getCountDownSecsText(currentAmountSecs:int):String{
    var minutes:int = Math.floor(currentAmountSecs / 60);
    var seconds:int = currentAmountSecs % 60;

    var prependString:String = "";
    if( minutes > 9 ) {
        prependString = "0";
    }

    return prependString + minutes + ":" + seconds;
}