Clone grid

My goal is to build a 5x5 grid of images. In the following code row

, col

and rowcol

were created as sprite-local variables, and newcol

, newrow

and cats

are global. (By the way, is it possible to specify which variables are local and global? It's easy to forget or make mistakes.)

code

The result is only a 5x1 mesh as shown here.

results

It is unclear in the order of execution of these statements. Is it called when I start as a clone

before or after add_cat

receiving the call a second time? My preliminary conclusion is that it is called afterwards, but the clone globals seem to contain their values ​​initially.

When I tried to debug it using the ask

and say

and commands wait

, the results changed a lot. Adding such pauses in some places completely eliminated the problem, resulting in a 5x5 grid. Elsewhere they called the 1x5 grid.

The main question is, how do I fix this so that it creates a 5x5 grid?

+3


source to share


1 answer


Explanation

Unfortunately, the execution order in Scratch is a bit odd. Whenever you edit a script (by adding or removing blocks, editing inputs, or dragging and dropping the entire script to a new location in the editor), it is placed at the bottom of the list (so it works last).

A good way to test this is to create an empty project with the following scripts:
two similar scenarios

When you click the green flag, the sprite will either say "script one" or "script two", whichever is fired first. Try clicking and dragging one of the blocks when green flag clicked

. The next time you click the green flag, the sprite will tell you which message corresponds to the script you just dragged and dropped.

This crazy order can make execution incredibly unpredictable, especially when using clones.



Decision

The only real solution is to write code that has a specific order of execution, inline (instead of relying on the whim of the editor). For simpler scenarios, this usually means using a block broadcast and wait

to trigger specific events in the required order.

For your specific project, I see two main solutions:

Procedural solution This is the simplest script, and this is probably what I would choose: ( and are sprite-only variables) Since clones inherit all sprite- only variable values, when created, each clone will have the correct row and column. when it is created.procedural decision
row

col


Recursive solution This solution is a little harder to understand than the first one, so I'll probably avoid it if you're just looking for something new:
recursive solution

+5


source







All Articles