Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    2 blocks to scan for clones

    2D Blocks
    1
    1
    6
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by

       

      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:

      e831e32e-e0a6-432e-8007-0f4a6a0b3998-image.png

       
      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.

      de3a9b35-fc8d-4d65-9d6d-797ce583e98f-image.png

       
      So on stage, you will see 6 tree objects like this:

      eebfc0d0-71f5-4490-b138-ff65e1035acc-image.png

       
      Now, in the Truck sprite, we can use this sensing block to find trees that are with 100 units distance from the truck:

      5733089a-dea8-4b91-826c-b9f3f49a05f7-image.png

       
      Then we can look at the content of table1:

      dc341578-fed3-42a9-befe-8dc385511c30-image.png

       
      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.

      77abc8c2-dde9-4ebd-bceb-5d54e2459d29-image.png

      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:

      4073efa3-a883-42b8-ad09-497a09e283bc-image.png

       
      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:

      050c0b67-7a27-4784-8877-e6861b7942e5-image.png

       
      The detection area will be 200 by 70 in front of the truck:

      00d68337-ca4b-489e-b34c-bf0442c7c8d7-image.png

       
      The table1 will contain 2 rows:

      9ab900f3-a6be-4785-b614-c9d1b9611a4c-image.png

       
      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:

      b147a6c9-6ee4-44bc-9367-fd5737041030-image.png

       
      You can try to play with different truck direction, tree position, distance and width settings to understand how this block works better.

      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post