0

I am looking for a way, basically, to code an alarm clock for my computer. I want it to show the time, day of the week, month and day, and then based off that, I want it to show a message and play a sound once. I have four dynamic text boxes on the stage: time, display, day, date.

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

}

Without the display.text block, everything works perfectly. When I try and fiddle with that last bit, either by doing a for or if function, it throws the whole thing off.

How do I make the display text box read what's in the other boxes, and then return a phrase based on those values? I took a basic class in actionscript like four years ago, so layman's terms would be highly appreciated.

1 Answers1

0

To start, about your getDisplay() function, you should know that if you want that the text of a text field get the returned value of a function you have to use "return" inside that function to return a String object (as you did with your 3 others function).

Also, you are not forced to use text fields to put your informations before set it to a final text field.

So in your case, if you don't need to use the time, day and date text fields, you can set display.text like this :

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

But you can more simplify things using a flash.globalization.DateTimeFormatter object which will do the job for you and you have just to show what you need in your text field(s), like this :

// 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

Of course you can put those values into variables and then use them with your text field(s).

For more details about the pattern string used by the DateTimeFormatter object to format dates and times, take a look on the setDateTimePattern() function.

Hope that can help.

akmozo
  • 9,829
  • 3
  • 28
  • 44
  • Thank you so much for your reply!!! My issue isn't making the display text box show the weird combination of date variables, I want it to be able to display different messages based on the time of day (ie. at Noon M-F it will remind me to walk my dog). How do I get that text box to know the time but not display it, and then display something based on the time? – Shellie McCurdy Sep 18 '15 at 19:46
  • @ShellieMcCurdy If you can get the time, you can then use some `if` or a `switch` to do what you want : `if(time < 10){ trace('good morning'); } ... ` ... – akmozo Sep 19 '15 at 11:10
  • I tried to write a switch function, but I'm running into the same problem that I had with my previous if function. Whenever I write anything into the getDisplay(); the time stops updating itself. With no changes to the code except the removal of the getDisplay(); my code works perfectly fine. I'm new to this site, obviously. Is there a way for me to attach my .fla? – Shellie McCurdy Sep 20 '15 at 04:57
  • @ShellieMcCurdy To upload your fla, you can use any cloud / sharing free service like [copy.com](https://copy.com?r=sPuPXG), [mediafire.com](https://mfi.re/?2a66ldk), ... then you can put the link here. – akmozo Sep 20 '15 at 18:40
  • @ShellieMcCurdy OK, I saw your fla. You have some things that you should correct. 1. Remove the second image from your main timeline. 2. You can not do : `display.text = getDisplay();` and inside the same function you set the text of the `display` text field ! If you want to set the text inside the function, you have just to call the function : `getDisplay();` which will return nothing in that case (it should have the `void` as type), otherwise, you have to set its type to `String` and inside it you have to use a `return "some text";` and then in then : `display.text = getDisplay();`. – akmozo Sep 21 '15 at 02:17
  • @ShellieMcCurdy 3. Your `switch` should use the `time.text` ( a `String` value ) not the `time` text field ! Hope that can help. If you still have questions, don't hesitate to ask me ! Good luck ! – akmozo Sep 21 '15 at 02:20
  • I know it's been a while, but I'm still not able to get the time to update after initially opening the .swf. I don't want to have to keep reopening it; I want it to be open in the corner of my screen at all times. I want the clock and date to update in real time, and when it reads certain times, for it to display certain messages. For some reason, having the display element makes the time stop updating, even though there's no stop in the code at all. I made the changes you suggested and it's still not cooperating. Updated .fla: http://www.mediafire.com/download/4gpc32r1kott0i9/Clock4.fla – Shellie McCurdy Oct 09 '15 at 19:36
  • @ShellieMcCurdy No problem. To update the time, you can use a [`Timer`](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html) every `n` seconds (30 seconds for example) ... – akmozo Oct 09 '15 at 20:50