Adobe Flash

Create an object from a class

You'll create an existing class using visual tools (the TextField class) and code (using the Date class).

  1. Open a new Flash document and change the name of Layer 1 to Text.

  2. In the Text layer, create a dynamic text field and assign it the instance name of currentDate_txt.

  3. Create an Actions layer.

    With Frame 1 of the Actions layer selected, open the Actions panel.

  4. Create, or instantiate, an object from the Date class, named myDate:

    var myDate:Date=new Date();
    

  5. Create a variable called currentMonth equal to the getMonth() method:

    var currentMonth:Number = myDate.getMonth();
    

  6. Trace the value of currentMonth:

    trace (currentMonth);
    

  7. Save and test the document.

    You should see a number in the Output panel that represents the month.

    The getMonth() method displays the current month. The getMonth() method is zero-indexed, meaning the numbering begins at zero rather than one, so the number displayed is one less than what you would expect.

  8. Close the Output panel and the SWF file window.

Modify your script

You'll modify your script to compensate for the zero indexing.

  1. Add +1 to the value when you create currentMonth, and test your document to be sure the expected month number appears.

    That line of script should read as follows:

    var currentMonth:Number = myDate.getMonth()+1;
    

  2. Comment the trace statement:

    // trace (currentMonth);
    

  3. Below the trace statement, set the autoSize property of your text box to true:

    currentDate_txt.autoSize = true;
    

  4. Use the text property of your text box to display today's date in the form Today is mm/dd/yyyy.

    Use the currentMonth variable you already created, plus the geTDate() and getFullYear() methods of the Date object:

    currentDate_txt.text="Today is "+currentMonth+"/"+
    
      myDate.getDate() + "/"+myDate.getFullYear();
    

  5. Verify that your script appears as follows:

    var myDate:Date=new Date();
    
    var currentMonth:Number = myDate.getMonth()+1;
    
    // trace (currentMonth);
    currentDate_txt.autoSize = true;
    currentDate_txt.text="Today is "+currentMonth+"/"+myDate.getDate() + "/"+myDate.getFullYear();
    

  6. Save and test the document.

    The current date should appear in the SWF file window.

by BrainBellupdated
Advertisement: