Tài liệu Flash: ActionScript Language Reference- P4 ppt

100 341 1
Tài liệu Flash: ActionScript Language Reference- P4 ppt

Đ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

for 301 for Availability Flash Player 5. Usage for(init; condition; next) { statement(s); } Parameters init An expression to evaluate before beginning the looping sequence; usually an assignment expression. A var statement is also permitted for this parameter. condition An expression that evaluates to true or false . The condition is evaluated before each loop iteration; the loop exits when the condition evaluates to false . next An expression to evaluate after each loop iteration; usually an assignment expression using the increment ( ++) or decrement ( -- ) operators. statement(s) An instruction or instructions to execute within the body of the loop. Description Statement; evaluates the init (initialize) expression once and then starts a looping sequence. The looping sequence begins by evaluating the condition expression. If the condition expression evaluates to true, statement is executed and the next expression is evaluated. The looping sequence then begins again with the evaluation of the condition expression. The curly braces ( {} ) used to enclose the block of statements to be executed by the for statement are not necessary if only one statement will execute. Example The following example uses for to add the elements in an array: var my_array:Array = new Array(); for (var i:Number = 0; i<10; i++) { my_array[i] = (i+5)*10; //trace(my_array[i]); } trace(my_array); // output: 50,60,70,80,90,100,110,120,130,140 The following example uses for to perform the same action repeatedly. In the code, the for loop adds the numbers from 1 to 100. var sum:Number = 0; for (var i:Number = 1; i<=100; i++) { sum += i; } trace(sum); // output: 5050 CHAPTER 2 ActionScript Language Reference 302 Chapter 2: ActionScript Language Reference The following example shows that curly braces ({}) are not necessary if only one statement will execute: var sum:Number = 0; for (var i:Number = 1; i<=100; i++) sum += i; trace(sum); // output: 5050 See also ++ (increment), –– (decrement), for in, var, while, do while for in 303 for in Availability Flash Player 5. Usage for(variableIterant in object){ statement(s); } Parameters variableIterant The name of a variable to act as the iterant, referencing each property of an object or element in an array. object The name of an object to be iterated. statement(s) An instruction to execute for each iteration. Returns Nothing. Description Statement; iterates over the properties of an object or elements in an array and executes the statement for each property or element. Methods of an object are not enumerated by the for in action. Some properties cannot be enumerated by the for in action. For example, movie clip properties, such as _x and _y , are not enumerated. In external class files, static members are not enumerable, unlike instance members. The for in statement iterates over properties of objects in the iterated object’s prototype chain. Properties of the object are enumerated first, then properties of its immediate prototype, then properties of the prototype’s prototype, and so on. The for in statement does not enumerate the same property name twice. If the object child has prototype parent and both contain the property prop , the for in statement called on child enumerates prop from child but ignores the one in parent . The curly braces ( {} ) used to enclose the block of statements to be executed by the for in statement are not necessary if only one statement will execute. If you write a for in loop in a class file (an external AS file), then instance members are not available for the loop, but static members are. However, if you write a for in loop in a FLA file for an instance of the class, then instance members are available but static ones are not. Examples The following example shows using for in to iterate over the properties of an object: var myObject:Object = {name:"Tara", age:27, city:"San Francisco"}; for (var name in myObject) { trace("myObject."+name+" = "+myObject[name]); } CHAPTER 2 ActionScript Language Reference 304 Chapter 2: ActionScript Language Reference //output myObject.name = Tara myObject.age = 27 myObject.city = San Francisco The following example shows using for in to iterate over the elements of an array: var myArray:Array = new Array("one", "two", "three"); for (var index in myArray) trace("myArray["+index+"] = " + myArray[index]); // output: myArray[2] = three myArray[1] = two myArray[0] = one The following example uses the typeof operator with for in to iterate over a particular type of child: for (var name in this) { if (typeof (this[name]) == "movieclip") { trace("I have a movie clip child named "+name); } } Note: If you have several movie clips, the output consists of the instance names of those clips. The following example enumerates the children of a movie clip and sends each to Frame 2 in their respective Timelines. The RadioButtonGroup movie clip is a parent with several children, _RedRadioButton_ , _GreenRadioButton_, and _BlueRadioButton . for (var name in RadioButtonGroup) { RadioButtonGroup[name].gotoAndStop(2); } fscommand() 305 fscommand() Availability Flash Player 3. Usage fscommand("command", "parameters") Parameters command A string passed to the host application for any use, or a command passed to Flash Player. parameters A string passed to the host application for any use, or a value passed to Flash Player. Returns Nothing. Description Function; lets the SWF file communicate with either Flash Player or the program hosting Flash Player, such as a web browser. You can also use the fscommand() function to pass messages to Macromedia Director, or to Visual Basic, Visual C++, and other programs that can host ActiveX controls. Usage 1: To send a message to Flash Player, you must use predefined commands and parameters. The following table shows the values you can specify for the command and parameters parameters of the fscommand() function to control a SWF file playing in Flash Player, including projectors. Command Parameters Purpose quit None Closes the projector. fullscreen true or false Specifying true sets Flash Player to full-screen mode. Specifying false returns the player to normal menu view. allowscale true or false Specifying false sets the player so that the SWF file is always drawn at its original size and never scaled. Specifying true forces the SWF file to scale to 100% of the player. showmenu true or false Specifying true enables the full set of context menu items. Specifying false dims all the context menu items except About Flash Player. exec Path to application Executes an application from within the projector. trapallkeys true or false Specifying true sends all key events, including accelerator keys, to the onClipEvent(keyDown/keyUp) handler in Flash Player. CHAPTER 2 ActionScript Language Reference 306 Chapter 2: ActionScript Language Reference The exec command can contain only the characters A–Z, a–z, 0–9, period (.), and underscore (_). The exec command runs in the subdirectory fscommand only. In other words, if you use the fscommand exec command to call an application, the application must reside in a subdirectory named fscommand. Usage 2: To use the fscommand() function to send a message to a scripting language such as JavaScript in a web browser, you can pass any two parameters in the command and parameters parameters. These parameters can be strings or expressions and are used in a JavaScript function that handles, or catches, the fscommand() function. In a web browser, the fscommand() function calls the JavaScript function moviename_DoFScommand in the HTML page containing the SWF file. The moviename is the name of the Flash Player as assigned by the NAME attribute of the EMBED tag or the ID property of the OBJECT tag. If you assign the Flash Player the name myDocument , the JavaScript function called is myDocument_DoFScommand . Usage 3: The fscommand() function can send messages to Macromedia Director that are interpreted by Lingo (Director’s scripting language) as strings, events, or executable Lingo code. If the message is a string or an event, you must write the Lingo code to receive the message from the fscommand() function and carry out an action in Director. For more information, see the Director Support Center at www.macromedia.com/support/director. Usage 4: In Visual Basic, Visual C++, and other programs that can host ActiveX controls, the fscommand() function sends a VB event with two strings that can be handled in the environment’s programming language. For more information, use the keywords Flash method to search the Flash Support Center at www.macromedia.com/support/flash. Example In the following example, the fscommand() function sets Flash Player to scale the SWF file to the full monitor screen size when the fullscreen_btn button or unfullscreen_btn is released: this.fullscreen_btn.onRelease = function() { fscommand("fullscreen", true); }; this.unfullscreen_btn.onRelease = function() { fscommand("fullscreen", false); }; The following example uses the fscommand() function applied to a button in Flash to open a JavaScript message box in an HTML page. The message itself is sent to JavaScript as the fscommand parameter. You must add a function to the HTML page that contains the SWF file. This function, myDocument_DoFSCommand , sits in the HTML page and waits for an fscommand() function in Flash. When an fscommand is triggered in Flash (for example, when a user presses the button), the command and parameter strings are passed to the myDocument_DoFSCommand function. You can use the passed strings in your JavaScript or VBScript code in any way you like. In this example, the function contains a conditional if statement that checks to see if the command string is "messagebox" . If it is, a JavaScript alert box (or “message box”) opens and displays the contents of the parameters string. fscommand() 307 function myDocument_DoFSCommand(command, args) { if (command == "messagebox") { alert(args); } } In the Flash document, add the fscommand() function to a button: fscommand("messagebox", "This is a message box called from within Flash.") You can also use expressions for the fscommand() function and parameters, as in the following example: fscommand("messagebox", "Hello, " + name + ", welcome to our website!") To test the SWF file, select File > Publish Preview > HTML. Note: If you publish your SWF file using the Flash with FSCommand template in the HTML tab of the Publish Settings dialog box, the myDocument_DoFSCommand function is inserted automatically. The SWF file’s NAME and ID attributes will be the filename. For example, for the file myDocument.fla, the attributes would be set to myDocument . 308 Chapter 2: ActionScript Language Reference function Availability Flash Player 5. Usage function functionname ([parameter0, parameter1, .parameterN]){ statement(s) } function ([parameter0, parameter1, .parameterN]){ statement(s) } Parameters functionname The name of the new function. This parameter is optional. parameter An identifier that represents a parameter to pass to the function. This parameter is optional. statement(s) Any ActionScript instruction you have defined for the body of the function . Returns Usage 1: Nothing. Usage 2: A reference to the anonymous function. Description Statement; comprises a set of statements that you define to perform a certain task. You can define a function in one location and invoke, or call, it from different scripts in a SWF file. When you define a function, you can also specify parameters for the function. Parameters are placeholders for values on which the function operates. You can pass different parameters to a function each time you call it so you can reuse a function in different situations. Use the return statement in a function’s statement(s) to cause a function to generate, or return, a value. You can use this statement to define a function with the specified functionname , parameters , and statement(s) . When a script calls a function, the statements in the function’s definition are executed. Forward referencing is permitted; within the same script, a function may be declared after it is called. A function definition replaces any prior definition of the same function. You can use this syntax wherever a statement is permitted. You can also use this statement to create an anonymous function and return a reference to it. This syntax is used in expressions and is particularly useful for installing methods in objects. For additional functionality, you can use the arguments object in your function definition. Some common uses of the arguments object are creating a function that accepts a variable number of parameters and creating a recursive anonymous function. CHAPTER 2 ActionScript Language Reference function 309 Example The following example defines the function sqr , which accepts one parameter and returns the Math.pow(x, 2) of the parameter: function sqr(x:Number) { return Math.pow(x, 2); } var y:Number = sqr(3); trace(y); // output: 9 If the function is defined and used in the same script, the function definition may appear after using the function: var y:Number = sqr(3); trace(y); // output: 9 function sqr(x:Number) { return Math.pow(x, 2); } The following function creates a LoadVars object and loads params.txt into the SWF file. When the file successfully loads, variables loaded traces: var myLV:LoadVars = new LoadVars(); myLV.load("params.txt"); myLV.onLoad = function(success:Boolean) { trace("variables loaded"); } 310 Chapter 2: ActionScript Language Reference Function class Availability Flash Player 6. Description Both user-defined and built-in functions in ActionScript are represented by Function objects, which are instances of the Function class. Method summary for the Function class Method Description Function.apply() Invokes the function represented by a Function object, with parameters passed in through an array. Function.call() Invokes the function represented by a Function object. CHAPTER 2 ActionScript Language Reference [...]... See also import #include 331 Infinity CHAPTER 2 ActionScript Language Reference Availability Flash Player 5 Usage Infinity Description Constant; specifies the IEEE-754 value representing positive infinity The value of this constant is the same as Number.POSITIVE_INFINITY 332 Chapter 2: ActionScript Language Reference #initclip CHAPTER 2 ActionScript Language Reference Availability Flash Player 6 Usage... "Very good, you hit the button in "+difference+" seconds."; } }; See also else 326 Chapter 2: ActionScript Language Reference implements CHAPTER 2 ActionScript Language Reference Availability Flash Player 6 Usage myClass implements interface01 [, interface02, ] 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... macr.util.foo();) or add an import statement to the other frame that imports the classes in that package 328 Chapter 2: ActionScript Language Reference For more information on importing, see “Importing classes” and “Using packages” in Using ActionScript in Flash import 329 #include CHAPTER 2 ActionScript Language Reference Availability Flash Player 4 Usage #include "[path] filename.as" Note: Do not place a semicolon... obj); The trace() statement displays: this == obj? true See also Function.apply() 314 Chapter 2: ActionScript Language Reference CHAPTER 2 ActionScript Language Reference get Availability Flash Player 6 Usage function get property() { // your statements 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... trace("Instance \""+i+"\" is a TextInput component instance."); } } See also typeof 334 Chapter 2: ActionScript Language Reference interface CHAPTER 2 ActionScript Language Reference Availability Flash Player 6 Usage interface InterfaceName [extends InterfaceName ] {} 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... following ActionScript in a frame on the Timeline: var giants:Team = new Team("San Fran", "SFO"); trace(giants.name); giants.name = "San Francisco"; trace(giants.name); /* output: San Fran San Francisco */ When you trace giants.name, you use the get method to return the value of the property See also Object.addProperty(), set 316 Chapter 2: ActionScript Language Reference getProperty() CHAPTER 2 ActionScript. .. implemented For more information, see “Interfaces as data types” in Using ActionScript in Flash Example See interface See also class, extends, interface implements 327 CHAPTER 2 ActionScript Language Reference import Availability Flash Player 6 Usage import className import packageName.* Note: To use this keyword, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash tab of your FLA... this.getNextHighestDepth(), 0, 0, 100, 22); function updateTimer():Void { timer_txt.text = getTimer(); } var intervalID:Number = setInterval(updateTimer, 100); 318 Chapter 2: ActionScript Language Reference CHAPTER 2 ActionScript Language Reference getURL() Availability Flash 2 The GET and POST options are available only in Flash Player 4 and later versions Usage getURL(url:String [, window:String [,... Settings\user\Local Settings\ Application Data\Macromedia\Flash MX 2004 \language\ Configuration\Include Windows 98: C:\Windows\Application Data\Macromedia\Flash MX 2004\ language\ Configuration\Include Macintosh OS X: Hard Drive/Users/Library/Application Support/Macromedia/ Flash MX 2004 /language/ Configuration/Include • The Flash MX 2004 program \language\ First Run\Include directory; if you save a file here, it... _global object CHAPTER 2 ActionScript Language Reference Availability Flash Player 6 Usage _global.identifier Parameters None Returns A reference to the global object that holds the core ActionScript classes, such as String, Object, Math, and Array Description Identifier; creates global variables, objects, or classes For example, you could create a library that is exposed as a global ActionScript object, . param_name; } } CHAPTER 2 ActionScript Language Reference 316 Chapter 2: ActionScript Language Reference Enter the following ActionScript in a frame on. trace("myObject."+name+" = "+myObject[name]); } CHAPTER 2 ActionScript Language Reference 304 Chapter 2: ActionScript Language Reference //output myObject.name = Tara

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

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan