![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Simple ScriptingHere we cover the very basics or writing scripts for objects. Important ideas such as using tests (conditional statements) and using user-defined variables are introduced in a later section.The goals for this lesson are to learn to
You've already done some simple scripting, with the car project. You know that in the viewer, scripts are those lines that begin with a yellow exlamation mark If you drag any one of these (like "Claire forward by 5") out of the viewer and set it down on the world, it will sprout a scripting box around it: ![]() This box is called a scriptor. You can then drag in other scripting commands to make a single larger script with several statements in it. Note We do not like scripts named "script1". We always change scripts to names that explain what they do. Similarly, we always change the names of objects we create to something meaningful, not "sketch12". This avoids a lot of confusion when you have multiple scripts and multiple objects. SCRIPTOR HEADER Look at the first line of the script box above (with "Claire script1" in it). The first line is called the script header. Let's examine what is available in the script header for any scriptor. Next comes a box with the name of the scriptee (Claire, in this case) Then a box with the name of the script itself (script1 here ... a bad name) A small clock called the tick indicator .Next comes the small phrase that indicates under what conditions the script executes, or how the script is triggered. If you hold down the mouse over this, you will get the following menu of options ![]() Next is a small box Finally, BE CAREFUL, the "destroyer" button Explorations Ticking Clock
![]()
TICK RATE We can change the tick rate of the clock, and have it accurately mark off the seconds. Hold down the mouse button over the small clock icon, and set the tick rate to 1 ![]() Now, how many degrees (out of 360 = once around) does the second hand move on an actual clock? Well, it moves 360/60 = 6 degrees each second. Change the "5" to say "6" by clicking on the green up arrow. The clock should now be ticking off seconds. Experiment with the ticks/second and have it tick at a different rate (try 100/sec). every script can have its speed adjusted in "ticks per second", The number of ticks/second can be a fraction. Type in "0.1" after selecting "other" in the menu. The hand will move once every 10 seconds. (You need to type 0.1, not .1. And remember to hit the return key, or click on "accept".) Change the ticks/second back to 1. TURN BY ANY AMOUNT AT EACH TICK How can you make the clock move backwards? Replace "turn by 6" with "turn by -6". RANDOM NUMBERS If we'd like the clock to behave erratically, we can have it turn a random amount at each step. When you position the mouse over the word "hand" in the header of the script, the cursor will turn into a small menu. Click and hold down, and select "hand me a random number tile", then drag that and put it in place of the "6" in the script: At each tick, a number is chosen between 1 and 180 randomly, and that is the amount that the hand moves. Change "180" to "30". An alternative place to find the random number tile is from the supplies bin. CHANGING WHEN IT RUNS Click on the small clock icon to stop the script from ticking. Hold down the mouse over the word "paused" in the script header, and select "mouseDown" from the menu that appears. ![]() Now the hand will move only when you click the mouse down on it. Try it. Change the condition to read "mouseStillDown", then click on the hand and hold the button down. The hand should continue to turn until you release the mouse button. Experiment with other conditions. For help, select "what do these mean?" from the menu. The three main settings are "normal", "paused", or "ticking". A "normal" script is usually meant to be executed once through (for example, by clicking on the yellow exclamation mark), and not meant to be set to "ticking" where the statements will be run over and over again. This does not mean though that during the entire run of a project a "normal" script won't execute more than once. An example is a script that resets objects to particular locations. You may want this to happen from time to time (e.g., pac-man returns to the center), but it would not make sense to have such a script continuously ticking (because, for example, pac-man would be unable to move away from the center). A "ticking" script is one whose statements are run through over and over. All statements are executed in order from top to bottom, and after the last statement in the script is run, they all are begun again, starting from the top. A "paused" script is a "ticking" script that is not currently running. A script is never paused between two statements in the middle of the script.... if the first statement in a script is run, then they all are, and the "pause" will take effect after the last statement in the script is next executed. (Programmers will be interested to know that scripts are "atomic", and each ticking script is executed completely once through without interruption from any other ticking script. ) So, what is the difference between "paused" and "normal"? None, really, except for the very useful tool labeled "All Scripts" found in the supplies bin. When the "go" button is pushed, all paused scripts are started running again, and when the "stop" button is pushed, all ticking scripts are then paused. Scripts set to normal are not started ticking (though there is an option to make this happen.) We refer to scripts typically intended to be run over and over again as tickers, and other scripts as fire-once scripts. So far, you have mostly dealt with tickers. The following need to be made user-friendly and graphical Random line drawing by putting pen down, then fwd random 180, turn random 180. Have several all going, with different colors. Your own drawing tool.
Create a script that does the following:
Butterfly on arcs
Geometry art Set Claire's penDown to true in the viewer. Claire geometryart
Now start it ticking. Make a slider work like a joystick.
Making objects chase
![]() How can we Colin follow Claire in a less direct fashion? We can simply modify the direction of Colin by a reasonably large random amount: ![]() Why does this work? What would happen if we switched the positions of the "turn by" and the "forward" statements like this: ![]() This is a good time to talk about stepping through your script to understand exactly how it works. Pretend to be the computer and do one step at a time, from top to bottom. Does that help explain the difference between the two scripts? This simple behavior forms the beginnings of a pursuit game. We could control Claire with a joystick, and have Colin chase her. But how do we control what happens when Claire gets caught? We'll need Tests which we'll see in the next section. RECAP lines with excl. mark are built-in scripts. drag them out, get script box can add other scripting lines to get compound script, executes one line after another. Can we change the pen thickness while it is going? We can by hand, but how can we get the program to do it? Exercises with Assignment statements. Explain green/purple arrow. New randomart, with line thickness set to random. Jumping claire set x, y random when mouse clicks Following claire set x, y to track mouse (off by 100, so don't get stuck) Make Claire hide
Sending to a place (executed once) is a useful thing for the beginning of a game, or when a ball needs to be served, or to reposition things. Recap
Old notes
Link to this Page
|