I'm sorry if this seems a foolish question but here goes...
Back in the old days (80s) whenn I used to dabble in BASIC on my ZX Spectrum, a much used programming technique was to define up an array of variables. Is this possible within Scratch?
Offline
I read something about this a while ago. I know in scratch that you can add variables to lists (which are scrtach's version of arrays) and in BYOB you can put lists in lists. Hope that helps.
Offline
Imaginos wrote:
Thanks shadowmouse. I presumed that Lists were the Scratch version of arrays but I couldn't see any information on Lists in the Scratch Reference Guide.
Well your presumption is in a sense correct because lists are arrays, just 2D or with only one column, whichever way you like to look at it.
Anyway, you want to form a 3D array, correct?
With regards,
ErnieParke
Offline
Imaginos wrote:
Yes Ernie, that's what I was thinking of... to define a grid of backgrounds (stages) etc...
Oh, okay. This shouldn't be too hard, with the right technique. That's because Scratch doesn't have a native system to create arrays. So this raises a question; how are we going to create an array?
So to make a 3D array (it's actually 2D, though it acts as a 3D one), all you have to do is create a list, set the first two items to the array's length and width, and you set the length of the list to your length * width + 2. Now, reading left to right, up to down in your imaginary array, number the first item 3, the second 4, and on... Their number will now correspond to their position in the list. Now you have an array!
Just as an example, let's say we want to turn the following 2x2 array into it's corresponding list:
12 6
4 15
After a bit of editing, we get:
2 //Length
2 //Width
12
6
4
15
Does this make sense?
Now, the final thing you need to know is how to read an item from the list. If you know its column and row, just use this:
(item (((((Row) -(1)) * (length)) + (column)) + (2)) of [Array v])
Last edited by ErnieParke (2013-03-29 21:08:33)
Offline
I agree with what Ernie says, but alternatively, you can set up constants for the length and width, to prevent any silly coding mistakes and clean up the code a little bit (having an extra +2 in the code makes it just that much less user-friendly)
Offline