将代码转换为按分钟和小时而不是100递增

时间:2016-02-16 19:13:29

标签: actionscript-3 flash

我正在尝试将此代码块转换为增量,如时间和分钟(0-60),而不是在它达到100时进行计数和更改。这是从为分数添加1,5或10的按钮运行的。 MC包含三个子MC的数字,每个数字都有一个文本字段,其中有10个帧,从0到9改变它。它的工作原理很完美,但是当前两位数字达到60而不是99时,我希望第100位(或digit3)变为1.我无法弄清楚我需要如何将其合并到代码中。任何帮助,将不胜感激。我甚至可能需要重写整个事情。

var SPEED:int = 1;// how fast to count
var NUM_DIGITS:int = 3;// how many digits are there in the score

var _totalScore:int = 0;
var _displayScore:int = 0;


// add an amount to the score
function add(amount:int):void
{
    _totalScore +=  amount;
    addEventListener(Event.ENTER_FRAME, updateScoreDisplay);// start the display counting up
}

// this runs every frame to update the score
function updateScoreDisplay(e:Event):void
{

    // increment the display score by the speed amount
    _displayScore +=  SPEED;

    // make sure the display score is not higher than the actual score
    if (_displayScore > _totalScore)
    {
        _displayScore = _totalScore;
    }

    var scoreStr:String = String(_displayScore);// cast displayScore as a String

    // add leading zeros
    while (scoreStr.length < NUM_DIGITS)
    {
        scoreStr = "0" + scoreStr;
    }

    // loop through and update each digit
    for (var i:int = 0; i < NUM_DIGITS; i++)
    {
        var num = int(scoreStr.charAt(i));

        this["digit" + (i + 1)].gotoAndStop(num+1);

    }
    // set the digit mc to the right frame;
    // if the display score is equal to the total score remove the enterframe event
    if (_totalScore == _displayScore)
    {
    removeEventListener(Event.ENTER_FRAME, updateScoreDisplay);
    }
}

2 个答案:

答案 0 :(得分:1)

您是否正在更改实际计算得分?或者只是显示分数的标签?

如果您希望将计算得分保持为正常数字,但只更改显示方式,请使用:

var seconds:String = (_displayScore % 60).toString();
var minutes:String = Math.floor(_displayScore / 60).toString();
if(seconds.length == 1)
    seconds = "0" + seconds;

var scoreStr:String = minutes + seconds;

答案 1 :(得分:0)

我宁愿将上述内容重写为符合您当前要求的内容。 这是一个例子:

Main.as

package {

import flash.display.Sprite;
import flash.events.Event;

[SWF(frameRate="24", backgroundColor="#FFFFFF", width="400", height="240")]
public class Main extends Sprite {

    private static const STOP_AT_HOUR:uint = 2;
    private static const STOP_AT_MINUTES:uint = 31;

    public function Main() {
        var tracker:Tracker = new Tracker();
        startTracking(tracker, STOP_AT_HOUR, STOP_AT_MINUTES,
                function onTrackingProgress(tracker:Tracker):void {
                    updateVisualElements(tracker);
                });
    }

    private function startTracking(tracker:Tracker, stopAtHour:uint, stopAtMinutes:uint, onProgress:Function):void {
        addEventListener(Event.ENTER_FRAME, function onEnterFrame(event:Event):void {
            onProgress(tracker.increment());
            if (tracker.hours == stopAtHour && tracker.minutes == stopAtMinutes) {
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                trace("Maximum bounds have been reached; tracking stopped.");
            }
        });
    }

    private function updateVisualElements(tracker:Tracker):void {
        var hoursString: String =  appendLeadingZero(tracker.hours);
        var minutesString:String = appendLeadingZero(tracker.minutes);
        trace("Tracked time: " + hoursString + ":" + minutesString);

        try {
            // update visual elements that display hour
            this["digit0"].gotoAndPlay(uint(hoursString.charAt(0)) + 1);
            this["digit1"].gotoAndPlay(uint(hoursString.charAt(1)) + 1);

            // update visual elements that display minutes
            this["digit2"].gotoAndPlay(uint(minutesString.charAt(0)) + 1);
            this["digit3"].gotoAndPlay(uint(minutesString.charAt(1)) + 1);

        } catch (error: Error) {
            // does not work because there are no visual elements named "digit0", "digit1", "digit2", "digit3"
            // in this example; the above code is purely demonstrative to show how such visual elements 
            // could be updated using the tracker information
        }
    }

    private function appendLeadingZero(temporalValue:uint):String {
        if (temporalValue >= 100) throw new ArgumentError("Implementation missing for values above 99");
        return temporalValue < 10 ? String("0" + temporalValue) : String(temporalValue);
    }
}
}

Tracker.as

package {

public class Tracker {

    private var _hours:uint = 0;
    private var _minutes:uint = 0;

    private var minutesResetValue:uint;

    public function Tracker(minutesResetValue:uint = 60) {
        this.minutesResetValue = minutesResetValue;
    }

    public function increment():Tracker {
        _minutes++;
        if (_minutes == minutesResetValue) {
            _minutes = 0;
            _hours++;
        }
        return this;
    }

    public function get hours():uint {
        return _hours;
    }

    public function get minutes():uint {
        return _minutes;
    }
}
}

运行上面的代码将输出:

[trace] Tracked time: 00:01
[trace] Tracked time: 00:02
[trace] Tracked time: 00:03
[trace] Tracked time: 00:04
[trace] Tracked time: 00:05
[trace] Tracked time: 00:06
[trace] Tracked time: 00:07
[trace] Tracked time: 00:08
[trace] Tracked time: 00:09
[trace] Tracked time: 00:10
[trace] Tracked time: 00:11
[trace] Tracked time: 00:12
[trace] Tracked time: 00:13
[trace] Tracked time: 00:14
[trace] Tracked time: 00:15
[trace] Tracked time: 00:16
[trace] Tracked time: 00:17
[trace] Tracked time: 00:18
[trace] Tracked time: 00:19
[trace] Tracked time: 00:20
[trace] Tracked time: 00:21
[trace] Tracked time: 00:22
[trace] Tracked time: 00:23
[trace] Tracked time: 00:24
[trace] Tracked time: 00:25
[trace] Tracked time: 00:26
[trace] Tracked time: 00:27
[trace] Tracked time: 00:28
[trace] Tracked time: 00:29
[trace] Tracked time: 00:30
[trace] Tracked time: 00:31
[trace] Tracked time: 00:32
[trace] Tracked time: 00:33
[trace] Tracked time: 00:34
[trace] Tracked time: 00:35
[trace] Tracked time: 00:36
[trace] Tracked time: 00:37
[trace] Tracked time: 00:38
[trace] Tracked time: 00:39
[trace] Tracked time: 00:40
[trace] Tracked time: 00:41
[trace] Tracked time: 00:42
[trace] Tracked time: 00:43
[trace] Tracked time: 00:44
[trace] Tracked time: 00:45
[trace] Tracked time: 00:46
[trace] Tracked time: 00:47
[trace] Tracked time: 00:48
[trace] Tracked time: 00:49
[trace] Tracked time: 00:50
[trace] Tracked time: 00:51
[trace] Tracked time: 00:52
[trace] Tracked time: 00:53
[trace] Tracked time: 00:54
[trace] Tracked time: 00:55
[trace] Tracked time: 00:56
[trace] Tracked time: 00:57
[trace] Tracked time: 00:58
[trace] Tracked time: 00:59
[trace] Tracked time: 01:00
[trace] Tracked time: 01:01
[trace] Tracked time: 01:02
[trace] Tracked time: 01:03
[trace] Tracked time: 01:04
[trace] Tracked time: 01:05
[trace] Tracked time: 01:06
[trace] Tracked time: 01:07
[trace] Tracked time: 01:08
[trace] Tracked time: 01:09
[trace] Tracked time: 01:10
[trace] Tracked time: 01:11
[trace] Tracked time: 01:12
[trace] Tracked time: 01:13
[trace] Tracked time: 01:14
[trace] Tracked time: 01:15
[trace] Tracked time: 01:16
[trace] Tracked time: 01:17
[trace] Tracked time: 01:18
[trace] Tracked time: 01:19
[trace] Tracked time: 01:20
[trace] Tracked time: 01:21
[trace] Tracked time: 01:22
[trace] Tracked time: 01:23
[trace] Tracked time: 01:24
[trace] Tracked time: 01:25
[trace] Tracked time: 01:26
[trace] Tracked time: 01:27
[trace] Tracked time: 01:28
[trace] Tracked time: 01:29
[trace] Tracked time: 01:30
[trace] Tracked time: 01:31
[trace] Tracked time: 01:32
[trace] Tracked time: 01:33
[trace] Tracked time: 01:34
[trace] Tracked time: 01:35
[trace] Tracked time: 01:36
[trace] Tracked time: 01:37
[trace] Tracked time: 01:38
[trace] Tracked time: 01:39
[trace] Tracked time: 01:40
[trace] Tracked time: 01:41
[trace] Tracked time: 01:42
[trace] Tracked time: 01:43
[trace] Tracked time: 01:44
[trace] Tracked time: 01:45
[trace] Tracked time: 01:46
[trace] Tracked time: 01:47
[trace] Tracked time: 01:48
[trace] Tracked time: 01:49
[trace] Tracked time: 01:50
[trace] Tracked time: 01:51
[trace] Tracked time: 01:52
[trace] Tracked time: 01:53
[trace] Tracked time: 01:54
[trace] Tracked time: 01:55
[trace] Tracked time: 01:56
[trace] Tracked time: 01:57
[trace] Tracked time: 01:58
[trace] Tracked time: 01:59
[trace] Tracked time: 02:00
[trace] Tracked time: 02:01
[trace] Tracked time: 02:02
[trace] Tracked time: 02:03
[trace] Tracked time: 02:04
[trace] Tracked time: 02:05
[trace] Tracked time: 02:06
[trace] Tracked time: 02:07
[trace] Tracked time: 02:08
[trace] Tracked time: 02:09
[trace] Tracked time: 02:10
[trace] Tracked time: 02:11
[trace] Tracked time: 02:12
[trace] Tracked time: 02:13
[trace] Tracked time: 02:14
[trace] Tracked time: 02:15
[trace] Tracked time: 02:16
[trace] Tracked time: 02:17
[trace] Tracked time: 02:18
[trace] Tracked time: 02:19
[trace] Tracked time: 02:20
[trace] Tracked time: 02:21
[trace] Tracked time: 02:22
[trace] Tracked time: 02:23
[trace] Tracked time: 02:24
[trace] Tracked time: 02:25
[trace] Tracked time: 02:26
[trace] Tracked time: 02:27
[trace] Tracked time: 02:28
[trace] Tracked time: 02:29
[trace] Tracked time: 02:30
[trace] Tracked time: 02:31
[trace] Maximum bounds have been reached; tracking stopped.