go to Part:   previous   next   1   2   3   4   5   6   7   8   9   10   11 

Patterns Part 4

Getting out of a mess

Gemma Pye writes:

When I press the Go! button now it plays the first time and it plays until I let go and once I let go the actuall image still shows on the stack untill I press again for it to create another

Right.  Exactly. Lots of coloured rectangles.

And, when I press the button for a long time I get something like this:

I'm not sure if we can call it pretty or not.  Probably not.

However…  we can no longer see the button:  the rectangles have been painted over it.

How do we get out of this mess?

We have written very little code:  only 11 lines!

Let's look at the code again, here it is:  (I've put line numbers in for us to see things better)

  1  on MouseDown

  2   if there is an image 1 then delete image 1

  3   put the width of this stack into lW; put the height of this stack into lH

  4   choose rectangle tool

  5   repeat while the mouse is down

  6   set the linesize to random(10)

  7   set the pencolor to (random(255)-1),random((255)-1),random((255)-1)

  8   drag from random(lW),random(lH) to random(lW),random(lH)

  9   end repeat

 10   choose browse tool

 11  end MouseDown 

When you hold the button down, this happens:

In line 2 the existing image is erased, if there was one.

In line 2 the program gets the width and height of the window so it can paint rectangles all over it.

In line 4 the program chooses the rectangle tool from the tools palette.

Now it becomes interesting:  line 5 says the program should repeat something while the mouse is being held down.  The part the program repeats doing is lines 6, 7, and 8.  That it's only those three lines the program keeps repeating is indicated by the end repeat in line 9.  Line 9 itself does nothing at all, it just sits there to tell us where the part that is being repeatedly done ends.

So the program goes around doing line 6, then 7 then 8, then looks in line 5 again to see if the mouse is still being held down.  If so, the program does 6, 7 and 8 again.  Each time the program draws a random rectangle with a random colour and random thickness.

Then we let the mouse go up.  The program sees that when it comes to line 5, it then jumps to line 9 and stops repeating.

It then goes on to line 10 where it chooses the browse tool, and finally in line 11 it stops completely.

But the button may have been overpainted completely.

What we should do after line 9 and before line 11 is to make the button appear from under the paint.

Layers and the Application Browser

Each object in Revolution is on a layer.  We have only two layers:  one with the Go! button and one with the image.

To see layers, go to programming mode and choose Tools—>Application Browser.  A window pops up:

click on the little grey triangle next to the icon of the stack "Patterns" then click once on the icon of the card that comes up, this shows:

The Application Browser has two sides:  on the left are stacks and cards (forget for the time being what they may be) and on the right you see objects of a card.

The right side has 6 columns:

  1. icons of objects
  2. if they are visible or not (column with the little magnifying glass at the top)
  3. (forget that column for now)
  4. the layer of the object
  5. the name of the object
  6. the number of lines of code in the script of the object.

You see that the layer of the image is 2 and that of the button is 1.  The button is under the image.

The Application Browser allows us to get at the button again:  right-click the button in the application browser and select "Edit Script":

So let's add a line of code to set the layer of the button to one higher than the layer of the image when the mouse is released:

  1  on MouseDown

  2   if there is an image 1 then delete image 1

  3   put the width of this stack into lW; put the height of this stack into lH

  4   choose rectangle tool

  5   repeat while the mouse is down

  6   set the linesize to random(10)

  7   set the pencolor to (random(255)-1),random((255)-1),random((255)-1)

  8   drag from random(lW),random(lH) to random(lW),random(lH)

  9   end repeat

 10   set the layer of me to 1+(the layer of image 1)

 11   choose browse tool

 12  end MouseDown

 

Line 10 says what I just wrote, but it uses the word me instead of button "Go!".  That is fine, because the word me means that object to which the script belongs.  Since we are writing the script of button "Go!", me means button "Go!".

Now you can let the program overpaint the button as long as you like, as soon as you release the button it pops back to the top layer, above the image.

Save your work.

(next part)