1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Flash: ActionScript Language Reference- P3 docx

100 409 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề ActionScript Language Reference
Trường học University of Technology and Education
Chuyên ngành Computer Science
Thể loại Document
Năm xuất bản 2004
Thành phố Ho Chi Minh City
Định dạng
Số trang 100
Dung lượng 318,63 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

To use this code in a Flash FLA document, place it on Frame 1 on the main Timeline and place a movie clip on the Stage with the instance name my_mc, as in the following code: // Create a

Trang 1

expression Any expression.

statement(s) Any statement or sequence of statements

Returns

Nothing

Description

Statement; defines a condition for the switch statement If the expression parameter equals the

expression parameter of the switch statement using strict equality (===), then Flash Player will

execute statements in the statement(s) parameter until it encounters a break statement or the

end of the switch statement

If you use the case statement outside a switch statement, it produces an error and the script

doesn’t compile

Note: You should always end the statement(s) parameter with a break statement If you omit the

break statement from the statement(s) parameter, it continues executing with the next case

statement instead of exiting the switch statement.

Example

The following example defines conditions for the switch statement thisMonth If thisMonth

equals the expression in the case statement, the statement executes

var thisMonth:Number = new Date().getMonth();

Trang 2

See also

break, default, === (strict equality), switch

Trang 3

[dynamic] class className [ extends superClass ]

[ implements interfaceName [, interfaceName ] ]

{

// class definition here

}

Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash

tab of your FLA file’s Publish Settings dialog box This keyword is supported only when used in

external script files—not in scripts written in the Actions panel.

Parameters

className The fully qualified name of the class

superClass The name of the class that className extends (inherits from) This parameter is

optional

interfaceName The name of the interface whose methods className must implement This

parameter is optional

Description

Statement; defines a custom class, which lets you instantiate objects that share methods and

properties that you define For example, if you are developing an invoice-tracking system, you

could create an invoice class that defines all the methods and properties that each invoice should

have You would then use the new invoice() command to create invoice objects

The name of the class must match the name of the external file that contains the class The name

of the external file must be the name of the class with the file extension as appended For

example, if you name a class Student, the file that defines the class must be named Student.as

If a class is within a package, the class declaration must use the fully qualified class name of the

form base.sub1.sub2.MyClass (for more information, see “Using packages” in Using ActionScript

in Flash) Also, the class’s AS file must be stored within the path in a directory structure that

reflects the package structure, such as base/sub1/sub2/MyClass.as (for more information, see

“Understanding the classpath” in Using ActionsScript in Flash) If a class definition is of the form

“class MyClass,” it is in the default package and the MyClass.as file should be in the top level of

some directory in the path

For this reason, it’s good practice to plan your directory structure before you begin creating

classes Otherwise, if you decide to move class files after you create them, you have to modify the

class declaration statements to reflect their new location

You cannot nest class definitions; that is, you cannot define additional classes within a

class definition

CHAPTER 2

ActionScript Language Reference

Trang 4

To indicate that objects can add and access dynamic properties at runtime, precede the class statement with the dynamic keyword To declare that a class implements an interface, use the

implements keyword To create subclasses of a class, use the extends keyword (A class can extend only one class, but can implement several interfaces.) You can use implements and

extends in a single statement The following examples show typical uses of the implements and

extends keywords:

class C implements Interface_i, Interface_j // OK

class C extends Class_d implements Interface_i, Interface_j // OK

class C extends Class_d, Class_e // not OK

For more information, see in Using ActionScript in Flash.

// Following line is constructor

// because it has the same name as the class

function Plant(param_leafType:String, param_bloomSeason:String) {

// Assign passed values to properties when new Plant object is created this.leafType = param_leafType;

this.bloomSeason = param_bloomSeason;

}

// Create methods to return property values, because best practice

// recommends against directly referencing a property of a class

In an external script file or in the Actions panel, use the new operator to create a Plant object

var pineTree:Plant = new Plant("Evergreen", "N/A");

// Confirm parameters were passed correctly

Trang 5

See also

dynamic, extends, implements, import, interface, new, Object.registerClass()

Trang 6

var intervalID:Number = setInterval(callback, 1000);

You need to clear the interval when you have finished using the function Create a button called

clearInt_btn and use the following ActionScript to clear setInterval():

Trang 7

The Color class lets you set the RGB color value and color transform of movie clips and retrieve

those values once they have been set

You must use the constructor new Color() to create a Color object before calling its methods

Method summary for the Color class

Constructor for the Color class

Constructor; creates a Color object for the movie clip specified by the target_mc parameter You

can then use the methods of that Color object to change the color of the entire target movie clip

Example

The following example creates a Color object called my_color for the movie clip my_mc and sets

its RGB value to orange:

var my_color:Color = new Color(my_mc);

my_color.setRGB(0xff9933);

Color.getRGB() Returns the numeric RGB value set by the last setRGB() call.

Color.getTransform() Returns the transform information set by the last setTransform() call.

Color.setRGB() Sets the hexadecimal representation of the RGB value for a Color object.

Color.setTransform() Sets the color transform for a Color object

CHAPTER 2

ActionScript Language Reference

Trang 8

The following code retrieves the RGB value for the Color object my_color, converts the value to

a hexadecimal string, and assigns it to the myValue variable To see this code work, add a movie clip instance to the Stage, and give it the instance name my_mc:

var my_color:Color = new Color(my_mc);

// set the color

my_color.setRGB(0xff9933);

var myValue:String = my_color.getRGB().toString(16);

// trace the color value

trace(myValue); // traces ff9933

See also

Color.setRGB()

Trang 9

var my_color:Color = new Color(my_mc);

var myTransform:Object = my_color.getTransform();

myTransform = { ra: 50, ba: 50, aa: 30};

my_color.setTransform(myTransform);

For descriptions of the parameters for a color transform object, see Color.setTransform()

See also

Color.setTransform()

Trang 10

in the main Timeline and select Control > Test Movie:

var my_color:Color = new Color(my_mc);

my_color.setRGB(0xFF0000); // my_mc turns red

See also

Color.setTransform()

Trang 11

Method; sets color transform information for a Color object The colorTransformObject

parameter is a generic object that you create from the new Object constructor It has parameters specifying the percentage and offset values for the red, green, blue, and alpha (transparency)

components of a color, entered in the format 0xRRGGBBAA.

The parameters for a color transform object correspond to the settings in the Advanced Effect dialog box and are defined as follows:

ra is the percentage for the red component (-100 to 100)

rb is the offset for the red component (-255 to 255)

ga is the percentage for the green component (-100 to 100)

gb is the offset for the green component (-255 to 255)

ba is the percentage for the blue component (-100 to 100)

bb is the offset for the blue component (-255 to 255)

aa is the percentage for alpha (-100 to 100)

ab is the offset for alpha (-255 to 255)

You create a colorTransformObject parameter as follows:

var myColorTransform:Object = new Object();

You can also use the following syntax to create a colorTransformObject parameter:

var myColorTransform:Object = { ra: 50, rb: 244, ga: 40, gb: 112, ba: 12, bb:

90, aa: 40, ab: 70}

Trang 12

This example creates a new Color object for a target SWF file, creates a generic object called

myColorTransform with the properties defined above, and uses the setTransform() method to pass the colorTransformObject to a Color object To use this code in a Flash (FLA) document, place it on Frame 1 on the main Timeline and place a movie clip on the Stage with the instance name my_mc, as in the following code:

// Create a color object called my_color for the target my_mc

var my_color:Color = new Color(my_mc);

// Create a color transform object called myColorTransform using

// Set the values for myColorTransform

var myColorTransform:Object = { ra: 50, rb: 244, ga: 40, gb: 112, ba: 12, bb:

90, aa: 40, ab: 70};

// Associate the color transform object with the Color object

// created for my_mc

my_color.setTransform(myColorTransform);

Trang 13

The ContextMenu class provides runtime control over the items in the Flash Player context

menu, which appears when a user right-clicks (Windows) or Control-clicks (Macintosh) on Flash

Player You can use the methods and properties of the ContextMenu class to add custom menu

items, control the display of the built-in context menu items (for example, Zoom In and Print),

or create copies of menus

You can attach a ContextMenu object to a specific button, movie clip, or text field object, or to an

entire movie level You use the menu property of the Button, MovieClip, or TextField classes to do

this For more information about the menu property, see Button.menu, MovieClip.menu, and

TextField.menu

To add new items to a ContextMenu object, you create a ContextMenuItem object, and then add

that object to the ContextMenu.customItems array For more information about creating

context menu items, see the ContextMenuItem class entry

Flash Player has three types of context menus: the standard menu (which appears when you

right-click in Flash Player), the edit menu (which appears when you right-right-click over a selectable or

editable text field), and an error menu (which appears when a SWF file has failed to load into

Flash Player.) Only the standard and edit menus can be modified with the ContextMenu class

Custom menu items always appear at the top of the Flash Player context menu, above any visible

built-in menu items; a separator bar distinguishes built-in and custom menu items You can add

no more than 15 custom items to a context menu You cannot remove the Settings menu item

from the context menu The Settings menu item is required in Flash so users can access the

settings that affect privacy and storage on their computers You also cannot remove the About

menu item from the context menu, which is required so users can find out what version of Flash

Player they are using

You must use the constructor new ContextMenu() to create a ContextMenu object before calling

its methods

Method summary for the ContextMenu class

ContextMenu.copy() Returns a copy of the specified ContextMenu object.

ContextMenu.hideBuiltInItems() Hides most built-in items in the Flash Player context menu.

CHAPTER 2

ActionScript Language Reference

Trang 14

Property summary for the ContextMenu class

Event handler summary for the ContextMenu class

Constructor for the ContextMenu class

the context menu, but before the menu is actually displayed This is useful for customizing menu

contents based on application state or based on the type of object (movie clip, text field, or button) or the Timeline that the user right-clicks or Control-clicks (For an example of creating

an event handler, see ContextMenu.onSelect.)

Trang 15

ContextMenu class 215

var showItem = true; // Change this to false to remove

var my_cm:ContextMenu = new ContextMenu(menuHandler);

my_cm.customItems.push(new ContextMenuItem("Hello", itemHandler));

function menuHandler(obj, menuObj) {

function itemHandler(obj, item) {

// put code here

Button.menu , ContextMenu.onSelect , ContextMenu.customItems ,

ContextMenu.hideBuiltInItems() , MovieClip.menu , TextField.menu

Trang 16

Property; an object that has the following Boolean properties: save, zoom, quality, play, loop,

rewind, forward_back, and print Setting these variables to false removes the corresponding menu items from the specified ContextMenu object These properties are enumerable and are set

Note: You cannot disable the Settings or About menu items from the context menu

In the next example, a for in loop enumerates through all names and values of the built-in menu items of the ContextMenu object, my_cm

var my_cm:ContextMenu = new ContextMenu();

for(eachProp in my_cm.builtInItems) {

var propName = eachProp;

var propValue = my_cm.builtInItems[propName];

trace(propName + ": " + propValue);

}

Trang 17

Method; creates a copy of the specified ContextMenu object The copy inherits all the properties

of the original menu object

Example

This example creates a copy of the ContextMenu object named my_cm whose built-in menu items are hidden, and adds a menu item with the text “Save ” It then creates a copy of my_cm and assigns it to the variable clone_cm, which inherits all the properties of the original menu

var my_cm:ContextMenu = new ContextMenu();

Trang 18

To add new menu items, you first create a new ContextMenuItem object, and then add it to the

menu_mc.customItems array (for example, using Array.push()) For more information about creating new menu items, see the ContextMenuItem class entry

Example

The following example creates a new custom menu item called menuItem_cm with a caption of

“Send e-mail” and a callback handler named emailHandler The new menu item is then added

to the ContextMenu object, my_cm, using the customItems array Finally, the new menu is attached to a movie clip named email_mc

var my_cm:ContextMenu = new ContextMenu();

var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Send e-mail",

Trang 19

This method hides only menu items that appear in the standard context menu; it does not affect items that appear in the edit or error menus For more information about the different menu types, see the ContextMenu class entry.

This method works by setting all the Boolean members of my_cm.builtInItems to false You can selectively make a built-in item visible by setting its corresponding member in

my_cm.builtInItems to true (as demonstrated in the following example)

Trang 20

Availability

Flash Player 7

Usage

my_cm.onSelect = function (item:Object, item_menu:ContextMenu) : Void{

// your code here

}

Parameters

item A reference to the object (movie clip, button, or selectable text field) that was under the mouse pointer when the Flash Player context menu was invoked and whose menu property is set

to a valid ContextMenu object

item_menu A reference to the ContextMenu object assigned to the menu property of object

You can also specify the callback handler for a ContextMenu object when you construct a new ContextMenu object For more information, see the ContextMenu class entry

Example

The following example determines over what type of object the context menu was invoked:

my_cm = new ContextMenu();

function menuHandler(obj:Object, menu:ContextMenu) {

if(obj instanceof MovieClip) {

trace("Movie clip: " + obj);

}

if(obj instanceof TextField) {

trace("Text field: " + obj);

Trang 21

You use the ContextMenuItem class to create custom menu items to display in the Flash Player

context menu Each ContextMenuItem object has a caption (text) that is displayed in the context

menu, and a callback handler (a function) that is invoked when the menu item is selected To add

a new context menu item to a context menu, you add it to the customItems array of a

ContextMenu object

You can enable or disable specific menu items, make items visible or invisible, or change the

caption or callback handler associated with a menu item

Custom menu items appear at the top of the context menu, above any built-in items A separator

bar always divides custom menu items from built-in items You can add no more than 15 custom

items to a context menu Each item must contain at least one visible character— control

characters, newlines, and other white space characters are ignored No item can be more than 100

characters long Items that are identical to any built-in menu item, or to another custom item, are

ignored, whether the matching item is visible or not Menu items are compared without regard to

case, punctuation, or white space

None of the following words can appear in a custom item: Macromedia, Flash Player, or Settings.

Method summary for the ContextMenuItem class

Property summary for the ContextMenuItem class

Event handler summary for the ContextMenuItem class

ContextMenuItem.copy() Returns a copy of the specified ContextMenuItem object.

ContextMenuItem.caption Specifies the text displayed in the menu item.

ContextMenuItem.enabled Specifies whether the menu item is enabled or disabled.

ContextMenuItem.separatorBefore Specifies whether a separator bar should appear above the

menu item.

ContextMenuItem.visible Specifies whether the menu item is visible.

ContextMenuItem.onSelect Invoked when the menu item is selected.

CHAPTER 2

ActionScript Language Reference

Trang 22

Constructor for the ContextMenuItem class

[ visible:Boolean] ] ] ) : ContextMenuItem

Parameters

caption A string that specifies the text associated with the menu item

callbackFunction A function that you define, which is called when the menu item is selected

separatorBefore A Boolean value that indicates whether a separator bar should appear above the menu item in the context menu The default value is false This parameter is optional

enabled A Boolean value that indicates whether the menu item is enabled or disabled in the context menu The default value is true This parameter is optional

visible A Boolean value that indicates whether the menu item is visible or invisible The default value is true This parameter is optional

This example adds Start and Stop menu items, separated by a bar, to the ContextMenu object

my_cm The startHandler() function is called when Start is selected from the context menu;

stopHandler() is called when Stop is selected The ContextMenu object is applied to the current Timeline

var my_cm:ContextMenu = new ContextMenu();

my_cm.customItems.push(new ContextMenuItem("Start", startHandler));

my_cm.customItems.push(new ContextMenuItem("Stop", stopHandler, true));

function stopHandler(obj, item) {

trace("Stopping ");

}

function startHandler(obj, item) {

Trang 23

var my_cm:ContextMenu = new ContextMenu();

var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Pause Game", onPause); my_cm.customItems.push(menuItem_cmi);

function onPause(obj, menuItem) {

trace("You chose: " + menuItem.caption);

}

this.menu = my_cm;

Trang 24

var original_cmi:ContextMenuItem = new ContextMenuItem("Pause", onPause); function onPause(obj:Object, menu:ContextMenu) {

trace("pause me");

}

var copy_cmi:ContextMenuItem = original_cmi.copy();

var my_cm:ContextMenu = new ContextMenu();

my_cm.customItems.push(original_cmi);

my_cm.customItems.push(copy_cmi);

my_mc.menu = my_cm;

Trang 25

Property; a Boolean value that indicates whether the specified menu item is enabled or disabled

By default, this property is true

Example

The following example creates two new context menu items: Start and Stop When the user selects Start, the number of milliseconds from when the SWF file started is traced Start is then disabled in the menu When Stop is selected, the number of milliseconds that have elapsed since the SWF file started is traced The Start menu item is re-enabled and the Stop menu item is disabled

var my_cm:ContextMenu = new ContextMenu();

var startMenuItem:ContextMenuItem = new ContextMenuItem("Start",

Trang 26

The following example assigns a function to the onSelect handler for a ContextMenuItem object named my_cmi The function displays the caption of the selected menu item

var my_cmi:ContextMenu = new ContextMenu();

var start_cmi:ContextMenuItem = new ContextMenuItem("Start");

start_cmi.onSelect = function(obj, item) {

trace("You chose: "+item.caption);

Trang 27

array Finally, the menu is attached to the current Timeline of the SWF file.

var my_cm:ContextMenu = new ContextMenu();

var open_cmi:ContextMenuItem = new ContextMenuItem("Open", itemHandler); var save_cmi:ContextMenuItem = new ContextMenuItem("Save", itemHandler); var print_cmi:ContextMenuItem = new ContextMenuItem("Print", itemHandler); print_cmi.separatorBefore = true;

my_cm.customItems.push(open_cmi, save_cmi, print_cmi);

function itemHandler(obj, menuItem) {

trace("You chose: " + menuItem.caption);

};

this.menu = my_cm;

See also

ContextMenu.onSelect

Trang 28

var my_cm:ContextMenu = new ContextMenu();

var startMenuItem:ContextMenuItem = new ContextMenuItem("Start",

Trang 29

Statement; jumps past all remaining statements in the innermost loop and starts the next iteration

of the loop as if control had passed through to the end of the loop normally It has no effect

outside a loop

Example

In the following while loop, continue causes the Flash interpreter to skip the rest of the loop

body and jump to the top of the loop, where the condition is tested:

In the following do while loop, continue causes the Flash interpreter to skip the rest of the loop

body and jump to the bottom of the loop, where the condition is tested:

Trang 30

In a for loop, continue causes the Flash interpreter to skip the rest of the loop body In the following example, if the i modulo 3 equals 0, then the trace(i) statement is skipped:

Trang 31

The methods of the CustomActions class allow a SWF file playing in the Flash authoring tool to

manage any custom actions that are registered with the authoring tool A SWF file can install and

uninstall custom actions, retrieve the XML definition of a custom action, and retrieve the list of

registered custom actions

You can use these methods to build SWF files that are extensions of the Flash authoring tool

Such an extension could, for example, use the Flash Application Protocol to navigate a UDDI

repository and download web services into the Actions toolbox

Method summary for the CustomActions class

CustomActions.get() Reads the contents of a custom action XML definition file.

CustomActions.install() Installs a new custom action XML definition file.

CustomActions.list() Returns a list of all registered custom actions.

CustomActions.uninstall() Removes a custom action XML definition file.

CHAPTER 2

ActionScript Language Reference

Trang 32

If the definition file specified by the customName cannot be found, a value of undefined is returned If the custom action XML definition specified by the customName parameter is located,

it is read in its entirety and returned as a string

Example

The following example lists the custom actions in a ComboBox instance, and gets the custom action when a Button instance is clicked Drag an instance of a ComboBox, Button, and TextArea onto the Stage Give the ComboBox an instance name of customActionName_cb, the TextArea

an instance name of customActionXml_ta, and the Button an instance name of view_button Enter the following ActionScript on Frame 1 of the Timeline:

Trang 33

customName The name of the custom action definition to install.

customXML The text of the XML definition to install

Returns

A Boolean value of false if an error occurs during installation; otherwise, a value of true is returned to indicate that the custom action has been successfully installed

Description

Method; installs a new custom action XML definition file indicated by the customName

parameter The contents of the file is specified by the string customXML

The name of the definition file must be a simple filename, without the xml file extension, and without any directory separators (':', '/' or '\')

If a custom actions file already exists with the name customName, it is overwritten

If the Configuration/ActionsPanel/CustomActions directory does not exist when this method is invoked, the directory is created

<string version="7" id="getFleas" name="getFleas" tiptext="gets number

of fleas" text=".getFleas(% fleas %)" />

Trang 34

Then open a new FLA file in the same directory and select Frame 1 of the Timeline Enter the following code into the Actions panel:

var my_xml:XML = new XML();

Trang 35

The following example lists the custom actions in a ComboBox instance, and gets the custom action when a Button instance is clicked Drag an instance of a ComboBox, Button, and TextArea onto the Stage Give the ComboBox an instance name of customActionName_cb, the TextArea

an instance name of customActionXml_ta, and the Button an instance name of view_button Enter the following ActionScript on Frame 1 of the Timeline:

Trang 36

Method; removes the Custom Actions XML definition file named customName.

The name of the definition file must be a simple filename, without the xml file extension, and without any directory separators (':', '/' or '\')

Example

The following example installs a new custom action and displays an array containing the names of all the custom actions that are registered with the Flash authoring tool in the Output panel When the uninstall_btn is clicked, the custom action is then uninstalled An array containing names of the custom actions installed is displayed, and dogclass should then be removed from the array Create a button called uninstall_btn and then enter the following ActionScript onto Frame 1 of the Timeline:

var my_xml:XML = new XML();

Trang 37

The Date class lets you retrieve date and time values relative to universal time (Greenwich Mean

Time, now called universal time or UTC) or relative to the operating system on which Flash

Player is running The methods of the Date class are not static but apply only to the individual

Date object specified when the method is called The Date.UTC() method is an exception; it is a

static method

The Date class handles daylight saving time differently, depending on the operating system and

Flash Player version Flash Player 6 and later versions handle daylight saving time on the

following operating systems in these ways:

• Windows—the Date object automatically adjusts its output for daylight saving time The Date

object detects whether daylight saving time is employed in the current locale, and if so, it

detects the standard-to-daylight saving time transition date and times However, the transition

dates currently in effect are applied to dates in the past and the future, so the daylight saving

time bias might calculate incorrectly for dates in the past when the locale had different

transition dates

• Mac OS X—the Date object automatically adjusts its output for daylight saving time The

time zone information database in Mac OS X is used to determine whether any date or time in

the present or past should have a daylight saving time bias applied

• Mac OS 9—the operating system provides only enough information to determine whether the

current date and time should have a daylight saving time bias applied Accordingly, the date

object assumes that the current daylight saving time bias applies to all dates and times in the

past or future

Flash Player 5 handles daylight saving time on the following operating systems as follows:

• Windows—the U.S rules for daylight saving time are always applied, which leads to incorrect

transitions in Europe and other areas that employ daylight saving time but have different

transition times than the U.S Flash correctly detects whether daylight saving time is used in

the current locale

To call the methods of the Date class, you must first create a Date object using the constructor for

the Date class, described later in this section

Method summary for the Date class

Date.getDate() Returns the day of the month according to local time.

Date.getDay() Returns the day of the week according to local time.

Date.getFullYear() Returns the four-digit year according to local time.

Date.getHours() Returns the hour according to local time.

CHAPTER 2

ActionScript Language Reference

Trang 38

Date.getMilliseconds() Returns the milliseconds according to local time.

Date.getMinutes() Returns the minutes according to local time.

Date.getMonth() Returns the month according to local time.

Date.getSeconds() Returns the seconds according to local time.

Date.getTime() Returns the number of milliseconds since midnight January 1, 1970,

universal time.

Date.getTimezoneOffset() Returns the difference, in minutes, between the computer’s local time

and universal time.

Date.getUTCDate() Returns the day of the month according to universal time.

Date.getUTCDay() Returns the day of the week according to universal time.

Date.getUTCFullYear() Returns the four-digit year according to universal time.

Date.getUTCHours() Returns the hour according to universal time.

Date.getUTCMilliseconds() Returns the milliseconds according to universal time.

Date.getUTCMinutes() Returns the minutes according to universal time.

Date.getUTCMonth() Returns the month according to universal time.

Date.getUTCSeconds() Returns the seconds according to universal time.

Date.getYear() Returns the year according to local time.

Date.setDate() Sets the day of the month according to local time Returns the new

Date.setTime() Sets the date in milliseconds Returns the new time in milliseconds.

Date.setUTCDate() Sets the date according to universal time Returns the new time

Trang 39

new Date() : Date

new Date(timeValue:Number) : Date

new Date(year:Number, month:Number [, date:Number [, hour:Number [,

minute:Number [, second:Number [, millisecond:Number ]]]]]) : Date

Parameters

year A value of 0 to 99 indicates 1900 through 1999; otherwise all four digits of the year must

be specified

month An integer from 0 (January) to 11 (December)

date An integer from 1 to 31 This parameter is optional

hour An integer from 0 (midnight) to 23 (11 p.m.)

minute An integer from 0 to 59 This parameter is optional

second An integer from 0 to 59 This parameter is optional

millisecond An integer from 0 to 999 This parameter is optional

Returns

A reference to a Date object

Date.setUTCHours() Sets the hour according to universal time Returns the new time

Date.setYear() Sets the year according to local time.

Date.toString() Returns a string value representing the date and time stored in the

specified Date object.

Date.UTC() Returns the number of milliseconds between midnight on January 1,

1970, universal time, and the specified time

Trang 40

Constructor; constructs a new Date object that holds the specified date or the current date and time

Example

The following example retrieves the current date and time:

var now_date:Date = new Date();

The following example creates a new Date object for Mary’s birthday, August 12, 1974 (because the month parameter is zero-based, the example uses 7 for the month, not 8):

var maryBirthday:Date = new Date (74, 7, 12);

The following example creates a new Date object and concatenates the returned values of Date.getMonth(), Date.getDate(), and Date.getFullYear():

var today_date:Date = new Date();

var date_str:String = ((today_date.getMonth()+1)+"/"+today_date.getDate()+"/

"+today_date.getFullYear());

trace(date_str); // displays current date in United States date format

Ngày đăng: 14/12/2013, 14:15

TỪ KHÓA LIÊN QUAN