0

基本上,我正在寻找一种方法来为我的计算机编写闹钟。我希望它显示时间、星期几、月份和日期,然后基于此,我希望它显示一条消息并播放一次声音。我在舞台上有四个动态文本框:时间、显示、日期、日期。

import flash.net.URLRequest;
import flash.media.Sound;

time.text = getTime();
function getTime(){
    var time = new Date();
    var hour = time.getHours();
    var minute = time.getMinutes();
    var temp = "" + ((hour > 12) ? hour - 12 : hour);
    temp += ((minute < 10) ? ":0" : ":") + minute;
    temp += (hour >= 12) ? " PM" : " AM";
    return temp;
}

day.text = getToday();
function getToday(){
    var weekday_array:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    var today = new Date();
        var weekday:String = weekday_array[today.getDay()];
        var temp = weekday + ","; 
    return temp;
}

date.text = getDayz();
function getDayz() {
    var month_array:Array = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    var calendar = new Date();
        var number = calendar.getDate();
        var month:String = month_array[calendar.getMonth()];
        var temp = month + " ";
        temp += number;  
   return temp;
}

    display.text = getDisplay();
    function getDisplay(){
        time.text = getTime();
        day.text = getToday();
        date.text = getDayz();

}

没有 display.text 块,一切正常。当我尝试通过执行 for 或 if 函数来摆弄最后一点时,它会把整个事情扔掉。

如何让显示文本框读取其他框中的内容,然后根据这些值返回一个短语?像四年前一样,我参加了动作脚本的基础课程,因此外行的术语将受到高度赞赏。

4

1 回答 1

0

首先,关于您的getDisplay()函数,您应该知道,如果您希望文本字段的文本获得函数的返回值,则必须在该函数中使用“return”来返回一个 String 对象(就像您对 3其他功能)。

此外,您不必在将信息设置为最终文本字段之前使用文本字段来放置信息。

所以在你的情况下,如果你不需要使用time,daydate文本字段,你可以display.text这样设置:

display.text = getTime() + getToday() + getDayz();

但是您可以使用一个可以为您完成工作的对象来简化事情flash.globalization.DateTimeFormatter,您只需在文本字段中显示您需要的内容,如下所示:

// I'm not in USA, that's why I used "en-US" to get the english format (for the example)
// for you, you can simply use LocaleID.DEFAULT, you will get your local format
var df:DateTimeFormatter = new DateTimeFormatter('en-US');  

    // get the time 
    // hh : hour of the day in a 12-hour format in two digits [01 - 12]
    // mm : minute of the hour in two digits [00 - 59]
    // a  : AM/PM indicator
    df.setDateTimePattern('hh:mm a');
    trace(df.format(new Date()));       // gives : 11:02 AM

    // get the day
    // EEEE : full name of the day in week
    df.setDateTimePattern('EEEE');
    trace(df.format(new Date()));       // gives : Friday

    // get the day and month
    // MMMM : full name of the month
    // dd   : day of the month in two digits [01 - 31]
    df.setDateTimePattern('MMMM dd');
    trace(df.format(new Date()));       // gives : September 18

当然,您可以将这些值放入变量中,然后将它们与您的文本字段一起使用。

有关DateTimeFormatter对象用于格式化日期和时间的模式字符串的更多详细信息,请查看setDateTimePattern()函数

希望能有所帮助。

于 2015-09-18T09:51:52.500 回答