arrays
Arrays are a way to have a single variable or property hold more than one piece of information. For instance, if we make a property called "mypictures(6) as picture", it holds six pictures rather than one.
If you are making hangman, your property might be letters(5) as string, where each one would hold a single letter. If the word was "hello", the values of letters would be:
letters(1) = "h"
letters(2) = "e"
letters(3) = "l"
letters(4) = "l"
letters(5) = "o"
Then if someone was guessing the letter, you could use a loop to see if they got one.
Assignment
Make a copy of your slot machine project. Save it as slotmachinearray. Keep it in the same folder as the original, and you won't have to make more copies of the pictures and sounds.
Change the project so that the pictures are in an array. Add a property mypictures(6) as picture.
In the open event, fill the array:
mypicture(1) = pic1, etc.
In the fillcanvas method, change it so it uses the array
dim i as integer
i = rnd*6 + 1
c.graphics.drawpicture(mypicture(i), 0, 0)
You don't need any 'if' statements!
Control Arrays
You can put controls (like canvases) in an array. This will be useful if you are building a game like tic-tac-toe, or concentration. To do it, make a bunch of canvases and give each one the SAME NAME. RealBasic will ask you if you want to make a control array, and you say yes. For some extra credit, change your slotmachinearray project so that in the button action, it fills the canvases like this:
dim i(3) as integer
dim j as integer
for j = 1 to 3
i(j) = FillCanvas(canvas1(j))
next
//see if you can figure out what goes next!
Arrays can be confusing, but they are totally worth it! There are some built in sorting and shuffling methods for arrays. Look in the language reference under arrays.