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
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
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
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!
Offline
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)
Offline
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 starting spot )y 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 starting spot )y 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
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
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)
Offline
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.
Offline
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)
Offline
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