Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Spots already taken by other sprites
- Kidary
-
5 posts
Spots already taken by other sprites
I want to command something like:
“If spot 2 is already taken, I want the sprite to go to a random position between 1,3,4 and 5.” I've tried the (set variable to ‘pick random 3-5’) but this doesn't account for spot 1
“If spot 2 is already taken, I want the sprite to go to a random position between 1,3,4 and 5.” I've tried the (set variable to ‘pick random 3-5’) but this doesn't account for spot 1
- ReallySopa
-
27 posts
Spots already taken by other sprites
It's because you didn't also calucate the number “1” in pick random (3) to (5), it should be “pick random (1) to (5)”, but if you want to expect a number like “2”, then do this:
- Nolswit
-
100+ posts
Spots already taken by other sprites
you could make it trigger with a broadcast and pick a number, and if that number is already picked, then it will run the code again over and over until it gets an unused number. I might be a little slow, though
- 10goto10
-
500+ posts
Spots already taken by other sprites
I usually use a list as a working set of open locations and delete items from the list as they are randomly picked.
Assuming you have your spot locations in two lists for X, Y coordinates.
Assuming you have your spot locations in two lists for X, Y coordinates.
Then the My X variable can be used in a go to block (and same for My Y variable)
- Kidary
-
5 posts
Spots already taken by other sprites
I usually use a list as a working set of open locations and delete items from the list as they are randomly picked.
Assuming you have your spot locations in two lists for X, Y coordinates.Then the My X variable can be used in a go to block (and same for My Y variable)
If spot 2 has already been taken but I use the block
and there are more than 2 items in the list, my sprite might overlap into 2 again.
I'm not sure how to fix that or whether I should use another block instead.
- 10goto10
-
500+ posts
Spots already taken by other sprites
The first time “2” is picked it will be deleted from the Spots Open X positions list. The Spot positions actually in the Spots Open X (and Y) positions list will be “1”, “3”, and “4”.
Did you try coding my example to see what it would do?
Did you try coding my example to see what it would do?
- Kidary
-
5 posts
Spots already taken by other sprites
Yes, I did. 2 is deleted off the list but then the next sprite that needs to choose a position sometimes goes into spot 2 because the list is more than 2 items in length
- 10goto10
-
500+ posts
Spots already taken by other sprites
I think you'll need to post a link to your project (after sharing it).
- Nolswit
-
100+ posts
Spots already taken by other sprites
what he is saying is that it take the length of the list and chooses a number between 1 and the length of the list. so for example the list has 3 items on it and we say get the number 2 so we remove 2 from the list. now the length of the list is 2 and there is a 50/50 chance that it picks 2 again. also if 3 is not the first thing picked them it will never be utilized. it has to go perfect 3,2,1 everytime or your method wont work
- 10goto10
-
500+ posts
Spots already taken by other sprites
I want to command something like:
“If spot 2 is already taken, I want the sprite to go to a random position between 1,3,4 and 5.”
what he is saying is that it take the length of the list and chooses a number between 1 and the length of the list. so for example the list has 3 items on it and we say get the number 2 so we remove 2 from the list. now the length of the list is 2 and there is a 50/50 chance that it picks 2 again. also if 3 is not the first thing picked them it will never be utilized. it has to go perfect 3,2,1 everytime or your method wont work
You seem to be conflating a position on the screen with a position on the list. If the problem is to avoid putting two sprites onto the same screen position then my code will work.
- Discussion Forums
- » Help with Scripts
-
» Spots already taken by other sprites