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

#1 2012-11-12 16:48:33

A2Exhbits
New Scratcher
Registered: 2012-11-12
Posts: 2

How to play sound on key press but don't repeat until key is released?

I'm looking for a way to do the following action:

When (action key) is pressed
play this (sound)
stop, don't repeat until (action key) is released

What happens when I set this up as:

when [space v] key pressed
play sound [meow v]
is that the sound keeps looping the whole time the action key is held down. I only want the sound to play once while the key is pressed, and if it is held down it won't keep looping. But if you release the key, then repress it, it will play the sound again once, and wait until it is released and pressed again.

Anyone know how to go about this? I have an thought about using a counter to count to 1 when the key is pressed, only playing the sound when the counter is equal to or less than 1, and resetting the counter when the key is released? Am I headed in the right direction?

Thanks,

Adam

Offline

 

#2 2012-11-12 16:59:38

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: How to play sound on key press but don't repeat until key is released?

Yes you are headed in the right direction.  big_smile

when gf clicked
set [sound v] to (0)

when key [space v] pressed
if <(sound) = (0)>
play sound [sound v]
set [sound v] to (1)
end

set [sound v] to (0)
Good luck on this project!  smile

Last edited by powerpoint56 (2012-11-12 17:00:22)


http://i48.tinypic.com/2072ctw.gif

Offline

 

#3 2012-11-12 17:14:14

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: How to play sound on key press but don't repeat until key is released?

powerpoint56 wrote:

Yes you are headed in the right direction.  big_smile

when gf clicked
set [sound v] to (0)

when key [space v] pressed
if <(sound) = (0)>
play sound [sound v]
set [sound v] to (1)
end
set [sound v] to (0)
Good luck on this project!  smile

That wouldn't wok because when you press space, yes, the sound will start playing, but then sound will get reset to 0, the script will run again, sound will get played again, and the problem won't be solved. Instead, after moving/changing a few blocks, you could get something that works:

when gf clicked
set [sound v] to (0)

when key [space v] pressed
if <(sound) = (0)>
set [sound v] to (1)
play sound [sound v] until done
set [sound v] to (0)
end
Anyway, I hope that this helps!


http://i46.tinypic.com/35ismmc.png

Offline

 

#4 2012-11-12 18:01:23

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: How to play sound on key press but don't repeat until key is released?

when gf clicked
set [sound v] to (0)
when key [space v] pressed
if <(sound) = (0)>
  set [sound v] to (1)
  play sound [sound v] until done
  wait until <not <key [space v] pressed?>>
  set [sound v] to (0)
end

Last edited by BoltBait (2012-11-12 18:03:19)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#5 2012-11-13 13:51:15

A2Exhbits
New Scratcher
Registered: 2012-11-12
Posts: 2

Re: How to play sound on key press but don't repeat until key is released?

Thanks to everyone for their input! BoltBait was right on the money!

Offline

 

#6 2012-11-13 14:06:28

Llamalover
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: How to play sound on key press but don't repeat until key is released?

Hi!
I don't know if you're already done and dusted with this, but there's a much simpler way to do this!


when gf clicked

forever  

if <key [space v] pressed?> 

play sound [meow v] until done

wait until <not <key [space v] pressed?>>

end

end


Be nice, I'm an old lady  wink

Offline

 

Board footer