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

Tài liệu Flash: ActionScript Language Reference- P2 pptx

100 433 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

Định dạng
Số trang 100
Dung lượng 342,51 KB

Nội dung

arguments.length 101 arguments.length Availability Flash Player 5. Usage arguments.length:Number Description Property; the number of parameters actually passed to a function. Example The following ActionScript includes a function called getArgLength , which returns the number of arguments that are passed into the function. Although the method expects three arguments, you can pass as many arguments as you want. function getArgLength(param_arg1:String, param_arg2:String, param_arg3:String) { return (arguments.length); } trace(getArgLength("one", "two", "three")); // output: 3 trace(getArgLength("one", "two")); // output: 2 trace(getArgLength("one", "two", "three", "four")); // output: 4 In the following example, the function called listSum adds the values passed to it and returns the sum. The function uses a for loop to examine each argument passed. If the value is a number, the value is added to the sum variable. At the end of the function, the value of sum is returned. function listSum():Number { var sum:Number = 0; for (var i = 0; i<arguments.length; i++) { if (!isNaN(Number(arguments[i]))) { sum += parseFloat(arguments[i]); } } return sum; } trace(listSum(100, 200, 300, 7)); // output: 607 102 Chapter 2: ActionScript Language Reference Array() Availability Flash Player 6. Usage Array() : Array Array(numElements:Number) : Array Array( [element0:Object [, element1 , element2, .elementN ] ]) : Array Parameters element One or more elements to place in the array. Returns An array. Description Conversion function; creates a new, empty array or converts specified elements to an array. Using this function is similar to creating an array with the Array constructor (see “Constructor for the Array class” on page 104). Example Usage 1: The following example adds items to an array by using the array’s index and then by using the Array class’s push method: var myArray:Array = Array(); myArray.push(12); trace(myArray); //traces 12 myArray[4] = 7; trace(myArray); //traces 12,undefined,undefined,undefined,7 Usage 2: The following example creates an array of length 4 but with no elements defined: var myArray:Array = Array(4); trace(myArray.length); // traces 4 trace(myArray); // traces undefined,undefined,undefined,undefined Usage 3: The following example creates an array with three defined elements: var myArray:Array = Array(["firstElement", "secondElement", "thirdElement"]); trace (myArray); // traces firstElement,secondElement,thirdElement Note: Unlike the Array class constructor, the Array() function does not use the keyword new. See also Array class CHAPTER 2 ActionScript Language Reference Array class 103 Array class Availability Flash Player 5 (became a native object in Flash Player 6, which improved performance significantly). Description The Array class lets you access and manipulate arrays. An array is an object whose properties are identified by a number representing their position in the array. This number is referred to as the index. All arrays are zero-based, which means that the first element in the array is [0], the second element is [1], and so on. To create an Array object, you use the constructor new Array() . To access the elements of an array, you use the array access ( [] ) operator. In the following example, my_array contains four months of the year: var my_array:Array = new Array(); my_array[0] = "January"; my_array[1] = "February"; my_array[2] = "March"; my_array[3] = "April"; Method summary for the Array class Property summary for the Array class Method Description Array.concat() Concatenates the parameters and returns them as a new array. Array.join() Joins all elements of an array into a string. Array.pop() Removes the last element of an array and returns its value. Array.push() Adds one or more elements to the end of an array and returns the array’s new length. Array.reverse() Reverses the direction of an array. Array.shift() Removes the first element from an array and returns its value. Array.slice() Extracts a section of an array and returns it as a new array. Array.sort() Sorts an array in place. Array.sortOn() Sorts an array according to a field in the array. Array.splice() Adds and removes elements from an array. Array.toString() Returns a string value representing the elements in the Array object. Array.unshift() Adds one or more elements to the beginning of an array and returns the array’s new length. Property Description Array.length A non-negative integer specifying the number of elements in the array. CHAPTER 2 ActionScript Language Reference 104 Chapter 2: ActionScript Language Reference Constructor for the Array class Availability Flash Player 5. Usage new Array() : Array new Array(length:Number) : Array new Array(element0, element1, element2, .elementN) : Array Parameters length An integer that specifies the number of elements in the array. element0 .elementN A list of two or more arbitrary values. The values can be of type Boolean, Number, String, Object, or Array. The first element in an array always has an index or position of 0. Note: If only a single numeric parameter is passed to the Array constructor, it is assumed to be length and it is converted to an integer using the Integer() function. Returns A reference to an Array object. Description Constructor; lets you create an array. You can use the constructor to create different types of arrays: an empty array, an array with a specific length but whose elements have undefined values, or an array whose elements have specific values. Usage 1: If you don’t specify any parameters, an array with a length of 0 is created. Usage 2: If you specify only a length, an array is created with length number of elements. Each element’s value is set to undefined . Usage 3: If you use the element parameters to specify values, an array is created with specific values. Example Usage 1: The following example creates a new Array object with an initial length of 0: var my_array:Array = new Array(); trace(my_array.length); // traces 0 Usage 2: The following example creates a new Array object with an initial length of 4: var my_array:Array = new Array(4); trace(my_array.length); // returns 4 trace(my_array[0]); // returns undefined if (my_array[0] == undefined) { // no quotation marks around undefined trace("undefined is a special value, not a string"); } // traces: undefined is a special value, not a string Array class 105 Usage 3: The following example creates the new Array object go_gos_array with an initial length of 5: var go_gos_array:Array = new Array("Belinda", "Gina", "Kathy", "Charlotte", "Jane"); trace(go_gos_array.length); // returns 5 trace(go_gos_array.join(", ")); // displays elements The initial elements of the go_gos_array array are identified, as shown in the following example: go_gos_array[0] = "Belinda"; go_gos_array[1] = "Gina"; go_gos_array[2] = "Kathy"; go_gos_array[3] = "Charlotte"; go_gos_array[4] = "Jane"; The following code adds a sixth element to the go_gos_array array and changes the second element: go_gos_array[5] = "Donna"; go_gos_array[1] = "Nina" trace(go_gos_array.join(" + ")); // returns Belinda + Nina + Kathy + Charlotte + Jane + Donna See also Array.length, [] (array access) 106 Chapter 2: ActionScript Language Reference Array.concat() Availability Flash Player 5. Usage my_array.concat( [ value0:Object, value1, .valueN ]) : Array Parameters value0, .valueN Numbers, elements, or strings to be concatenated in a new array. If you don’t pass any values, a duplicate of my_array is created. Returns An array. Description Method; concatenates the elements specified in the parameters with the elements in my_array , and creates a new array. If the value parameters specify an array, the elements of that array are concatenated, rather than the array itself. The array my_array is left unchanged. Example The following code concatenates two arrays: var alpha_array:Array = new Array("a","b","c"); var numeric_array:Array = new Array(1,2,3); var alphaNumeric_array:Array =alpha_array.concat(numeric_array); trace(alphaNumeric_array); // creates array [a,b,c,1,2,3] The following code concatenates three arrays: var num1_array:Array = [1,3,5]; var num2_array:Array = [2,4,6]; var num3_array:Array = [7,8,9]; var nums_array:Array=num1_array.concat(num2_array,num3_array) trace(nums_array); // creates array [1,3,5,2,4,6,7,8,9] Nested arrays are not flattened in the same way as normal arrays. The elements in a nested array are not broken into separate elements in array x_array , as shown in the following example: var a_array:Array = new Array ("a","b","c"); // 2 and 3 are elements in a nested array var n_array:Array = new Array(1, [2, 3], 4); var x_array:Array = a_array.concat(n_array); trace(x_array[0]); // a trace(x_array[1]); // b trace(x_array[2]); // c trace(x_array[3]); // 1 trace(x_array[4]); // 2, 3 trace(x_array[5]); // 4 Array.join() 107 Array.join() Availability Flash Player 5. Usage my_array.join([separator:String]) : String Parameters separator A character or string that separates array elements in the returned string. If you omit this parameter, a comma (,) is used as the default separator. Returns A string. Description Method; converts the elements in an array to strings, inserts the specified separator between the elements, concatenates them, and returns the resulting string. A nested array is always separated by a comma (,), not by the separator passed to the join() method. Example The following example creates an array with three elements: Earth, Moon, and Sun. It then joins the array three times—first using the default separator (a comma [,] and a space), then using a dash (-), and then using a plus sign (+). var a_array:Array = new Array("Earth","Moon","Sun") trace(a_array.join()); // displays Earth,Moon,Sun trace(a_array.join(" - ")); // displays Earth - Moon - Sun trace(a_array.join(" + ")); // displays Earth + Moon + Sun The following example creates a nested array containing two arrays. The first array has three elements: Europa, Io, and Callisto. The second array has two elements: Titan and Rhea. It joins the array using a plus sign (+), but the elements within each nested array remain separated by a comma (,). var a_nested_array:Array = new Array(["Europa", "Io", "Callisto"], ["Titan", "Rhea"]); trace(a_nested_array.join(" + ")); // returns Europa,Io,Callisto + Titan,Rhea See Also String.split() 108 Chapter 2: ActionScript Language Reference Array.length Availability Flash Player 5. Usage my_array.length:Number Description Property; a non-negative integer specifying the number of elements in the array. This property is automatically updated when new elements are added to the array. When you assign a value to an array element (for example, my_array[index] = value ), if index is a number, and index+1 is greater than the length property, the length property is updated to index+1 . Note: If you assign a value to the length property that is shorter than the existing length, the array will be truncated. Example The following code explains how the length property is updated: var my_array:Array = new Array(); trace(my_array.length); // initial length is 0 my_array[0] = 'a'; trace(my_array.length); // my_array.length is updated to 1 my_array[1] = 'b'; trace(my_array.length); // my_array.length is updated to 2 my_array[9] = 'c'; trace(my_array.length); // my_array.length is updated to 10 trace(my_array); // displays: // a,b,undefined,undefined,undefined,undefined,undefined,undefined,undefined,c // if the length property is now set to 5, the array will be truncated my_array.length = 5; trace(my_array.length); // my_array.length is updated to 5 trace(my_array); // outputs: a,b,undefined,undefined,undefined Array.pop() 109 Array.pop() Availability Flash Player 5. Usage my_array.pop() : Object Parameters None. Returns The value of the last element in the specified array. Description Method; removes the last element from an array and returns the value of that element. Example The following code creates the myPets_array array containing four elements, and then removes its last element: var myPets_array:Array = new Array("cat", "dog", "bird", "fish"); var popped:String = myPets_array.pop(); trace(popped); // displays fish trace(myPets_array); // displays cat,dog,bird See Also Array.push(), Array.shift(), Array.unshift() 110 Chapter 2: ActionScript Language Reference Array.push() Availability Flash Player 5. Usage my_array.push(value, .) : Number Parameters value One or more values to append to the array. Returns An integer representing the length of the new array. Description Method; adds one or more elements to the end of an array and returns the array’s new length. Example The following example creates the array myPets_array with two elements, cat and dog . The second line adds two elements to the array. Because the push() method returns the new length of the array, the trace() statement in the last line sends the new length of myPets_array ( 4 ) to the Output panel. var myPets_array:Array = new Array("cat", "dog"); var pushed:Number = myPets_array.push("bird", "fish"); trace(pushed); // displays 4 See Also Array.pop(), Array.shift(), Array.unshift() [...]... catch finally 132 Chapter 2: ActionScript Language Reference CHAPTER 2 ActionScript Language Reference Button class Availability Flash Player 6 Description All button symbols in a SWF file are instances of the Button object You can give a button an instance name in the Property inspector, and use the methods and properties of the Button class to manipulate buttons with ActionScript Button instance... primitive value type of a new Boolean object is false: var x:Boolean = new Boolean(); trace(x.valueOf()); // false x = (6==3+3); trace(x.valueOf()); // true 130 Chapter 2: ActionScript Language Reference break CHAPTER 2 ActionScript Language Reference Availability Flash Player 4 Usage break Parameters None Returns Nothing Description Statement; appears within a loop (for, for in, do while or while) or... "fish"); trace( pets_array ); // dog,cat,fish pets_array.unshift("ferrets", "gophers", "engineers"); trace( pets_array ); // ferrets,gophers,engineers,dog,cat,fish 124 Chapter 2: ActionScript Language Reference CHAPTER 2 ActionScript Language Reference asfunction Availability Flash Player 5 Usage asfunction:function:Function,"parameter":String Parameters function parameter An identifier for a function A... abcd // barb // Bob // catchy Performing a case-insensitive, descending sort on the password field produces the following results: my_array.sortOn("password", 1|2); // catchy // Bob 118 Chapter 2: ActionScript Language Reference // barb // abcd Performing a default sort on the age field produces the following results: my_array.sortOn("age"); // 29 // 3 // 35 // 4 Performing a numeric sort on the age field... iTrack 1"; list_txt.htmlText += "Track 2"; When you click a link, the MP3 sound file streams in Flash Player asfunction 125 CHAPTER 2 ActionScript Language Reference Boolean() Availability Flash Player 5; behavior changed in Flash Player 7 Usage Boolean(expression) : Boolean Parameters expression An expression to convert to a Boolean value... // output: false trace(Boolean("true")); // output: true trace(Boolean("false")); // output: true trace(Boolean("Craiggers")); // output: true trace(Boolean("")); // output: false 126 Chapter 2: ActionScript Language Reference If files are published for Flash Player 6 or earlier, the results will differ for three of the preceding examples: trace(Boolean("true")); // output: false trace(Boolean("false"));... objects are compared by reference var a:Boolean = new Boolean("a"); // a is true var b:Boolean = new Boolean(1); // b is true trace(a==b); // false See also Boolean class Boolean() 127 CHAPTER 2 ActionScript Language Reference Boolean class Availability Flash Player 5 (became a native object in Flash Player 6, which improved performance significantly) Description The Boolean class is a wrapper object . the number of elements in the array. CHAPTER 2 ActionScript Language Reference 104 Chapter 2: ActionScript Language Reference Constructor for the Array. function does not use the keyword new. See also Array class CHAPTER 2 ActionScript Language Reference Array class 103 Array class Availability Flash Player

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

TỪ KHÓA LIÊN QUAN

w