2 blocks to scan for clones
-
Introduction
In game projects, clones are commonly used to create objects like enemies, obstacles or powerups. However, it is not easy to scan for them in our code, which is often needed when we need to do something clever, such as “find all the enemies near me”, or “check if I have a clear line of fire”.
In CreatiCode, every clone has a clone ID, and the original sprite can also be treated as a clone with an ID of “originalsprite”. There are 2 new blocks that can be used to scan for clones of a given sprite: one is simply based on distance from this sprite, and the other based on a scan area.
Find Clones by Distance
You can use this sensing block to find clones of a given sprite within the given distance from this sprite, and save their information in the given table:
Notes:- You can pick any sprite, even this sprite itself
- The original sprite can also be treated as a clone, with a clone ID of “originalsprite”.
- The distance is the direct-line distance between this sprite and each clone of that sprite, calculated using their x and y positions.
- If any clones are found within this distance, they are listed in the table in order of increasing distance (nearest first). The table will always have at least 4 columns: clone ID, x, y and distance. If the clone has any private variables, they will also be added as additional columns.
Example
Here is an example program showing how it works: play.creaticode.com/projects/685e804081a45ef495ae5fdf
In the Tree sprite, we create 5 clones of the tree. Each clone will go to a random position, generate a “secret” that is private to itself, and then say its clone ID to identify itself.
So on stage, you will see 6 tree objects like this:
Now, in the Truck sprite, we can use this sensing block to find trees that are with 100 units distance from the truck:
Then we can look at the content oftable1
:
As shown, there are 3 clones whose distance is less than 100 from the truck.The nearest clone is the original tree sprite, which is at x of 0 and y of 0. It is about 64.85 units away from the truck.
The second nearest clone is the one with ID of “clone_3”. It is a bit further away, and its
secret
is 78.You can try to change the distance threshold or drag the trees around, and observe how
table1
changes. For example, when the distance threshold is very large, all clones should be listed in the table; and when it is very small, the table should become empty.
Detect Blocking Clones in the Forward Direction
Another very common question we need to ask is whether there are obstacles in front of our sprite, which may block our sprite’s movement or block the shots it fires.
One way to do this checking is to use the block above to find all clones on the stage, and then check the position of each of them. But the calculation become more complex when the sprite is facing an angle (not straight left/right or up/down).
Instead, you can use the following block for this task, which defines a rectangle “scan area” in front of this sprite, and list any clone of the given sprite it finds in this area in the given table.
Notes:
- You can pick any sprite, even this sprite itself
- The original sprite can also be treated as a clone, with a clone ID of “originalsprite”.
- The distance is going forward along this sprite’s current direction. So if this sprite is facing 45 degrees, you can imagine it moves forward along this direction for 200 units, and check if there are any clones that’s 20 (half of 40) units away on the left or right side.
- If any clones are found within this distance, they are listed in the table in order of increasing distance (nearest first). The table will always have at least 4 columns: clone ID, x, y and distance. If the clone has any private variables, they will also be added as additional columns.
Example
Here is an example program showing how it works:
play.creaticode.com/projects/685e928081a45ef495ae82c0
The truck is facing -30 degrees (upper left direction). In the Tree sprite, 2 clones are created in a similar way.
You can drag them to a formation like this:
The original tree sprite is directly on the forward path of the truck. The clone 2 is slightly blocking the path on the right side, and clone 1 should not be a blocker.
Now, suppose we run this block in the truck sprite:
The detection area will be 200 by 70 in front of the truck:
The table1 will contain 2 rows:
Explanation:- The center point of both the original sprite and the clone 2 are inside the red rectangle, so they are included in the result table.
- For clone 1, although its edge touches with the red rectangle, its center point is outside the rectangle. Since we only look at the x/y position of each clone, and not their size, the only thing that matters is their center point’s position.
Therefore, when deciding the width of the detection area, you need to consider the width of both this sprite and the clones.
Now, suppose we change the block to use width of 100, then the table will contain 3 rows, because the detection area now includes the center point of the clone 1 as well:
You can try to play with different truck direction, tree position, distance and width settings to understand how this block works better. -
info-creaticode