Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Distance
- BenAndMe
-
Scratcher
100+ posts
Distance
How on a variable could I track how far away a different clone (only 1) is is from a clone (Could be 10, or more) (The variable is local so can have a score for each clone).
- ECLIPSE-STUDIOS
-
Scratcher
100+ posts
Distance
Hi!
Create a variable called clone_x and clone_y.
In the clone scripts, tell the clone to set clone_x to its x position and clone_y to its y position.
Then, in the original sprite, use:
That should work - let me know if it doesn't.
I hope this helps!
Create a variable called clone_x and clone_y.
In the clone scripts, tell the clone to set clone_x to its x position and clone_y to its y position.
Then, in the original sprite, use:
set [x_dist v] to (((clone_x) - (x position)) * ((clone_x) - (x position)))
set [y_dist v] to (((clone_y) - (y position)) * ((clone_y) - (y position)))
set [Distance v] to ([sqrt v] of ((x_dist) + (y_dist))
That should work - let me know if it doesn't.
I hope this helps!
- deck26
-
Scratcher
1000+ posts
Distance
You'll need to store the x and y position of each clone and use Pythagoras to calculate the distance or have a spare sprite go to each x y position so you can use the ‘distance to sprite’ block to get the distance.
- BenAndMe
-
Scratcher
100+ posts
Distance
I have this simple code but it only checks distance from original no clones
The sprite it is trying to find the distance to is not a clone, sorry I mislead you but I though I would say this because the code you gave me might now work now.
when green flag clicked
forever
set [NM_Rdistance v] to (distance to [Sprite3 v])
end
The sprite it is trying to find the distance to is not a clone, sorry I mislead you but I though I would say this because the code you gave me might now work now.
- Despicable_Dad
-
Scratcher
500+ posts
Distance
Here's an example using both methods, and using lists to handle multiple clones: https://scratch.mit.edu/projects/194583854
- ECLIPSE-STUDIOS
-
Scratcher
100+ posts
Distance
I have this simple code but it only checks distance from original no cloneswhen green flag clicked
forever
set [NM_Rdistance v] to (distance to [Sprite3 v])
end
The sprite it is trying to find the distance to is not a clone, sorry I mislead you but I though I would say this because the code you gave me might now work now.
Ah, OK. I was doing it the other way round (ie the sprte works out its distance from the clone rather than the other way round) - yes, your way is way simpler and I probably should've thought of that first!
- Discussion Forums
- » Help with Scripts
-
» Distance



