Difference between revisions of "MessageBox Tutorial"
m
→Running the same choice for multiple frames: Clarity
imported>Haama m (→Keeping multiple menus separated: Clarity) |
imported>Haama m (→Running the same choice for multiple frames: Clarity) |
||
Line 183: | Line 183: | ||
===Running the same choice for multiple frames=== | ===Running the same choice for multiple frames=== | ||
So far, we've | So far, we've been using GetButtonPressed like this: | ||
<pre>... | <pre>... | ||
elseif (Choosing == 1) | elseif (Choosing == 1) | ||
Line 194: | Line 194: | ||
<pre>... | <pre>... | ||
elseif (Choosing == -1) | elseif (Choosing == -1) | ||
messagebox "What would you like to do?" " | messagebox "What would you like to do?" "Count food" "Nothing" | ||
set Choosing to 1 | set Choosing to 1 | ||
set Choice to GetButtonPressed | set Choice to GetButtonPressed | ||
Line 201: | Line 201: | ||
if (Choice == -1) | if (Choice == -1) | ||
return | return | ||
elseif (Choice == 0) ; | elseif (Choice == 0) ;Count the number of food items in the player's possession | ||
set InvPos to (InvPos + 1) | set InvPos to (InvPos + 1) | ||
set pInvObj to (player.GetInventoryObject | set pInvObj to (player.GetInventoryObject | ||
if (IsFood pInvObj) | |||
set FoodCount to (FoodCount + 1) | |||
endif | |||
... | ... | ||
elseif (Choice == 1) ;Nothing | elseif (Choice == 1) ;Nothing | ||
Line 210: | Line 212: | ||
endif | endif | ||
...</pre> | ...</pre> | ||
" | "Count food" requires several frames to complete. Once GetButtonPressed returns 0, it will start off and test the first item. However, on the following frames GetButtonPressed will return -1, causing an infinite loop of ''if (Choice == -1) -> return''. To prevent this, only run GetButtonPressed when it had previously returned -1: | ||
<pre>... | <pre>... | ||
elseif (Choosing == -1) | elseif (Choosing == -1) | ||
messagebox "What would you like to do?" " | messagebox "What would you like to do?" "Count food" "Nothing" | ||
set Choosing to 1 | set Choosing to 1 | ||
set Choice to -1 | set Choice to -1 | ||
Line 219: | Line 221: | ||
if (Choice == -1) | if (Choice == -1) | ||
set Choice to GetButtonPressed | set Choice to GetButtonPressed | ||
elseif (Choice == 0) ; | elseif (Choice == 0) ;Count the number of food items in the player's possession | ||
set InvPos to (InvPos + 1) | set InvPos to (InvPos + 1) | ||
set pInvObj to (player.GetInventoryObject | set pInvObj to (player.GetInventoryObject | ||
if (IsFood pInvObj) | |||
set FoodCount to (FoodCount + 1) | |||
endif | |||
... | ... | ||
elseif (Choice == 1) ;Nothing | elseif (Choice == 1) ;Nothing | ||
Line 228: | Line 232: | ||
endif | endif | ||
...</pre> | ...</pre> | ||
Note that Choice is reset to -1 after displaying a menu. Otherwise, Choice would still be the same as for the last menu ( | Note that Choice is reset to -1 after displaying a menu. Otherwise, Choice would still be the same as for the last menu (0, in this case). GetButtonPressed would never be run because Choice isn't equal to -1, and that Choice (0) would run instead of the player's choice. | ||
==Creating Your New Menu== | ==Creating Your New Menu== |