I started out to try to figure out the most elegant solution, and due to my limited expertise I'm still trying to come up with any solution. The setup is simple.

1. Click a sprite and it generates a random number

2. Then a matching number of objects is created.

In my case I want to use it to visualize arithmetic problems for teaching. I.E. the random number 4 is generated, and 4 bars (sprites) are created along with it to show the quantity. I assume the best approach for the bars is to have the system check the number, then create one bar, then make instances and offset them. I'm not clear on how to make it all happen. I have spent a lot of time trying. I'm sure I can figure out the offsetting, it's the creation of a certain quantity that matches the random number that I'm stuck on.

This c3p shows the basic setup for my specific case.

Ideally the solution would accomodate larger numbers, say up to 50 at least, in which case I know I would have to deal with how to have a certain number of bars in a row, before going to a new row. Also, it's not really for just a single number, but for addition and subtraction problems, but cross that bridge later. I'd like to gain a basic understanding first.

Hi Diego, this is great. Thanks very much. I'm not totally sure I understand what's going on:

1) "Amount" looks like a local variable (icon), but it's actually a function parameter that you added?

2) It's looping the create action for "amount" times, and I see amount is set to the GV "theNumber", but why could the number of loops not be set directly to the GV? Is that not allowed? It seems to work ok if I do that.

3) I assumed the -1 was because I have the GV "theNumber" starting at 1, but that doesn't seem to be the case. If I make theNumber start at 0 and remove the -1, then there is one more bar than there should be, so I'm not quite clear on this.

"Amount" is the function parameter for "CreateItems", function parameters look like local variables. You can try right clicking on top of the function and selecting "Add parameter" to see how adding more would look like.

When the function is called, "theNumber" is passed in so then inside the function "Amount" takes that value. For this contrived example it wasn't really necessary, just using "theNumber" inside the funtion would have worked the same. If you try replacing "Amount" by the "theNumber" in the for loop, it should be the same.

The reason I have the loop starting at 0 and ending at amount - 1, has to do with the calculation to position the items. I want the first LoopIndex to be 0, so the first item will not have any offset applied to it.

I changed the round random to include zero, and when it generates a zero it's looking like this,with an initial bar in the normal place and then one to the left.

It makes sense that if you pass 0 to the function it would do something strange.

LoopIndex gives you a number as the loop moves from the start index to the end index.

So in this case, if the start index = 0 and the end index = 0 - 1, then the for loop will be executed two times. The first time loopIndex will be 0 and the second time loopindex will be -1.

If you plug those number in the calculation it is doing to place the bars, you will see it places the first one with no offset, and the second one to the left of the first.

The function should have a special case for 0, so that it doesn't even go in the for loop and just exits early after destroying all the existing instances.

It makes sense that if you pass 0 to the function it would do something strange.

LoopIndex gives you a number as the loop moves from the start index to the end index.

So in this case, if the start index = 0 and the end index = 0 - 1, then the for loop will be executed two times. The first time loopIndex will be 0 and the second time loopindex will be -1.

If you plug those number in the calculation it is doing to place the bars, you will see it places the first one with no offset, and the second one to the left of the first.

The function should have a special case for 0, so that it doesn't even go in the for loop and just exits early after destroying all the existing instances.