Flash: ActionScript Language Reference- P3

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:100

0
41
lượt xem
5
download

Flash: ActionScript Language Reference- P3

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Tham khảo tài liệu 'flash: actionscript language reference- p3', công nghệ thông tin, đồ họa - thiết kế - flash phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả

Chủ đề:
Lưu

Nội dung Text: Flash: ActionScript Language Reference- P3

  1. CHAPTER 2 ActionScript Language Reference 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"); } case 201
  2. See also break, default, === (strict equality), switch 202 Chapter 2: ActionScript Language Reference
  3. CHAPTER 2 ActionScript Language Reference 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. class 203
  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. 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]; } 204 Chapter 2: ActionScript Language Reference
  5. }; 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("http://www.macromedia.com/devnet/mx/ 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() class 205
  6. 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() 206 Chapter 2: ActionScript Language Reference
  7. CHAPTER 2 ActionScript Language Reference 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 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. 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); Color class 207
  8. 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() 208 Chapter 2: ActionScript Language Reference
  9. 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() Color.getTransform() 209
  10. 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() 210 Chapter 2: ActionScript Language Reference
  11. 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} Color.setTransform() 211
  12. 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); 212 Chapter 2: ActionScript Language Reference
  13. CHAPTER 2 ActionScript Language Reference 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. ContextMenu class 213
  14. Property summary for the ContextMenu class 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. Event handler summary for the ContextMenu class Property Description 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) 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. 214 Chapter 2: ActionScript Language Reference
  15. 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 ContextMenu class 215
  16. 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); } 216 Chapter 2: ActionScript Language Reference
  17. 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); } ContextMenu.copy() 217
  18. 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, TextField.menu 218 Chapter 2: ActionScript Language Reference
  19. 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; ContextMenu.hideBuiltInItems() 219
  20. 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; 220 Chapter 2: ActionScript Language Reference
Đồng bộ tài khoản