i have the following code:
in one sprite:
forever
if touching sprite
broadcast msg
wait 0.01
On the other sprite:
when i recieve msg
wait 0.01
if playing = 0
set playing 1
switch costume a
play sound 60 for 0.25
set playing 0
switch costume b
Where playing is a local var to the other sprite.
What happens is after the play executes the set playing 0 and switch costume are ignored and never execute. Don't know why this would be.
I tried rearranging the blocks, to no avail.
I was trying to guard against the reentrancy of the message block and keep it from playing multiple notes while sprite was traversing other sprite.
Any clues?
Thanks.
Offline
You are getting a stream of broadcasts at about 100 per second, which keep restarting the script. Broadcast-and-wait is probably what you want here.
Offline
Broadcast and wait makes the situation worse. Imagine two sprites passing over (or through) one-another, as they move across each other a sound is played and then motion continues after the sound is played. It looks and sounds pretty bad.
All I was looking for was 1 sound.
This is not a problem if there is a rebound (change of direction) on impact -- away from the other sprite.
Also not a problem if one sprite consumes/eats the other sprite making it disappear.
But, in my situation I would like to guard/block against the 100 per second messages being blasted in - and only take the first one.
How do I do this in the Scratch language?
Thanks.
Offline
You could wait until the sound is done before allowing another sound to start.
This requires that whatever is triggering the sounds not check again until the sound is done. It can be done in various ways, but will probably involve one of the "wait" commands:
wait x seconds
wait until <ok to check>
broadcast <start sound> and wait
play sound and wait
Offline
Create a variable "Touching".
I think this would work:
Set "touching" = 0
Forever
- If touching Sprite AND "touching" = 0
- - Set "touching" to 1
- - broadcast message.
- If NOT touching Sprite AND "touching" = 1
- - Set "touching" to 0
- wait (arbitary value to stop forever loop fouling up)
The broadcast will only happen when the two sprites first touch, until they have been seperated.
Offline
better:
forever if touching sprite
play sound (or broadcast message)
wait until not touching sprite
Offline
The if touching/not touching did the trick. Thanks.
I still think there is a bug in the code which fails to execute the two lines of code as mentioned in my first post.
The results of this are posted as this project:
http://scratch.mit.edu/projects/jsimone/15732
Rather than having to go through effort of animating things like bouncing drum heads or piano keys, I should think there would be a way to share these "parts".
Offline