This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-04-05 19:46:30

PazAiko
Scratcher
Registered: 2008-04-05
Posts: 6

Randomly picking sprites

I'm brand new to Scratch and although I have done some tutorials, what I have been trying to figure out is how to make one sprite appear randomly at a time. I have one main sprite (lightening bolt) that needs to interact with all the other sprites (about 12 of them) but one at a time. I can get one to appear at a time but I would love to add the randomly picked sprite aspect to my game. Thoughts and explicit instructions or a recommended game to look at that does the same thing would be appreciated.

-Claire

Offline

 

#2 2008-04-05 19:55:56

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Randomly picking sprites

You can make them all appear random seconds from the start. First, add this code to all the sprites you need to interact with the bolt of lightning:

[blocks]
<when[ green flag ]clicked>
<hide>
<wait( <pick random(1)to(30)secs>
<show>

This should make a sprite appear randomly between 1 and 30 seconds after the flag is clicked.

Offline

 

#3 2008-04-05 19:58:40

PazAiko
Scratcher
Registered: 2008-04-05
Posts: 6

Re: Randomly picking sprites

Thanks, I was sort of on to that, what I really need to do is have each sprite wait until the main sprite has interacted with the prior sprite before moving on to the next one and so on through all of them. And then I would like one of the other sprites to appear. Sort of like interacting with shuffled flashcards in real life.

Offline

 

#4 2008-04-05 20:19:55

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Randomly picking sprites

So you want them to appear in a random order?

Offline

 

#5 2008-04-05 20:25:44

PazAiko
Scratcher
Registered: 2008-04-05
Posts: 6

Re: Randomly picking sprites

Yeah in random order, and let the main sprite interact with them one at a time.

Offline

 

#6 2008-04-05 20:33:55

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Randomly picking sprites

Okay, for that I'll need to do some 'testing'

Offline

 

#7 2008-04-05 21:13:13

PazAiko
Scratcher
Registered: 2008-04-05
Posts: 6

Re: Randomly picking sprites

Thanks very much for your help.

Offline

 

#8 2008-04-05 21:31:02

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Randomly picking sprites

I'll try my best

Offline

 

#9 2008-04-05 21:41:41

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Randomly picking sprites

I think you might be able to do something where the master sprite sends out a request message to all the other sprites.  When the other sprites get the request, the ones that haven't already been used wait a random number of seconds and then the first one that responds is used next.  This would repeat until all the sprites had been used. 

This would be a fairly complex bit of coding as it would require a couple of variables to keep track of who has been used and when a request has be fulfilled and so on.  Let me know if you want me to take a whack at it!


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#10 2008-04-05 21:46:34

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Randomly picking sprites

Or, another approach would be to use my simple array

http://scratch.mit.edu/projects/Paddle2SeeFixIt/91316

and randomize it.  Then, pull numbers off of it in sequence and use the sprite associated with the number.  I have a larger version (52 elements) in my Buried Treasure game, if you need more than 10 elements.

Last edited by Paddle2See (2008-04-05 21:47:11)


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#11 2008-04-05 22:07:41

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Randomly picking sprites

Or you could do it my way:

Make a variable called
[blocks]
<{ random }>
[/blocks]
then on the stage put this script:
[blocks]
<when green flag clicked>
<broadcast[ startrand ]>


<when I receive[ startrand ]>
<set{ random }to( <pick random( 1 )to( 12 ) )>
<if><( <{ random }> <=> 1 )>
<broadcast[ spr2 ]>
<end>
<if><( <{ random }> <=> 2 )>
<broadcast[ spr3 ]>
<end>
[/blocks]
Etc. until you have an if statement for everything from 1-12 and a broadcast thing for everything from 1-12.

Then, øn each sprite that must interact with the main sprite, put the respective code. For example if it is sprite2, then use this script:

<when green flag clicked>
<hide>
<go to x sad  starting spot )y sad  starting spot )>

<when I receive[ spr1 ]>
<show>
<wait until><touching[ main sprite ]>
<broadcast[ startrand ]>
<hide>

and for sprite3, use:

<when green flag clicked>
<hide>
<go to x sad  starting spot )y sad  starting spot )>

<when I receive[ spr2 ]>
<show>
<wait until><touching[ main sprite ]>
<broadcast[ startrand ]>
<hide>
[/blocks]
etc. until you have that on every sprite. This should work because if it gets the same random number twice, then it would already be touching the main sprite and would then pick another random number. It might flash for a small amount of time on occasion, but otherwise it should work fine unless your 'interaction with main sprite' is something other than touching it.

Offline

 

#12 2008-04-06 00:34:57

PazAiko
Scratcher
Registered: 2008-04-05
Posts: 6

Re: Randomly picking sprites

Coolstuff,
I tried out your method and i'm going to try and work on it to see if I can manipulate it so i can use it for my game, it really works though! I think I just need to complicate it to make all the sprites act the way i want them to.
Paddle2See,
I am checking out your buried treasure game right now.
Thanks for the advice so far everyone.

Offline

 

#13 2008-04-06 01:10:51

PazAiko
Scratcher
Registered: 2008-04-05
Posts: 6

Re: Randomly picking sprites

Ok, so I combined my system of my main sprite correctly messaging my other sprites, but how do i get the random generator to only pick each sprite once?

Offline

 

#14 2008-04-06 06:04:57

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Randomly picking sprites

PazAiko wrote:

Ok, so I combined my system of my main sprite correctly messaging my other sprites, but how do i get the random generator to only pick each sprite once?

You have located the hard part of the problem!  That is where an array solution really comes into play:  it's like working with a deck of cards, you shuffle the cards and when you deal them out one at a time, you know you are only going to get each card once.  There is probably a way to solve the problem without using an array, maybe with flag variables, but I still recommend that you look at my Small Array project

http://scratch.mit.edu/projects/Paddle2SeeFixIt/91316

Actually, after more thought, I came up with a much cleaner approach.  See my post below or go to this project

http://scratch.mit.edu/projects/Paddle2SeeFixIt/135957

Last edited by Paddle2See (2008-04-06 08:11:36)


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#15 2008-04-06 07:45:44

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Randomly picking sprites

A clunky method is to choose a random number for your first sprite. then put your method of choosing arandom number for your second sprite into a "reapeat until not = "first sprite" loop.

Then for the 3rd sprite you have "repeat until not=first sprite or second sprite"

etc.

Its a very clumy way of doing it, mind you.


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#16 2008-04-06 08:10:34

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Randomly picking sprites

I just came up with this really cool solution that doesn't use arrays at all, just a few variables!  Check it out:

http://scratch.mit.edu/projects/Paddle2SeeFixIt/135957

Each sprite assigns itself a number using a short random wait to make sure it is different each time.  They pick the number from an incrementing global variable to make sure they are in sequence, but different from each other.  So I am using the global variable as the "deck of cards" but each sprite waits a random amount of time before picking it's card. 

Then, the master sprite simply increments a counter and asks the first sprite to start moving.  Once that sprite reaches the master, it signals the second sprite and so on.  But, remember, the order was assigned randomly so which sprite is first, second and so on, changes every time you run it.

Last edited by Paddle2See (2008-04-06 09:16:45)


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#17 2008-04-06 10:33:29

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: Randomly picking sprites

Well, with my method, if it does pick the sprite twice, then it is already touching the first sprite and should only flash once. Once all of the sprites have been picked, then you may have a little flashy problem.

Offline

 

Board footer