Danh mục

Controlling a Script's Flow

Số trang: 11      Loại file: pdf      Dung lượng: 20.34 KB      Lượt xem: 2      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:

Thông thường, hành động trong kịch bản của bạn thực hiện liên tục, từ đầu đến cuối, một chuỗi đại diện dòng lệnh của bạn. Sử dụng logic có điều kiện, bạn có thể kiểm soát dòng chảy này bởi kịch bản hành động cụ thể để thực hiện chỉ khi điều kiện cụ thể được đáp ứng hoặc tồn tại trong phim của bạn. Bằng cách thực hiện điều kiện logic trong kịch bản của bạn, bạn cho phim của bạn khả năng đưa ra quyết định và hành động dựa trên các điều kiện khác nhau mà bạn...
Nội dung trích xuất từ tài liệu:
Controlling a Scripts Flow < Day Day Up >Controlling a Scripts FlowTypically, actions in your scripts execute consecutively, from beginning to end—asequence that represents your scripts flow. Using conditional logic, you can control thisflow by scripting specific actions to execute only when specific conditions are met orexist in your movie. By implementing conditional logic in your scripts, you give yourmovie the ability to make decisions and take action based on various conditions youveset, and your movie takes on more dimension as a result. Youll use the conditionalstatements or phrases described in this lesson to implement conditional logic in yourscripts.If/Then StatementsAt the heart of conditional logic is the simple if/then statement. Heres an example:if (moneySaved > 500) {buyStuff();}// next line of actions...The buyStuff() function is called only if the variable moneySaved has a value greaterthan 500. If moneySaved is equal to or less than 500, the buyStuff() function call isignored and actions immediately below the if statement are executed.At its core, a conditional statement looks at a circumstance (placed within parentheses)and determines whether that circumstance is true or false. If the circumstance is true,actions within the statement are executed; if the circumstance is false, the actions areignored. When you create an if statement, you state, essentially:if (...what is shown here is true) { Do this;}The data you place within parentheses represents the condition to be analyzed. The datawithin the curly braces ({}) represents the actions to be taken if the condition exists.As in real life, sometimes an if statement needs to analyze multiple conditions beforetaking a single action or set of actions. For example:if (moneySaved > 500 && billsPaid == true) { buyStuff();}The AND operator (&&) has been added to the statement so that now the buyStuff()function is called only if moneySaved is more than 500 and billsPaid has a value of true.If either condition is false, buyStuff() will not be called.Using the OR operator (||) allows you to take a slightly different approach:if (moneySaved > 500 || wonLottery == true) { buyStuff();}The buyStuff() function is called if either moneySaved has a value greater than 500 orwonLottery has a value of true. Both conditions need not be true for the buyStuff()function to be called, as was the case when using the AND operator (&&) in the earlierexample.You can mix the AND and OR operators to create sophisticated conditional statementslike this one:if (moneySaved > 500 && billsPaid == true || wonLottery == true) { buyStuff();}In this script, the buyStuff() function is called only if moneySaved is more than 500 andbillsPaid has a value of true, or if wonLottery has a value of true.The following table shows a list of the common operators (known as comparisonoperators because theyre used to compare values) used in conditional logic, with briefdescriptions and examples of how theyre used.OPERATOR DESCRIPTION EXAMPLE EXECUTE THE FUNCTION IF…== Checks for if (name == Derek) name has an exact value of equality Derek!= Checks for if (name != Derek) name has a value other than inequality Derek< Less than if (age < 30) age has a value less than 30> Greater than if (age > 30) age has a value greater than 30= 30) age has a value greater than equal to or equal to 30&& Logical AND if (day == Friday day has a value of Friday && pmTime > 5) and pmTime has a value greater than 5|| Logical OR if (day == day has a value of Saturday Saturday || day == or Sunday Sunday)A common mistake when checking equality is to insert a single equals sign (=) where adouble equals sign (==) belongs. Use a single equals sign to assign a value (for example,money = 300). Use a double equals sign to check for equality: money == 300 does notassign a value of 300 to money. Rather, it asks whether money has a value of 300.NOTEAlthough number comparisons are straightforward—after all, most of us understand that50 is less than 100—text-value comparisons are less obvious. Derek doesnt equal derekeven though the same letters are used. With string values, A has a lower value than Z, andlowercase letters have greater values than uppercase letters. Thus, if A has a value of 1, zhas a value of 52(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz).If/Else If StatementsAn if/else if statement is similar to the basic if statement except that it enables your scriptto react to multiple conditions. Heres an example:if (money > 500) { buyTV(35 inch);} else if (money > 300) { buyTV(27 inch);}This script executes various actions depending on the value of money: If money has avalue greater than 500, buy the 35-inch TV. If money has a value less than 500, this partof the script is ignored and the next condition is examined. The next condition says that ifmoney has a value greater than 300, buy the 27-inch TV. Thus, if money has a value of450 when this script is executed, the first part of the statement is ignored (because 450 isnot greater than 500), but the second part of the ...

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