![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Properties and VariablesMuch of programming involves changing properties of objects while the program runs. We will write scripts that do this, but first it is important to know what types of properties there are, and how they can be changed by hand. In the next section, we'll start making things happen automatically.Goals:
I have drawn a little character, "Claire". Let's explore the different panes available in the viewer, and in particular, explore the built-in-variables (which we'll call properties) that come automatically with any sketched object. It will help if you are running squeak, and draw your own sketch using the paint tools. I will assume your sketch is also called "Claire", but it is not important that it be as pretty as mine. For now, even a scribble will do fine. Now open up a viewer for Claire, and look at the "basic" pane.
Squeak exercises
Numeric VariablesLook again at the property "Claire's x", and notice the small menu icon just to the left of the property name. If you click, you'll find the following options:x (Number) simple watcher detailed watcher decimal places show categories The title, "x (Number)", says that the variable x has type Number. That is, it has values that can be numbers. We cannot set x to "hello", or to the color blue. Try typing in "hello" in place of the x coordinate (and hit return). What happens? Squeak knows that this is invalid, and won't accept it. One of the advantages of programming using the tile system is that "data type-checking" is done for you; you cannot enter a value that doesn't make sense for a variable. Decimal PlacesBecause x is a number, we can choose how many decimal places we want to display, by selecting "decimal places" from this menu. Try it. Notice that we can now set the x coordinate to 100.65. (Before, we could have typed this in as well, but we wouldn't see the decimal places). Note: for decimals smaller than 1, you need to type 0 to the left of the decimal, thus "0.1" instead of just ".1".CategoriesSelecting "show categories" from the menu sbows us which panes a given property appears on. In this case, we'd see "basic", "geometry", and "motion", since "Claire's x" appears in each of these panes for convenience.WatchersWhat is a watcher?Click on "detailed watcher" for Claire's x, and deposit the watcher that you obtain onto the background somewhere. Make Claire move forward some, and see how the watcher changes. Drag out a "simple watcher" also, to see how that works. What good are watchers? Sometimes in a program you will want to have a value observable without having the viewer open. A watcher does that for you. A perfect application is in a game. If a variable called "score" (which you define... more on this in a later section) keeps track of the player's score, then you will want to display that value. Other reasons include program debugging (explanation), or use as part of user-viewable interface that the user can change as input to the program. For example, suppose I write a program using Claire, and I want the user to be able to change Claire's age (because this may affect how she behaves in the program). Or perhaps the goal of the program will be to keep Claire in a good mood... so it would be nice to have a display of the current mood. I could do that with watchers, like this ![]() Recap
There are several other panes that contain most all of the built-in properties of objects in Squeak. They are: pen use geometry color & border drag & drop graphics Some other panes contain variables that are duplicated in one of the above (for example, "motion" contains x and y coordinates, and heading, which also appear in the basic pane and the geometry pane). Of the above, the first three are the most useful. You can ignore the last two. We will discuss them at some later section, but feel free to explore. The Pen Use PaneWe'll first look at the pen use pane in detail to introduce some properties that do not have numeric values. Every object has a "pen" underneath its center, which it can lower or lift, and drag while it moves, thereby drawing on the background.Open up the "pen use" pane for your sketch, and you should see something like the picture below (without the explanations to the right).
Color and Boolean Valued VariablesSo far, we have explored variables like x and y coordinates, or heading, that have numbers as their values. Remember my car? It was 20 feet long, and blue. Sometimes numbers are good at describing things, sometimes colors are. To tell you about my car's length, I must tell you a number. To tell you about what it looks like, in particular it's color, I must tell you something like "blue" or "green", and so on. Since we are programming with objects that appear on the screen, part of the way we describe some of their properties is by using colors. So, some variables have color values. Above, the penColor property has value type color.Change the Color The current color is probably blue. To change the color, click in the blue colored box and an eyedropper and color palatte will appear. You can choose a color from the palatte, or you can choose any color anywhere else on the screen (for example, the orange color of the navigator tab) by clicking on the color you'd like. Notice that when you do this, the penColor shown changes. Put the Pen Down To make Claire draw, the pen must be down. The property "penDown" has two possible values: "true" or "false". This is called a Boolean valued variable. (Named after mathematician George Boole.) You can change from "false" to "true" by either using the green up/down arrows next to "false", or by clicking on false and selecting "true" from the list that pops up. Change Claire's penDown to "true". Now Draw Click on the brown icon on Claire's halo, and use it to drag Claire around. Write your name. (You can also "right-click" to drag Claire. The black icon picks Claire up, as does the usual left-click, so that no pen marking is seen if you move her this way.) Experiment by changing the numeric variable "penSize". Try changing the variable "trailStyle" to a different kind of pen trail. When you want to erase, click on the yellow exclamation point next to "clear all pen trails". Click on the menu icon next to penColor, and pull out a watcher for Claire's penColor. Close Claire's viewer by clicking on the small tab. You can continue to draw, and you can use the watcher to change colors, by clicking in the small color box of the watcher. Recap
There are other value types beyond numeric, color, and boolean. The two others we will encounter later are string, and player. A string value type is just a sequence of alphabetic characters, for holding text. A player value type is actually another object. We will encounter player-valued variables much later when we tackle advanced techniques. The Geometry PaneOpen up the geometry pane in Claire's viewer. ![]() Most of these numeric properties are easily understood. We've already learned about the x, y coordinates, and the heading. The properties left, right give the x coordinate of Claire's left and right edges. The properties top and bottom give the y coordinates of Claire's top and bottom edges, and the properties length and width are the height (up and down) and width (left and right) in pixels of the object. (So Claire's length = Claire's top minus Claire's bottom). Try changing the left, right, top, bottom, width, and length. The scalefactor allows us to grow and shrink an object by a factor of up to ten in either direction. Scalefactor can be any value between 0.1 and 10.0. Try changing it and see what happens. You can always set it back to 1. Notice while you change it how the left, right, top, bottom, length, and width all change. Claire's x and y do not change, because they are the location of the center of the sketch. Ignore the properties "theta", "distance", and "heading". We will see them in an advanced section. Color and Border PropertiesOpen up the pane for Claire's border and color.![]() There are just two properties. "dropShadow" is Boolean, being true or false depending on whether or not Claire has a shadow. (All objects have a shadow when lifted by the mouse. The dropshadow is one that can appear when they are not lifted by the mouse.) Try toggling it to true. The other is a color-valued property which tells the color of the shadow (if dropShadow is true). Play. Now we point out the color and border pane is not very useful for sketches, but for an object dragged out of the supplies bin such as ellipses, stars, etc., there are many more options. Drag out a rectangle from the supplies bin, open up a viewer, and look at the color and border pane. You will see something like this: ![]() Play with all of the options. See what they do. Tuck away in the back of your head that these are things that you can control for these objects. The main use of color and border while programming is this: For simples shapes taken from the supplies bin, you will be able to write a script that will change their colors when the script is run This might be useful, for example, to make shining stars, a traffic light, etc. Special Properties for Special ObjectsWe have looked at most of the properties that all objects have in common. However, remember the joystick from an earlier tutorial? It had a pane called "joystick", and a property called "joystick's leftRight" and "joystick's upDown".In general, different kinds of objects may need special properties that don't make sense for other objects. You will find that when this is the case, there will be some special panes available from the object's viewer. We will introduce these when we use the particular object. You may want to play around with some now. If so, I suggest looking at
Link to this Page
|