Danh mục

Creating Functions

Số trang: 7      Loại file: pdf      Dung lượng: 21.74 KB      Lượt xem: 8      Lượt tải: 0    
10.10.2023

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

code này biểu diễn cách phổ biến nhất của việc tạo ra một chức năng, cũng như cú pháp của chúng tôi sẽ sử dụng thường xuyên nhất trong bài học này. Bạn sẽ thấy rằng việc khai báo chức năng bắt đầu với chức năng từ, theo sau là tên của hàm (mà có thể được bất cứ điều gì bạn chọn, miễn là nó sau điển hình ước đặt tên).
Nội dung trích xuất từ tài liệu:
Creating Functions < Day Day Up >Creating FunctionsBefore you use a function, you must create or define it. You can do this by using one oftwo possible syntaxes.Syntax 1This code describes the first syntax:function myFunction (parameter1:DataType,parameter2:DataType,etc.) { //actions go here;}The code represents the most common way of creating a function, as well as the syntaxwell use most frequently in this lesson. Youll see that the function declaration beginswith the word function, followed by the name of the function (which can be anything youchoose, as long as it follows typical naming conventions). Following the function name isan area enclosed by parentheses. You can leave this area blank, or you can fill it withinformation (parameter data) that the function can use. By leaving the area within theparentheses blank, you create a generic function—that is, one that performs the sameway whenever its called (used). If your function contains parameters, it will perform in aunique way each time its called based on the parameter information it receives. Givingthe function information in this way is called passing in arguments or passing inparameters. You can use as many parameters as you want. Well tell you how to useparameter data a bit later in this lesson.Following the optional parameter area is an open curly brace, followed by a carriagereturn and then some actions before the curly brace that concludes the function. In thespace between the curly braces, you write the actions that you want your function toperform. These actions can also make use of any parameter information passed to thefunction (as you will see soon).TIPYou can create a function skeleton (that is, a function that does not yet contain anyactions) in the Actions panel by clicking Statements > User-Defined Functions, thendouble-clicking the function command.Syntax 2This code represents the second syntax for creating functions:myFunction = function (parameter1:DataType,parameter2:DataType,etc.) {/* actions go here */};You would use this syntax to create a function dynamically or to define your own custommethod of an object (which well discuss in Lesson 7, Creating Custom Classes). Theonly difference between this syntax and Syntax 1 is in the way the function name isassigned: the function name appears first, and the syntax for defining how the functionwill work follows the = assignment operator.Now that you understand the basic syntax for creating/defining a function, lets look athow a function is used, or called.If a function contains no parameters, it can be called using the following syntax:myFunction();When you call a function, youre telling Flash to execute all the actions within thatfunction. If myFunction() contained 20 actions, all of them could be executed by usingthis single line of script.If a function has been defined to accept parameter data, you can use the following syntaxto call it:myFunction(parameter1, parameter2);If parameter1 had a value of cat and parameter2 had a value of 36, then those two valueswould be sent to the function, for use by the actions within the function definition. At alater time, the same function could be called, but different parameter values sent. Thiswould result in the same functions working in a slightly different way from the firstexample, because the actions within the function would be making use of differentparameter values.The foregoing examples assume that the function and function call reside on the sametimeline. Just as each timeline contains its own variables and objects, each timeline alsocontains any functions youve defined there. To call a function on a specific timeline, youneed to place the target path to that timeline in front of the function call, like this:_root.clip1.clip2.myFunction();In this exercise, youll create a Power button on a television remote control. With thisbutton, the Flash-TV can be toggled on and off using a function.TIPA function can be called dynamically based on a value—for instance,_root[aVariableName]();. Thus, if aVariableName had a value of sayHello, the functioncall would actually look like _root.sayHello();. 1. Open television1.fla in the Lesson05/Assets folder. The movies structure has already been created. The Actions layer is where you will include all of the ActionScript for this exercise. On the TV layer, a movie clip instance named tv_mc has three layers on its timeline: the bottom layer (Television) is a graphic, and the layer above that (Screen) contains a movie clip instance called screen_mc (which is then masked by the layer above that). The screen_mc movie clip instance itself includes two layers and two frames, and contains graphical content that represents the various programs seen when changing channels on the TV. On the main timeline, a layer named Remote contains a movie clip instance named remote_mc. Inside remote_mc, youll find a layer that contains most of the remote- control graphics, including a movie clip with an instance name of light_mc, as well as another layer that contains the buttons for the remote. The numbered buttons have instance names of channel1_btn through channel6_btn. Under the numbered buttons are the Up and Down channel buttons whose instance names are up_btn and down_btn, respectively. The bottom button, Power, has an instance name of power_btn.2. Select Frame 1 of the Actions layer on the main timeline. With the Actions panel open, add the script:3.4. var tvPower:Boolean = false;5.6. function togglePower() {7.8. var newChannel:Number;9.10. if (tvPower) {11.12. newChannel = 0;13.14. tvPower = false;15.16. } else {17.18. newChannel = 1;19.20. tvPower = true;21. ...

Tài liệu được xem nhiều: