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

#1 2007-06-15 15:45:37

jsimone
Scratcher
Registered: 2007-05-20
Posts: 28

bug in when i receive message play sound

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

 

#2 2007-06-15 17:07:14

weissjd
Scratcher
Registered: 2007-05-16
Posts: 64

Re: bug in when i receive message play sound

Try changing the code in the first sprite to Broadcast and Wait. I've seen issues where the code in the broadcast doesn't happen if you don't wait for it.

Offline

 

#3 2007-06-16 11:58:22

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: bug in when i receive message play sound

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

 

#4 2007-06-20 11:53:09

jsimone
Scratcher
Registered: 2007-05-20
Posts: 28

Re: bug in when i receive message play sound

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

 

#5 2007-06-20 13:44:31

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: bug in when i receive message play sound

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

 

#6 2007-06-20 14:24:43

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

Re: bug in when i receive message play sound

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.


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

 

#7 2007-06-20 20:27:25

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: bug in when i receive message play sound

better:


forever if touching sprite
    play sound (or broadcast message)
    wait until not touching sprite

Offline

 

#8 2007-06-22 10:47:23

jsimone
Scratcher
Registered: 2007-05-20
Posts: 28

Re: bug in when i receive message play sound

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

 

Board footer