16 Replies Latest reply on Dec 11, 2006 3:19 PM by Newsgroup_User

    New to Actionscript

    BetsyW. Level 1
      Both of the following are in my text; and it is not clear how they differ.. Anyone?

      var myDate:Date = new Date();

      var myDate:Date;
      myDate = “new Date()”;

      Elizabeth.. thanks
        • 1. Re: New to Actionscript
          cinemaguy
          var myDate:Date = new Date(); is creating a new date object
          myDate = "new Date()"; is setting "new Date()" as a string to the variable myDate
          • 2. Re: New to Actionscript
            BetsyW. Level 1
            Thanks cinemaguy but...

            Isn't the first statement sufficient... why would I need to set the "new Date()" as a string to the variable myDate?
            • 3. Re: New to Actionscript
              cinemaguy Level 1
              I have no idea. The first statement is definitely sufficient. Unless you are wanting to output the string from myDate at some later time in the movie which it sounds like you don't need to do there is no need for it. Your date object was created with that first line.
              • 4. Re: New to Actionscript
                BetsyW. Level 1
                thank you very much....

                Elizabeth
                • 5. New to Actionscript
                  ken-chan05
                  Creating the Date object is certainly not sufficient, it's like a clock without batteries. By setting the newDate() as a string to the variable my date, you can associate the Date object to a specific date.

                  var myDate:Date = new Date (99,6,10);

                  The above creates a instance of the Date class associated with the date 10 July 1999.

                  Or to create a Date object associated with the user's system clock,you can leave the parameters setting blank.
                  For example:-

                  var myDate:Date = new Date();
                  var myMonth:Number = myDate.getMonth();
                  var myDay:Number = myDate.getDay();
                  ....etc....
                  • 6. New to Actionscript
                    cinemaguy Level 1
                    Yes ken is correct you will obviously have to set the properties of the date Object but var myDate:Date = new Date(); is sufficient enough to create a new date Object. Just as I can create any object by calling var myObj:Object = new Object(); granted its not going to do anything for me but create an instance of that object.
                    • 7. Re: New to Actionscript
                      BetsyW. Level 1
                      OK I think I get it .. it goes like this:

                      You need to create an instance of the class to use it in ActionScript, and you do so by naming it, or creating a named object • Use the keyword “new” to assign a unique name to a new instance of the class • Example: Adam = new Human (); (the statement that contains the “new” operator in front of the class is called a constructor function) • another example: var myDate:Date = new Date(); (this statement creates an object called myDate from the Date class) • However it is like a clock without batteries • In order to associate the date object with a specific date you must set the “newDate()” as a string to the variable “myDate” like so:

                      var myDate:Date;
                      myDate = “new Date()”;

                      am I getting there???
                      Elizabeth
                      • 8. New to Actionscript
                        cinemaguy Level 1
                        well kind of but that code will not work.
                        use the code that ken posted...

                        var myDate:Date = new Date (99,6,10);

                        and then you could use this to return the date

                        trace(myDate.getDate());
                        • 9. New to Actionscript
                          ken-chan05 Level 1
                          var myDate:Date;
                          myDate = new Date();

                          Recommended to use the below:-

                          var myDate:Date = newDate();

                          However it depends on what you want to do with the Date object. Like me, I use the Date class to retrieve info about the day,month,year and time in my applications.

                          Associating the date object with a specific date has a different function however. Normally it is used with a DateChooser component.

                          var myBirthday:Date = newDate (2006,1,1);
                          calendar_dc.selectedDate = myBirthday;

                          The first line creates a date object named myBirthday, and the next line uses that date object to set the value of the selectedDate property.





                          • 10. New to Actionscript
                            cinemaguy Level 1
                            Ken I think you are missing something... the code that you are posting is different than what she has posted.
                            yours...
                            var myDate:Date;
                            myDate = new Date();
                            hers....
                            var myDate:Date;
                            myDate = “new Date()”; // this will simply return a string of new Date() for the variable myDate

                            a small detail but very important.

                            Note: you could also return your date object as String using myDate.toString()
                            • 11. Re: New to Actionscript
                              Level 7
                              It's entirely sufficient to create a date object alone - it simply uses the
                              current system date:

                              var d = new Date();
                              trace((d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear());

                              12/11/2006



                              --
                              Dave -
                              Head Developer
                              www.blurredistinction.com
                              Adobe Community Expert
                              http://www.adobe.com/communities/experts/


                              • 12. Re: New to Actionscript
                                BetsyW. Level 1
                                Here is the AS from the example in the book:

                                var myDate_date:Date;
                                myDate_date = new Date();
                                myTextField_txt.text = myDate_date.getDate();

                                (I named the textfield on the stage "myTextField")

                                Does this help with the context of the issue?
                                • 13. Re: New to Actionscript
                                  ken-chan05 Level 1
                                  My eyes are getting old...I didn't see the " " until you mentioned it...haha. Very observant Cinemaguy !
                                  • 14. New to Actionscript
                                    ken-chan05 Level 1
                                    Your code :-

                                    var myDate_date:Date;
                                    myDate_date = new Date();
                                    myTextField_txt.text = myDate_date.getDate();

                                    That will make your text field, myTextField, to return a value of 1 - 31, depending on the user's system clock.

                                    DMenennoh's script is a shorthand way and a better one.
                                    • 15. Re: New to Actionscript
                                      BetsyW. Level 1
                                      oops... I probably should have omitted the " "

                                      I am going between old notes and the text... the example from the book contains the correct code for the context.

                                      Elizabeth
                                      • 16. Re: New to Actionscript
                                        Level 7
                                        >> You need to create an instance of the class to use it in ActionScript,
                                        >> and you
                                        do so by naming it, or creating a named object ? Use the keyword ?new? to
                                        assign a unique name to a new instance of the class ? Example: Adam = new
                                        Human
                                        (); (the statement that contains the ?new? operator in front of the class is
                                        called a constructor function) ? another example: var myDate:Date = new
                                        Date();
                                        (this statement creates an object called myDate from the Date class) ?
                                        However
                                        it is like a clock without batteries ? In order to associate the date
                                        object
                                        with a specific date you must set the ?newDate()? as a string to the
                                        variable
                                        ?myDate? like so:

                                        var myDate:Date;
                                        myDate = ?new Date()?;

                                        am I getting there???
                                        <<

                                        You're getting there. The variable you create is a pointer to an instance of
                                        a class: var myDate = new Date(); You can create as many instances of a
                                        given class as you need, by storing them in different variables. The 'new'
                                        keyword lets you associate a variable to a class - and calls the constructor
                                        of the class. The constructor function resides inside the class definition
                                        itself - it's name is the same name as the class name. So class Date has a
                                        function Date. When you make a 'new' instance of a class, the constructor
                                        function is called automatically and sent any paramters you have defined.

                                        When you say var myDate = new Date(); you create an instance of class Date
                                        and point to it with variable myDate. The date it contains will be the
                                        current date defined by the system clock, because you didn't give it any
                                        parameters.

                                        If you want to create an instance of Date associated with a particular date,
                                        you can give it a date when instantiating it. You don't send in any strings
                                        however. I'm not sure where string came from, but Date accepts Numbers only.
                                        To create a Date object for Jan 1, 1970 you could say: var epoch = new
                                        Date(1970, 0, 1); (months go from 0 - 11).



                                        --
                                        Dave -
                                        Head Developer
                                        www.blurredistinction.com
                                        Adobe Community Expert
                                        http://www.adobe.com/communities/experts/