case 201
case
Availability
Flash Player 4.
Usage
case expression: statement(s)
Parameters
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();
switch (thisMonth) {
case 0 :
trace("January");
break;
case 1 :
trace("February");
break;
case 5 :
case 6 :
case 7 :
trace("Some summer month");
break;
case 8 :
trace("September");
break;
default :
trace("some other month");
}
CHAPTER 2
ActionScript Language Reference
202 Chapter 2: ActionScript Language Reference
See also
break
,
default
,
=== (strict equality)
,
switch
class 203
class
Availability
Flash Player 6.
Usage
[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
204 Chapter 2: ActionScript Language Reference
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.
Example
The following example creates a class called Plant. The Plant constructor takes two parameters.
// Filename Plant.as
class Plant {
// Define property names and types
var leafType:String;
var bloomSeason:String;
// 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
function getLeafType():String {
return leafType;
}
function getBloomSeason():String {
return bloomSeason;
}
}
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
trace(pineTree.getLeafType());
trace(pineTree.getBloomSeason());
The following example creates a class called ImageLoader. The
ImageLoader
constructor takes
three parameters.
// Filename ImageLoader.as
class ImageLoader extends MovieClip {
function ImageLoader(image:String, target_mc:MovieClip, init:Object) {
var listenerObject:Object = new Object();
listenerObject.onLoadInit = function(target) {
for (var i in init) {
target[i] = init[i];
}
class 205
};
var JPEG_mcl:MovieClipLoader = new MovieClipLoader();
JPEG_mcl.addListener(listenerObject);
JPEG_mcl.loadClip(image, target_mc);
}
}
In an external script file or in the Actions panel, use the
new
operator to create a ImageLoader
object.
var jakob_mc:MovieClip = this.createEmptyMovieClip("jakob_mc",
this.getNextHighestDepth());
var jakob:ImageLoader = new ImageLoader(" />blueprint/articles/nielsen/spotlight_jnielsen.jpg", jakob_mc, {_x:10, _y:10,
_alpha:70, _rotation:-5});
See also
dynamic
,
extends
,
implements
,
import
,
interface
,
new
,
Object.registerClass()
206 Chapter 2: ActionScript Language Reference
clearInterval()
Availability
Flash Player 6.
Usage
clearInterval( intervalID:Number ) : Void
Parameters
intervalID
A numeric (integer) identifier returned from a call to
setInterval()
.
Returns
Nothing.
Description
Function; cancels an interval created by a call to
setInterval()
.
Example
The following example first sets and then clears an interval call:
function callback() {
trace("interval called: "+getTimer()+" ms.");
}
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()
:
clearInt_btn.onRelease = function(){
clearInterval( intervalID );
trace(“cleared interval”);
};
See also
setInterval()
CHAPTER 2
ActionScript Language Reference
Color class 207
Color class
Availability
Flash Player 5.
Description
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
Availability
Flash Player 5.
Usage
new Color(target_mc:Object) : Color
Parameters
target_mc
The instance name of a movie clip.
Returns
A reference to a Color object.
Description
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);
Method Description
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
208 Chapter 2: ActionScript Language Reference
Color.getRGB()
Availability
Flash Player 5.
Usage
my_color.getRGB() : Number
Parameters
None.
Returns
A number that represents the RGB numeric value for the color specified.
Description
Method; returns the numeric values set by the last
setRGB()
call.
Example
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()
Color.getTransform() 209
Color.getTransform()
Availability
Flash Player 5.
Usage
my_color.getTransform() : Object
Parameters
None.
Returns
An object whose properties contain the current offset and percentage values for the
specified color.
Description
Method; returns the transform value set by the last
Color.setTransform()
call.
Example
The following example gets the transform object, and then sets new percentages for colors and
alpha of
my_mc
relative to their current values. To see this code work, place a multicolored movie
clip on the Stage with the instance name
my_mc
. Then place the following code on Frame 1 in the
main Timeline and select Control > Test Movie:
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()
210 Chapter 2: ActionScript Language Reference
Color.setRGB()
Availability
Flash Player 5.
Usage
my_color.setRGB(0xRRGGBB:Number) : Void
Parameters
0xRRGGBB
The hexadecimal or RGB color to be set.
RR
,
GG
, and
BB
each consist of two
hexadecimal digits that specify the offset of each color component. The
0x
tells the ActionScript
compiler that the number is a hexadecimal value.
Description
Method; specifies an RGB color for a Color object. Calling this method overrides any previous
Color.setTransform()
settings.
Returns
Nothing.
Example
This example sets the RGB color value for the movie clip
my_mc
. To see this code work, place a
movie clip on the Stage with the instance name
my_mc
. Then place the following code on Frame 1
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()
Color.setTransform() 211
Color.setTransform()
Availability
Flash Player 5.
Usage
my_color.setTransform(colorTransformObject:Object) : Void
Parameters
colorTransformObject
An object created with the
new Object
constructor. This instance of
the Object class must have the following properties that specify color transform values:
ra
,
rb
,
ga
,
gb
,
ba
,
bb
,
aa
,
ab
. These properties are explained below.
Returns
Nothing.
Description
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();
myColorTransform.ra = 50;
myColorTransform.rb = 244;
myColorTransform.ga = 40;
myColorTransform.gb = 112;
myColorTransform.ba = 12;
myColorTransform.bb = 90;
myColorTransform.aa = 40;
myColorTransform.ab = 70;
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}
212 Chapter 2: ActionScript Language Reference
Example
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);
ContextMenu class 213
ContextMenu class
Availability
Flash Player 7.
Description
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-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
Method Description
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
214 Chapter 2: ActionScript Language Reference
Property summary for the ContextMenu class
Event handler summary for the ContextMenu class
Constructor for the ContextMenu class
Availability
Flash Player 7.
Usage
new ContextMenu ([callBackFunction])
Parameters
callBackFunction
A reference to a function that is called when the user right-clicks or
Control-clicks, before the menu is displayed. This parameter is optional.
Returns
A reference to a ContextMenu object.
Description
Constructor; creates a new ContextMenu object. You can optionally specify an identifier for an
event handler when you create the object. The specified function is called when the user invokes
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
.)
Example
The following example hides all the built-in objects in the Context menu. (However, the Settings
and About items still appear, because they cannot be disabled.)
var newMenu:ContextMenu = new ContextMenu();
newMenu.hideBuiltInItems();
this.menu = newMenu;
In this example, the specified event handler,
menuHandler
, enables or disables a custom menu
item (using the
ContextMenu.customItems
array) based on the value of a Boolean variable
named
showItem
. If
false
, the custom menu item is disabled; otherwise, it’s enabled.
Property Description
ContextMenu.builtInItems
An object whose members correspond to built-in context
menu items.
ContextMenu.customItems
An array, undefined by default, that contains
ContextMenuItem objects.
Property Description
ContextMenu.onSelect
Invoked before the menu is displayed.
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) {
if (showItem == false) {
menuObj.customItems[0].enabled = false;
} else {
menuObj.customItems[0].enabled = true;
}
}
function itemHandler(obj, item) {
//...put code here...
trace("selected!");
}
this.menu = my_cm;
When the user right-clicks or Control-clicks the Stage, the custom menu is displayed.
See also
Button.menu, ContextMenu.onSelect, ContextMenu.customItems,
ContextMenu.hideBuiltInItems(), MovieClip.menu, TextField.menu
216 Chapter 2: ActionScript Language Reference
ContextMenu.builtInItems
Availability
Flash Player 7.
Usage
my_cm.builtInItems:Object
Description
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
to
true
by default.
Example
In this example, the built-in Quality and Print menu items are disabled for the ContextMenu
object
my_cm
, which is attached to the current Timeline of the SWF file.
var my_cm:ContextMenu = new ContextMenu ();
my_cm.builtInItems.quality=false;
my_cm.builtInItems.print=false;
this.menu = my_cm;
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);
}
ContextMenu.copy() 217
ContextMenu.copy()
Availability
Flash Player 7.
Usage
my_cm.copy() : ContextMenu
Parameters
None.
Returns
A ContextMenu object.
Description
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();
my_cm.hideBuiltInItems();
var menuItem_cmi:ContextMenuItem = new ContextMenuItem("Save...",
saveHandler);
my_cm.customItems.push(menuItem_cmi);
function saveHandler(obj, menuItem) {
// saveDocument();
// custom function (not shown)
trace("something");
}
clone_cm = my_cm.copy();
this.menu = my_cm;
for (var i in clone_cm.customItems) {
trace("clone_cm-> "+clone_cm.customItems[i].caption);
}
for (var i in my_cm.customItems) {
trace("my_cm-> "+my_cm.customItems[i].caption);
}
218 Chapter 2: ActionScript Language Reference
ContextMenu.customItems
Availability
Flash Player 7.
Usage
my_cm.customItems:Array
Description
Property; an array of ContextMenuItem objects. Each object in the array represents a context
menu item that you have defined. Use this property to add, remove, or modify these custom
menu items.
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",
emailHandler);
my_cm.customItems.push(menuItem_cmi);
email_mc.menu = my_cm;
function emailHandler() {
trace("sending email");
}
See also
Button.menu, ContextMenu class, MovieClip.menu, Te x tFi el d. m en u
ContextMenu.hideBuiltInItems() 219
ContextMenu.hideBuiltInItems()
Availability
Flash Player 7.
Usage
my_cm.hideBuiltInItems() : Void
Parameters
None.
Returns
Nothing.
Description
Method; hides all built-in menu items (except Settings) in the specified ContextMenu object. If
the Flash Debug Player is running, the Debugging menu item shows, although it is dimmed for
SWF files that don’t have remote debugging enabled.
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).
Example
The following example creates a new ContextMenu object named
my_cm
whose built-in menu
items are hidden, except for Print. The menu object is attached to the current Timeline.
var my_cm:ContextMenu = new ContextMenu();
my_cm.hideBuiltInItems();
my_cm.builtInItems.print = true;
this.menu = my_cm;
220 Chapter 2: ActionScript Language Reference
ContextMenu.onSelect
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
.
Returns
Nothing.
Description
Event handler; called when a user invokes the Flash Player context menu, but before the menu is
actually displayed. This lets you customize the contents of the context menu based on the current
application state.
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);
}
if(obj instanceof Button) {
trace("Button: " + obj);
}
}
my_cm.onSelect = menuHandler;
my_mc.menu = my_cm;
my_btn.menu = my_cm;
ContextMenuItem class 221
ContextMenuItem class
Availability
Flash Player 7.
Description
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
Method Description
ContextMenuItem.copy()
Returns a copy of the specified ContextMenuItem object.
Property Description
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.
Event handler Description
ContextMenuItem.onSelect
Invoked when the menu item is selected.
CHAPTER 2
ActionScript Language Reference
222 Chapter 2: ActionScript Language Reference
Constructor for the ContextMenuItem class
Availability
Flash Player 7.
Usage
new ContextMenuItem(caption:String,
callbackFunction:Function,
[ separatorBefore:Boolean,
[ enabled:Boolean,
[ 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.
Returns
A reference to a ContextMenuItem object.
Description
Constructor; creates a new ContextMenuItem object that can be added to the
ContextMenu.customItems
array.
Example
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) {
trace("Starting...");
}
this.menu = my_cm;
ContextMenuItem.caption 223
ContextMenuItem.caption
Availability
Flash Player 7.
Usage
menuItem_cmi.caption:String
Description
Property; a string that specifies the menu item caption (text) displayed in the context menu.
Example
The following example displays the caption for the selected menu item (Pause Game) in the
Output panel:
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;
224 Chapter 2: ActionScript Language Reference
ContextMenuItem.copy()
Availability
Flash Player 7.
Usage
menuItem_cmi.copy() : ContextMenuItem
Returns
A ContextMenuItem object.
Description
Method; creates and returns a copy of the specified ContextMenuItem object. The copy includes
all properties of the original object.
Example
This example creates a new ContextMenuItem object named
original_cmi
with the caption
Pause and a callback handler set to the function
onPause
. The example then creates a copy of the
ContextMenuItem object and assigns it to the variable
copy_cmi
.
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;
ContextMenuItem.enabled 225
ContextMenuItem.enabled
Availability
Flash Player 7.
Usage
menuItem_cmi.enabled:Boolean
Description
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",
startHandler);
startMenuItem.enabled = true;
my_cm.customItems.push(startMenuItem);
var stopMenuItem:ContextMenuItem = new ContextMenuItem("Stop", stopHandler,
true);
stopMenuItem.enabled = false;
my_cm.customItems.push(stopMenuItem);
function stopHandler(obj, item) {
trace("Stopping... "+getTimer()+"ms");
startMenuItem.enabled = true;
stopMenuItem.enabled = false;
}
function startHandler(obj, item) {
trace("Starting... "+getTimer()+"ms");
startMenuItem.enabled = false;
stopMenuItem.enabled = true;
}
this.menu = my_cm;