Creating Variables
Số trang: 8
Loại file: pdf
Dung lượng: 23.92 KB
Lượt xem: 2
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tạo biến Bạn đã tạo ra nhiều biến đổi trong bài học 1-5. Bài học ở đây trong 6, chúng tôi chính thức giới thiệu các biến số và cách chúng được tạo ra. Mỗi dòng thời gian trong dự án của bạn có thể chứa các thiết lập của riêng mình của các biến-container
Nội dung trích xuất từ tài liệu:
Creating Variables < Day Day Up >Creating VariablesYou have created many variables in Lessons 1–5. Here in Lesson 6, we formallyintroduce variables and how they are created.Each timeline in your project can contain its own set of variables—containers, so tospeak, for storing information (data). When you create variables, you must give themnames—monikers you cannot change, though you can alter their contents. Heres thesyntax for creating a variable:var myFullName:String = Jobe Makar;The script declares a new variable, associates a data type with that variable, and gives thevariable data to store.By using var followed by a space, you indicate that the next string of letters is thedeclaration of a variable. After the variable name (the string of letters after var) you add acolon. The Actions panel gives you a drop-down list of data types you can add after thecolon, or you can type the data type yourself. The assignment operator (=) sets what isfound to its right as the value of the variable. Above, Jobe Makar is the value ofmyFullName.NOTEAs you learned in Lesson 1, Introducing ActionScript, by associating a data type with avariable you allow the Flash player to assist you in debugging scripts. If you declare avariable as a string but later attempt to assign a number as a value, the Flash player willcatch this error and report it to you when you export your movie (at compile time).You can also declare a variable without giving it a value. For example:var myFullName:String;The script tells the Flash Player that when the variable myFullName is used at some pointin the future, it should have a String value.It is also possible to create a variable this way:myFullName = Jobe Makar;This script is a valid way to create a variable. However, creating a variable in this manneris no longer recommended. It is now recommended to use the var syntax so that you canmake full use of data typing and for good memory management.NOTEYou can name a variable anything, as long as you follow some simple rules. The namemust begin with a character or underscore, and it cannot contain spaces, specialcharacters (@, #, $, %, and so on), or punctuation marks. Even though you might notreceive an error from Flash if you use a special character for a variable name, it can stillcause unplanned behavior.TIPName your variables according to the data they contain—for example, numberOfDays,favoriteDogsName, totalTax, and so on—so you can remember and use them easilythroughout your project.Once you create a variable and assign it a value, you can use that value in any scriptsimply by referencing its name. For example:var myFavoriteNumber:Number = 66;This code creates a variable named myFavoriteNumber and assigns it a value of 66. Touse that value in a script, you use syntax like this:myButton_btn.onRelease = function() { gotoAndStop (myFavoriteNumber); // Moves the timeline to frame 66 cat_mc._xscale = myFavoriteNumber; // Scale cat movie clip instance 66% vertically};Variables derive their power from universally available data—that is, information thatcan be used in any script. If the value of your variable changes, the scripts that use it willexecute differently because some of the actions are based on the current value of thatvariable.The three main types of variable values are String, Boolean, and Number. A String is atext value. For example, var myFullName:String = Jobe Makar has the String valueJobe Makar. Strings are most often used to store text, such as in the example varlatestNews:String = We just got a new dog! String values are defined using quotationmarks (). The syntax var myFavoriteNumber:String = 27 assigns a text value of 27to myFavoriteNumber, not a number value of 27.A Boolean value is a true or false value such as the following:var playMusic:Boolean = true;In programming, Boolean values are often used to indicate whether something is on oroff: A script can look at the Booleans current state (on or off, true or false) and actaccordingly. For example, if you were to create a music toggle button for a Flash movie,you might want to set a Boolean variable to store the musics state—on or off (varmusicPlaying:Boolean = true or var musicPlaying:Boolean = false). You could attach ascript to the button so that when clicked, it would check to see if the music was on or off(true or false). If the music were currently on (true), the idea would be to turn the musicoff and then switch the value of the variable to false. If the music were off (false), themusic would need to be turned on and the value of the variable set to true. Because thevalue of musicPlaying is switched between true and false with each successive buttonclick, the script on the button would evaluate its current value and turn the music on oroff.NOTEIn Lesson 8, Using Conditional Logic, well cover Boolean values and their uses inmore depth.A Number value is just that, a number. Numbers are used to store numeric values—oftenfor mathematical use. You can use numeric values for peoples ages, for scores in a game,and to track the number of times someone has clicked a button—to name just a few uses.Heres how you would create a variable and assign a number value to it:var radius:Number = 32;Instead of assigning direct values (or literals)—for example, 32, dog, or somethingelse—to variables, you can use an expression to set the value. An expression is aphrase—or a collection of variables, numbers, text, and operators—that evaluates to astring, number, or Boolean value. For example:var bottlesOfBeerOnTheWall:Number = 99;var oneFellDown:Number = 1;var bottlesLeft:Number = bottlesOfBeerOnTheWall - oneFellDown;The third line in this script uses an expression to set the value of bottlesLeft. Theexpression substitutes the va ...
Nội dung trích xuất từ tài liệu:
Creating Variables < Day Day Up >Creating VariablesYou have created many variables in Lessons 1–5. Here in Lesson 6, we formallyintroduce variables and how they are created.Each timeline in your project can contain its own set of variables—containers, so tospeak, for storing information (data). When you create variables, you must give themnames—monikers you cannot change, though you can alter their contents. Heres thesyntax for creating a variable:var myFullName:String = Jobe Makar;The script declares a new variable, associates a data type with that variable, and gives thevariable data to store.By using var followed by a space, you indicate that the next string of letters is thedeclaration of a variable. After the variable name (the string of letters after var) you add acolon. The Actions panel gives you a drop-down list of data types you can add after thecolon, or you can type the data type yourself. The assignment operator (=) sets what isfound to its right as the value of the variable. Above, Jobe Makar is the value ofmyFullName.NOTEAs you learned in Lesson 1, Introducing ActionScript, by associating a data type with avariable you allow the Flash player to assist you in debugging scripts. If you declare avariable as a string but later attempt to assign a number as a value, the Flash player willcatch this error and report it to you when you export your movie (at compile time).You can also declare a variable without giving it a value. For example:var myFullName:String;The script tells the Flash Player that when the variable myFullName is used at some pointin the future, it should have a String value.It is also possible to create a variable this way:myFullName = Jobe Makar;This script is a valid way to create a variable. However, creating a variable in this manneris no longer recommended. It is now recommended to use the var syntax so that you canmake full use of data typing and for good memory management.NOTEYou can name a variable anything, as long as you follow some simple rules. The namemust begin with a character or underscore, and it cannot contain spaces, specialcharacters (@, #, $, %, and so on), or punctuation marks. Even though you might notreceive an error from Flash if you use a special character for a variable name, it can stillcause unplanned behavior.TIPName your variables according to the data they contain—for example, numberOfDays,favoriteDogsName, totalTax, and so on—so you can remember and use them easilythroughout your project.Once you create a variable and assign it a value, you can use that value in any scriptsimply by referencing its name. For example:var myFavoriteNumber:Number = 66;This code creates a variable named myFavoriteNumber and assigns it a value of 66. Touse that value in a script, you use syntax like this:myButton_btn.onRelease = function() { gotoAndStop (myFavoriteNumber); // Moves the timeline to frame 66 cat_mc._xscale = myFavoriteNumber; // Scale cat movie clip instance 66% vertically};Variables derive their power from universally available data—that is, information thatcan be used in any script. If the value of your variable changes, the scripts that use it willexecute differently because some of the actions are based on the current value of thatvariable.The three main types of variable values are String, Boolean, and Number. A String is atext value. For example, var myFullName:String = Jobe Makar has the String valueJobe Makar. Strings are most often used to store text, such as in the example varlatestNews:String = We just got a new dog! String values are defined using quotationmarks (). The syntax var myFavoriteNumber:String = 27 assigns a text value of 27to myFavoriteNumber, not a number value of 27.A Boolean value is a true or false value such as the following:var playMusic:Boolean = true;In programming, Boolean values are often used to indicate whether something is on oroff: A script can look at the Booleans current state (on or off, true or false) and actaccordingly. For example, if you were to create a music toggle button for a Flash movie,you might want to set a Boolean variable to store the musics state—on or off (varmusicPlaying:Boolean = true or var musicPlaying:Boolean = false). You could attach ascript to the button so that when clicked, it would check to see if the music was on or off(true or false). If the music were currently on (true), the idea would be to turn the musicoff and then switch the value of the variable to false. If the music were off (false), themusic would need to be turned on and the value of the variable set to true. Because thevalue of musicPlaying is switched between true and false with each successive buttonclick, the script on the button would evaluate its current value and turn the music on oroff.NOTEIn Lesson 8, Using Conditional Logic, well cover Boolean values and their uses inmore depth.A Number value is just that, a number. Numbers are used to store numeric values—oftenfor mathematical use. You can use numeric values for peoples ages, for scores in a game,and to track the number of times someone has clicked a button—to name just a few uses.Heres how you would create a variable and assign a number value to it:var radius:Number = 32;Instead of assigning direct values (or literals)—for example, 32, dog, or somethingelse—to variables, you can use an expression to set the value. An expression is aphrase—or a collection of variables, numbers, text, and operators—that evaluates to astring, number, or Boolean value. For example:var bottlesOfBeerOnTheWall:Number = 99;var oneFellDown:Number = 1;var bottlesLeft:Number = bottlesOfBeerOnTheWall - oneFellDown;The third line in this script uses an expression to set the value of bottlesLeft. Theexpression substitutes the va ...
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet phần mềm ứng dụng lập trình SQL HTML sever web XMLTài liệu liên quan:
-
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 278 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 258 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 257 1 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 256 0 0 -
47 trang 242 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 240 0 0 -
80 trang 228 0 0
-
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 1
122 trang 218 0 0 -
122 trang 217 0 0
-
Giáo trình môn học/mô đun: Mạng máy tính (Ngành/nghề: Quản trị mạng máy tính) - Phần 1
68 trang 213 0 0