Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to set a variable to a random value that is not close to another variable
- TheRealNetherBefore
-
Scratcher
1000+ posts
How to set a variable to a random value that is not close to another variable
I am trying to make a script that sets a sprite's position to a random one however makes sure that it is not within a set distance of another sprite. I decided that the best way to do this would be by using variables to set the positions of the sprites.
This is my current script so far:

Any idea where I should go from here?
This is my current script so far:

Any idea where I should go from here?
- Thefrenchpeople
-
Scratcher
500+ posts
How to set a variable to a random value that is not close to another variable
Something like this maybe
repeat until <((exitx) - (exity)) > [50 (Or whatever number)]>
end
Last edited by Thefrenchpeople (June 29, 2018 16:23:43)
- TheRealNetherBefore
-
Scratcher
1000+ posts
How to set a variable to a random value that is not close to another variable
Something like this maybeExitx and exity are not the two variables I'm comparing (I'm comparing them with a separate set of x and y variables) but I'll try that.repeat until <((exitx) - (exity)) > [50 (Or whatever number)]>
end
- TheRealNetherBefore
-
Scratcher
1000+ posts
How to set a variable to a random value that is not close to another variable
I've tried this however the script seems to break when both of the other x and y variables are positive. I'm assuming this is a mathematical error.Something like this maybeExitx and exity are not the two variables I'm comparing (I'm comparing them with a separate set of x and y variables) but I'll try that.repeat until <((exitx) - (exity)) > [50 (Or whatever number)]>
end
- Thefrenchpeople
-
Scratcher
500+ posts
How to set a variable to a random value that is not close to another variable
Maybe like this then
repeat until <([abs v] of ((Variable1) - (Variable2))) > [50]>
end
- red_king_cyclops
-
Scratcher
500+ posts
How to set a variable to a random value that is not close to another variable
Below is are some example variables and a “reporter” that reports the distance between two coordinate points (using the Pythagorean theorem):
x1 and y1 are the first set of coordinates and x2 and y2 are the second set of coordinates. They are example variables.
Here is the reporter being used in a custom block:
The reporter and the custom block above could come in handy for your project. And when 3.0 comes around, you could also make a custom reporter.
(x1)
(y1)
(x2)
(y2)
([sqrt v] of ((((x2) - (x1)) * ((x2) - (x1))) + (((y2) - (y1)) * ((y2) - (y1)))))
x1 and y1 are the first set of coordinates and x2 and y2 are the second set of coordinates. They are example variables.
Here is the reporter being used in a custom block:
define Find distance between x: (x1), y: (y1) and x: (x2), y: (y2)
set [distance v] to ([sqrt v] of ((((x2) - (x1)) * ((x2) - (x1))) + (((y2) - (y1)) * ((y2) - (y1)))))
(distance ::variables)
The reporter and the custom block above could come in handy for your project. And when 3.0 comes around, you could also make a custom reporter.
(distance between x: (), y: () and x: (), y: ()::custom blocks)
Last edited by red_king_cyclops (June 29, 2018 17:21:18)
- TheRealNetherBefore
-
Scratcher
1000+ posts
How to set a variable to a random value that is not close to another variable
-snip-Thanks! I've been able to solve my problem using this script! Asking this to be closed.
- red_king_cyclops
-
Scratcher
500+ posts
How to set a variable to a random value that is not close to another variable
-snip-Thanks! I've been able to solve my problem using this script! Asking this to be closed.
You're welcome.
- Discussion Forums
- » Help with Scripts
-
» How to set a variable to a random value that is not close to another variable


