Rather than determining in advance what percentage chance of getting a box, circle, letter or word for each iteration of createBox(), I wrote a program that sets a random breakpoints between 1 and 100. Suppose you get random break points at 10, 20 and 70. A random number between 1 and 100 is generated. If that number is greater than 70, you will get a box. That means there is a 30% chance of drawing a box. If the random number is between 20 and 70, then you will get a circle. That means there is a 50% chance of drawing a circle.
As with the reselection of a color scheme in project 13, we will reset the breakpoints at the end of the fade out process, so that the program, when it runs could do a series where it is very likely to draw boxes, or it could go through a sequence where it is more likely to add words to the page. You can see below in the createBox function where the breakpoints are used as a test to determine what to draw — a box, circle, letter or word.
Code Snippets for 14
This function returns an array of numbers to set as break points between 1 and 100. You might get something like 10, 30, and 70. These are used to figure out the percentage chance you might get a box, circle, letter or word.
I set a global variable to hold the array of breakdown numbers. I passed in a 3 to get three elements in the array, this will divide my range of 1 - 100 into four pieces.
The chanceArray numbers are used in the if statements in createBox to determine what type of shape to create. You can see where the array is tested on lines 21, 27 and 34.
A new chanceArray with new breakdown points is created at the end of fadeout process. You can see the array is reset on line 18.