Actionscript 3 根据一周中的时间和日期返回不同的文本

Actionscript 3 根据一周中的时间和日期返回不同的文本,actionscript-3,flash,actionscript,dynamic-text,Actionscript 3,Flash,Actionscript,Dynamic Text,我正在寻找一种方法,基本上,为我的电脑编码一个闹钟。我想让它显示时间、一周中的哪一天、哪一个月、哪一天,然后在此基础上,我想让它显示一条信息并播放一次声音。我在舞台上有四个动态文本框:时间、显示、日期和日期 import flash.net.URLRequest; import flash.media.Sound; time.text = getTime(); function getTime(){ var time = new Date(); var hour = time.

我正在寻找一种方法,基本上,为我的电脑编码一个闹钟。我想让它显示时间、一周中的哪一天、哪一个月、哪一天,然后在此基础上,我想让它显示一条信息并播放一次声音。我在舞台上有四个动态文本框:时间、显示、日期和日期

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();

}
导入flash.net.URLRequest;
导入flash.media.Sound;
time.text=getTime();
函数getTime(){
变量时间=新日期();
var hour=time.getHours();
var minute=time.getMinutes();
var temp=“”+((小时>12)?小时-12:小时);
温度+=((分钟<10)“:0”:“+”分钟;
温度+=(小时>=12)?“下午”:“上午”;
返回温度;
}
day.text=getToday();
函数getToday(){
var weekday_数组:数组=新数组(“星期日”、“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”);
var today=新日期();
var weekday:String=weekday_数组[today.getDay()];
var temp=工作日+“,”;
返回温度;
}
date.text=getDayz();
函数getDayz(){
var month_数组:数组=新数组(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”);
var calendar=新日期();
var number=calendar.getDate();
var month:String=month_数组[calendar.getMonth()];
变量温度=月份+“”;
温度+=数字;
返回温度;
}
display.text=getDisplay();
函数getDisplay(){
time.text=getTime();
day.text=getToday();
date.text=getDayz();
}
如果没有display.text块,一切都能正常工作。当我尝试摆弄最后一点时,无论是通过执行for还是if函数,它都会把整个事情抛在脑后


如何使“显示”文本框读取其他框中的内容,然后根据这些值返回短语?四年前,我参加了actionscript的基础课程,所以外行的术语将受到高度赞赏

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

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

因此,在您的情况下,如果不需要使用
时间
日期
日期
文本字段,您可以如下设置
显示。文本

display.text = getTime() + getToday() + getDayz();
// 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
但是,您可以使用
flash.globalization.DateTimeFormatter
对象来简化工作,该对象将为您完成任务,您只需在文本字段中显示所需内容,如下所示:

display.text = getTime() + getToday() + getDayz();
// 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
对象用于格式化日期和时间的模式字符串的更多详细信息,请查看


希望能有所帮助。

非常感谢您的回复!!!我的问题不是让显示文本框显示日期变量的奇怪组合,我希望它能够根据一天中的时间显示不同的消息(例如,在中午M-F它会提醒我遛狗)。如何让文本框知道时间但不显示它,然后根据时间显示一些内容?@shelleemccurdy如果你能得到时间,你可以使用一些
If
开关来做你想做的事情:
If(time<10){trace('good morning');}…我试图编写一个开关函数,但遇到了与前一个if函数相同的问题。每当我在getDisplay()中写入任何内容时;时间停止自我更新。除了删除getDisplay()之外,对代码没有任何更改;我的代码运行得非常好。很明显,我是这个网站的新手。有没有办法让我附上我的.fla?@ShellieMcCurdy要上传你的fla,你可以使用任何云/共享免费服务,比如。。。然后你可以把链接放在这里。@ShelleeMccurdy好的,我看到了你的fla。你有一些事情需要纠正。1.从主时间轴中删除第二个图像。2.您不能这样做:
display.text=getDisplay()
并在同一函数中设置
显示
文本字段的文本!如果要在函数内设置文本,只需调用函数:
getDisplay()
在这种情况下将不返回任何内容(它的类型应为
void
),否则,您必须将其类型设置为
String
,并在其内部使用
返回“some text”然后在中:
display.text=getDisplay()