Back to Lesson 3
Forward to Lesson 5

Lesson 4 - Tests and Truth

You may have noticed that moving the mouse too far to the left and right will make the cannon disappear off the screen. This will be especially evident if your stage size is smaller than your monitor resolution.

Fortunately, it is possible to keep the cannon on the stage, but still follow the mouse. This is achieved by using one of the fundamental programming concepts, the truth test.

All programming languages, and all human languages have some concept of True and False. Computers are built up from this basic distinction. This binary opposition is often seen as the values 1 and 0. Lingo is no exception, and it uses the word if to test whether something is true. What we really want is something like this...

if the mouse is within the stage...

Positions on the stage are described as points relative to the TOP LEFT of the stage window.

The left of the stage is 0 pixels horizontally, the right of the stage is variable, but will very likely be 480 pixels from the left. Because I don't know how wide your stage is, and because you might change it at any time, I am going to refer to it's width as

the width of the rect of the stage

...which gives the correct value no matter how big your stage is. Notice how this is a slightly more sophisticated reference to the property of an object. The stage is a window, and all windows have a rect property which describes the rectangular size and positioning of the window on the screen. All rects have a width property, the width of that rectangular area measured in pixels.

Now go back to your cannon script and modify it as follows...

on exitFrame me

  set mySprite to the spritenum of me
  set stagewidth to the width of the rect of the stage

  if the mouseH > 0 and the mouseH < stagewidth then
    set the locH of sprite mySprite to the mouseH
  end if

end

You may recognise the > and < characters from mathematics. They mean greater than... and less than... respectively and are often used in tests of this sort. Whatever is between if and then must be either true or false. If it is true, the instructions in the lines after then will be executed. If not, nothing will happen in this case.

Notice also the word and. This means that both conditions have to be true for the whole test to be true. In human language we might say something like

if you have worked through this tutorial and understood it...

So you can see that logical concepts like 'if' and 'and' are deeply woven into our language, it is a part of communicating and being intelligent beings that we understand basic logical thinking. Other human logical words which are very important, appearing in some form in all programming languages (and even some graphics applications), are or and not. See if you can identify or articulate the logic behind some human sentences which use the words and, not, or and if. I can guarantee you will find the idea of true and false in there somewhere. Two other words strongly related to this are else and otherwise. These will appear later.

After describing what we want to happen if the horizontal position of the mouse is within the area of the stage we have added end if on a seperate line, which means that I have finished describing what will happen if the conditions are met.

Director will usually typographically indent lingo which is at the same 'depth' of test so that it is easy to see if you have forgotten an end if or a then. If all your code suddenly shuffles up to the left hand side of the script window, you have probably forgotten something like an end if or a then, you may even have too many end if's!

Take care to write

if...then on the same line, the contingent instructions on seperate consecutive lines and end if on a seperate line afterwards. There are ways of deviating from this pattern which often cause problems so I will not discuss them here.

When you run your movie again, you will find that no matter where your mouse is, the cannon will stay on the stage.

 

Forward to Lesson 5