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

Patterns Part 10

A short recap

It's been some time since I put a part up to read. So let's recap what we learned and where we are:

  1. Objects have scripts; scripts are edited in a script window.  Scripts contain handlers, each handler tells the object what to do if something happens.  You write handlers to handle events such as MouseUp.
  2. To make a script look nice, Revolution indents the script text automatically.  If the text does not line up well, place the insertion point in the text and press tab:  the script will now line up well.  For example:

    will become well-aligned:

  3. To find bugs you can place breakpoints and use the variable watcher to see what happens.
  4. To know more about a function or a programming word, you can use the Documentation from the Help menu:

  5. Pieces of code can be nested to make more complex programs.

Solving our problems

In part 9 we listed problems.  The most exciting one to tackle is colour.  But we should really start by clearing our canvas before creating a lot of new objects.  This is the way to get rid of all the graphic objects:

on ClearCanvas

go to stack "Patterns"

repeat with i = the number of graphics down to 1

delete graphic i

end repeat

end ClearCanvas



Write this handler just after the DrawPattern handler in the script of stack "Patterns".

The handler does this:  if there are 10 graphic objects on our canvas, then the number of graphics is 10, the variable i will start at 10 and then go down 9, 8, 7, ... all the way to 1.

Why did I not write repeat with i = 1 to the number of graphics ?

Well, each time an object is created or deleted the numbers change.  If I delete graphic number 1 first, then the remaining 9 graphics are not numbered 2,3,4,…,10 but they are instantly renumbered 1,2,3,…,9 !

When i has been 1 and the repeat loop now makes i equal to 2, I will delete the graphic that was number 3 but has now number 2 and the graphic that was number 2 but has been renumbered to 1 will actually escape deletion!

To avoid that problem, we must delete from the highest number first and go down to the lowest. Got it?

Good, but nothing will call ClearCanvas.  We should tell the Go button to do that:  in the script of the Go button on the Controls stack, add a line:

on MouseUp

send "ClearCanvas" to stack "Patterns"

send "DrawPattern" to stack "Patterns"

end MouseUp

So we're sending first a message to tell the Patterns stack to clear off all old graphics and then a second message to draw a new pattern.

So far, so good. But you will observe that it takes some time and that we can see the objects disappear one by one.  If we want to speed things up we should tell Revolution not to bother showing anything until it has finished.  We do this with the command lock screen:

on ClearCanvas

go to stack "Patterns"

lock screen

repeat with i = the number of graphics down to 1

delete graphic i

end repeat

end ClearCanvas



Much faster!


OK, now we can add colour.

We have already made a nice colour picker and we have used it to set properties of the Patterns stack.

If you look at the inspector of the stack Patterns and select "Custom properties" you should see:

If you do not, then go back to part 7 and inspect all the scripts of the three sliders for red, green and blue and make sure they set the properties correctly.

Now, in DrawPattern make this change:

  1  on DrawPattern

  2   put the pRedIntensity of me into lRed

  3   put the pGreenIntensity of me into lGreen

  4   put the pBlueIntensity of me into lBlue

  5   put lRed,lGreen,lBlue into lColour

  6   repeat with y=1 to 10

  7   repeat with i=1 to 10

  8   put "R"&y&"C"&i into lName

  9   create graphic lName

 10   put random(4) into lShapeChoice

 11   switch lShapeChoice

 12   case 1

 13   set the style of graphic lName to "rectangle"

 14   break

 15   case 2

 16   set the style of graphic lName to "roundrect"

 17   break

 18   case 3

 19   set the style of graphic lName to "oval"

 20   break

 21   case 4

 22   set the style of graphic lName to "line"

 23   break

 24   end switch

 25   set the width of graphic lName to 40

 26   set the height of graphic lName to 40

 27   set the loc of graphic lName to i*40,y*40

 28   set the linesize of graphic lName to 2

 29   set the foregroundcolor of graphic lName to lColour

 30   end repeat

 31   end repeat

 32  end DrawPattern



So that's in lines 2 to 5 and 28 and 29.

Pick some colour by dragging the sliders on the Controls stack, press the Go button and see what happens. I got:

In the next part we're finishing off.