Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Random Direction Script?
- MouseclickHD
-
6 posts
Random Direction Script?
Hi, I'm making a maze solver, and I need a script which basically goes “If this sprite is touching either the edge or the maze, make it turn either left, right, up, or down (chosen randomly)”.
I've tried so that when it is touching “edge” or “maze” it changes a variable called “direction” to an integer from 1 to 4 (so either 1,2,3 or 4).
If it's 1 it points up, if 2 it points down, if 3 left, if 4 right. This is inside an “if/else” script, in the “if” field, where the “else” field is “move 10 steps”.
But it doesn't work.
If anybody has a “random left/right/up/down direction” script they are willing to share, please do!
Thanks!
I've tried so that when it is touching “edge” or “maze” it changes a variable called “direction” to an integer from 1 to 4 (so either 1,2,3 or 4).
If it's 1 it points up, if 2 it points down, if 3 left, if 4 right. This is inside an “if/else” script, in the “if” field, where the “else” field is “move 10 steps”.
But it doesn't work.
If anybody has a “random left/right/up/down direction” script they are willing to share, please do!
Thanks!
- -Scratcher-
-
100+ posts
Random Direction Script?
Maybe you could post the scripts here, so it's easier to understnad. Do you mean like This is inside an “if/else” script, in the “if” field, where the “else” field is “move 10 steps”.
?
- MouseclickHD
-
6 posts
Random Direction Script?
I would try to show it easier, but Scratchblocks won't work hmph
- -Scratcher-
-
100+ posts
Random Direction Script?
Oh okay. You could replace the whole part inside “if” with the point in direction block, that makes it a lot easier. I see one problem with the sensing, because if the sprite touches an object and then changes it's direction, it still touches the object, so it would probably just rotate all the time. Is your maze grid based?
First, you could try
to make sure it goes back to path when touching the walls. To make it a little bit faster you can also use
Because the other block also contains the direction it already had before, but it should rather try the other three directions.
Last edited by -Scratcher- (May 23, 2015 14:50:46)
- G0577
-
55 posts
Random Direction Script?
this thread is handy, thank you for asking and getting answered!
- moss-shadow
-
500+ posts
Random Direction Script?
This is the most simple way to do it in all directions:
Last edited by moss-shadow (Jan. 1, 2016 01:42:10)
- Discussion Forums
- » Help with Scripts
-
» Random Direction Script?