AS2 에서의 컨텍스트 메뉴 추가하기

새로운 버전의 플래시 플레이어에서는 ContextMenu와 관련한 두 개의 새로운 클래스를 제공합니다. ContextMenu class 와 ContextMenuItem class 의 두 가지입니다.(Flash Player 7에서만 제공)

이 두 가지의 클래스를 이용하면 플래시 무비에서 마우스 왼쪽버튼을 클릭할 경우에 나오는 메뉴의 항목과 역할을 조정할 수 있습니다.


위의 보기에 쓰인 스크립트입니다.

var mainMenu = new ContextMenu();
//새로운 메뉴 클래스를 등록합니다.
mainMenu.hideBuiltInItems();
//hideBuiltInItems() 메써드를 이용해서 내장된 메뉴아이템을 숨깁니다.
mainNewItem = new ContextMenuItem(“Send e-mail”, emailHandler);
//새로운 ContextMenuItem 객체를 선언하고 메뉴에 나타날 항목과 이때 실행될 함수를 적어줍니다.
function emailHandler() {
return null;
}
//위에 적힌 emailHandler함수를 선언합니다.
//이 함수가 선언되지 않으면 위의 컨텍스트 메뉴 아이템을 올바로 적어도
//새로운 메뉴가 나타나지 않습니다.
mainMenu.customItems.push(mainNewItem);
//mainMenu.customItems는 Array 형태이므로 push메써드를 이용해
//새롭게 만든 클래스를 추가합니다.
_root.menu = mainMenu;
//메인무비의 메뉴 클래스를 변경합니다.
var ballMenu = new ContextMenu();
ballMenu.hideBuiltInItems();
ballNewItem = new ContextMenuItem(“Move this ball”, moveX);
function moveX(mc) {
trace(“move”+[mc]);
trace([mc._name]);
mc._x += 2;
}//위의 클래스와 거의 같지만 이번에는
//함수를 이용해 해당하는 무비를 움직일 수 있도록 합니다.
ballMenu.customItems.push(ballNewItem);
_root.ball.menu = ballMenu;
//ball 인트턴스의 메뉴 클래스를 변경합니다.

아래는 무비를 실행시킨 후 변수를 모두 출력하도록 한 결과입니다.
어떤 객체가 새로 추가되었는지 알 수 있습니다.

Level #0:
Variable _level0.$version = “WIN 7,0,14,0″
Variable _level0.emailHandler = [function 'onSelect']
Variable _level0.moveX = [function 'onSelect']
Variable _level0.mainMenu = [object #3, class 'ContextMenu'] {
onSelect:undefined,
builtInItems:[object #4, class 'Object'] {
print:false,
forward_back:false,
rewind:false,
loop:false,
play:false,
quality:false,
zoom:false,
save:false
},
customItems:[object #5, class 'Array'] [
0:[object #6, class 'ContextMenuItem'] {
caption:”Send e-mail”,
onSelect:[function 'onSelect'],
separatorBefore:false,
enabled:true,
visible:true
}
]
}
Variable _level0.mainNewItem = [object #6, class 'ContextMenuItem']
Variable _level0.menu = [object #3, class 'ContextMenu']
Variable _level0.ballMenu = [object #7, class 'ContextMenu'] {
onSelect:undefined,
builtInItems:[object #8, class 'Object'] {
print:false,
forward_back:false,
rewind:false,
loop:false,
play:false,
quality:false,
zoom:false,
save:false
},
customItems:[object #9, class 'Array'] [
0:[object #10, class 'ContextMenuItem'] {
caption:”Move this ball”,
onSelect:[function 'onSelect'],
separatorBefore:false,
enabled:true,
visible:true
}
]
}
Variable _level0.ballNewItem = [object #10, class 'ContextMenuItem']
Movie Clip: Target=”_level0.ball”
Variable _level0.ball.menu = [object #7, class 'ContextMenu']


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. A context menu can contain no more than 15 custom menu items.

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.
Property summary for the ContextMenu class
ContextMenu.builtInItems ; An object whose members correspond to built-in context menu items.
ContextMenu.customItems ; An array, undefined by default, that contains ContextMenuItem objects.
Event handler summary for the ContextMenu class
ContextMenu.onSelect ; Invoked before the menu is displayed.

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) 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 = new ContextMenu();
newMenu.hideBuiltInItems();
_root.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.

var showItem = false; // Change this to true to see its effect
my_cm = 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) {
}
_root.menu = my_cm;

See also
Button.menu, ContextMenu.onSelect, ContextMenu.customItems, ContextMenu.hideBuiltInItems(), MovieClip.menu, TextField.menu


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’s displayed in the context menu and a callback handler (a function) that’s 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 the Flash Player 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
ContextMenuItem.copy() ; Returns a copy of the specified ContextMenuItem object.

Property summary for the ContextMenuItem class
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 or not.

Event handler summary for the ContextMenuItem class
ContextMenuItem.onSelect ; Invoked when the menu item is selected.

Constructor for the ContextMenuItem class
Availability ; Flash Player 7.

Usage
new ContextMenuItem(caption, callbackFunction, [ separatorBefore, [ enabled, [ visible ] ] ] )

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. This parameter is optional; its default value is false.

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

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

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 root Timeline.

my_cm = 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…”);
}
_root.menu = my_cm;