Arrays and Random
Top  Previous  Next


Description
Demonstrates how to use Arrays to display a random message.

Aim
Introduces methods for Arrays and Math.randomInt()


1. Create a new file and save it as "mymaxarray.swi". Insert a Dynamic text object and name it "message" on the 'Text' panel. Be sure to tick the 'Target' option so that it can be used in our script.

scripttute14_1

2. Right-Click on the text object in the 'Outline' panel and select Grouping | Group as Sprite from the menu. Name the sprite "s_message" on the 'Sprite' panel.

scripttute14_2

3. With the sprite object selected in the 'Outline' panel, open the 'Script' panel and press the 'Add Script' button. Select Events | Frame | onLoad() from the menu.

4. Right-Click on the onLoad() event and select Add Script | Statements | evaluate; from the menu.

scripttute14_4

5. Type "smt = new Array()" into the evaluate field on the 'Script' panel as shown below:

scripttute14_5

Note: In the script above, a new empty array object was created and assigned to the variable "smt".

6. Press the 'Add Script' button again and select Statements | evaluate; to add our first value to this array. In the evaluate field type what is shown on the image below:

scripttute14_6

Note: First the variable "smt" is used with brackets after it. The number inside the square brackets indicates the position (or index) within the array (please note, array indices always start from zero as the first element of the array). The message "awesome!" is defined for this position in the array.

7. Repeat Step #6 to add several more values to this array. Make sure that you increase the number inside the brackets by one each time a new position in the array is defined. It may be faster to copy and paste the first value and simply change the values. An example is shown below:

scripttute14_7

8. When you have created several new values for the array, press the 'Add Script' button again and select Statements | name=expr; from the menu. In the 'Target' field use the drop-down menu to select the dynamic text object "message" from the list of targets. In the 'Name' field, use the drop-down menu to select Text | text. Leave the Operator as "=" and begin typing what is shown in the image below "SWiSHmax is" etc. When you type the first bracket, Right-Click after it and select Math | Math.randomInt({max}) from the menu as shown below:

scripttute14_8

9. When the Math.randomInt() script is added, the cursor will be inside the parentheses. Type the number "8" (which is one number higher than the last number in the array) into the parentheses. Then finish the script by adding the closing bracket "]". The 'Script' panel should appear similar to the image below:

scripttute14_9

10. Go back to the 'Layout' panel and press the 'Play' button in the Control Toolbar to preview the movie. Press the 'Play' button several times to see a random message displayed.

Analysis:
The sprite "s_message" contains on onLoad() event which is triggered when the sprite is first loaded. Inside the onLoad() event a new Array object was defined. Array indices begin with 0 - zero - as the first position in the array and a value is defined for each position. Using the Math.randomInt() code, the movie will select a number between 0 and the number inserted into parentheses (note, the number in parentheses will not be one of the random numbers which is why we used a value one greater than the last position in the array).

The script that contains "smt[Math.randomInt()]" will created a random number for the array and the number that is picked will correspond with one of the positions in the array.