Discuss Scratch

Bubbajoe57wastaken
Scratcher
37 posts

Nearest Clone(s)

How might I detect the nearest clone to a sprite? I have a list of all of the clones, but I'm not sure if I need to use it to detect the nearest clone.
medians
Scratcher
1000+ posts

Nearest Clone(s)

Have a list with the positions of every clone (and use cloneid to update them), and use the distance formula (a variation of the pythagorean theorem) to find all distances. Find the smallest result.
So you would just do this:
when I start as a clone
forever
replace item (cloneid) of [x positions v] with (x position) //do the same but with y positions
end

Last edited by medians (Feb. 25, 2023 03:16:20)

Bubbajoe57wastaken
Scratcher
37 posts

Nearest Clone(s)

Is cloneid the ID of each specific clone? If so, how can you get a unique ID for each clone? I have both the cloneid and clones variables, but they're just acting as the same thing. I can't tell what I'm doing wrong. (This is the game I'm working on: https://scratch.mit.edu/projects/808724393/ )
medians
Scratcher
1000+ posts

Nearest Clone(s)

Bubbajoe57wastaken wrote:

Is cloneid the ID of each specific clone? If so, how can you get a unique ID for each clone? I have both the cloneid and clones variables, but they're just acting as the same thing. I can't tell what I'm doing wrong. (This is the game I'm working on: https://scratch.mit.edu/projects/808724393/ )
You have to make the clones variable for all sprites and the cloneid a sprite only variable. Clones have their own sprite only variables, meaning that if you do this:

Clones would have 1 as the value, while the main sprite has 0. However, if it were for all sprites, then it would have 1 for everything including the actual sprite.

Last edited by medians (Feb. 25, 2023 16:09:00)

Bubbajoe57wastaken
Scratcher
37 posts

Nearest Clone(s)

So it should appear like this? https://scratch.mit.edu/projects/808724393

Last edited by Bubbajoe57wastaken (Feb. 25, 2023 16:46:38)

medians
Scratcher
1000+ posts

Nearest Clone(s)

Bubbajoe57wastaken wrote:

So it should appear like this? https://scratch.mit.edu/projects/808724393
Well, you should swap the order of cloneid = clones and clones += 1 in the when I start as clone hat block
Bubbajoe57wastaken
Scratcher
37 posts

Nearest Clone(s)

In the min and max list, what roles min, max, and index serve?
medians
Scratcher
1000+ posts

Nearest Clone(s)

Bubbajoe57wastaken wrote:

In the min and max list, what roles min, max, and index serve?
You'll want to use min because that will show which distance is the lowest, meaning that the clone is closer (which is what min will store). index is needed to iterate over the list. You'll want to use that on the list with all the distances. You don't have to use max in this case.

Last edited by medians (Feb. 25, 2023 18:09:44)

Bubbajoe57wastaken
Scratcher
37 posts

Nearest Clone(s)

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
p-p-p-p-p-p-p-p-p-p-
Scratcher
1000+ posts

Nearest Clone(s)

Bubbajoe57wastaken wrote:

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
You could do this more simply:
Start by using a
(distance to [ v])
block in another sprite, and replace the selectable box with the sprite that has clones. Then, drag that block so it is hovering over the sprite with the clones, and drop it in, This will copy this block into the other sprite, Return to the sprite with the clones, and make a new script:
when I start as a clone
forever
replace item (clone id) of [Clone Distances v] with (distance to [Sprite w/ Clones v]
end
If you don’t already have clone id set up, create a variable and make sure it is set to “this sprite only”. This will make it so that each clone can have a separate value for this variable. Then, in the start set clone id to one, and when you create a clone increment it by one. This will essentially give each clone a numeric value associated with it.

Then, in the same sprite, use this on a NON CLONE:
forever
set [placeholder v] to (1)
repeat (length of [Clone Distances v] :: list)
if <(item (placeholder) of [Clone Distances v]) < (item (Closest Sprite) of [Clone Distances v])> then
Set [Closest Sprite v] to (placeholder)
end
Change [placeholder v] by (1)
end
end

I think this should work, let me know if you have any issues. I have previously used this in one of my project, and it worked really well in my opinion.
medians
Scratcher
1000+ posts

Nearest Clone(s)

Bubbajoe57wastaken wrote:

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
The distance formula, look up “distance formula” and you'll see how. You can use the item # of block to get which clone it is, or another variable.

p-p-p-p-p-p-p-p-p-p- wrote:

Bubbajoe57wastaken wrote:

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
You could do this more simply:
Maybe put it on the clones, because it might try to find the distance of the main sprite.

Last edited by medians (Feb. 25, 2023 21:54:01)

p-p-p-p-p-p-p-p-p-p-
Scratcher
1000+ posts

Nearest Clone(s)

medians wrote:

p-p-p-p-p-p-p-p-p-p- wrote:

Bubbajoe57wastaken wrote:

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
You could do this more simply:
Maybe put it on the clones, because it might try to find the distance of the main sprite.
What do you mean?
Bubbajoe57wastaken
Scratcher
37 posts

Nearest Clone(s)

medians wrote:

Bubbajoe57wastaken wrote:

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
The distance formula, look up “distance formula” and you'll see how. You can use the item # of block to get which clone it is, or another variable.

p-p-p-p-p-p-p-p-p-p- wrote:

Bubbajoe57wastaken wrote:

So I have a list of all of the clones, as well as their x and y positions, but I don't understand how to detect which one is closest to a sprite.
You could do this more simply:
Maybe put it on the clones, because it might try to find the distance of the main sprite.
How would I actually get the distance formula working in scratch?

Last edited by Bubbajoe57wastaken (Feb. 26, 2023 00:03:01)

medians
Scratcher
1000+ posts

Nearest Clone(s)

Bubbajoe57wastaken wrote:

How would I actually get the distance formula working in scratch?
Exponents are just repeated multiplication when working with positive integer exponents, so squaring can just be done like this:

So for the distance formula this can be done:

Or something really similar
And then for x1, you just put the x for the clone, same for y1 but with y, and then y1 and y2 are the x and y positions of the other sprite.

Last edited by medians (Feb. 26, 2023 01:01:54)

10data10
Scratcher
100+ posts

Nearest Clone(s)

I’m glad this topic didn’t get moved to Help with Scripts because I think it begs the question “is this at the ceiling of Scratch?”.

Bubbajoe57wastaken wrote:

How might I detect the nearest clone to a sprite? I have a list of all of the clones, but I'm not sure if I need to use it to detect the nearest clone.

You don’t actually have a list of clones, and that’s the problem. Scratch does not give us all the tools that we need to manage clones without using what I call Scratch microcode (lots of discrete processes putting data into lists that must be processed). If you had a list of clone as objects you could use the distance block directly for each clone and keep track of the clone that was closest.

The SNAP language can do that and I’ve written a little case study https://snap.berkeley.edu/project?username=10goto10&projectname=Nearest%20Clone If you look inside (use the Edit button) you’ll see what Scratch needs to give us to let us actually have a list of clones and not just a set of local variables that allow clones to tell each other apart.

Good luck with your project and like I said, since this is in the questions about Scratch forum I believe that my answer is still “on topic”. (And a guess I might have to edit my post if your topic get’s moved.)



Powered by DjangoBB