Discuss Scratch

bhughesbhs
New Scratcher
3 posts

Use one key to toggle a loop on and off

Hi world,
I'm looking to create a code to use to create basic launchpad for music loops. I'd like to trigger an audio file/loop by pressing one key, have the playback continually loop itself until the same key is pressed again. Any suggestions?
gor-dee
Scratcher
1000+ posts

Use one key to toggle a loop on and off

I think you will have to use clones, otherwise the “stop all sounds” block will stop ALL sounds. If you have a sprite for each sound and have
when green flag clicked
set [sound] to [off ]
forever

wait until <key [key] pressed?>
if <[sound] = [off ]> then
create clone of [myself]

set [sound] to [on ]
if <[sound] = [ on]> then
set [sound] to [ off]
delete this clone
wait until <not <key [ key] pressed?>>

end

end

end

when I start as a clone
forever

play sound [ v] until done
end

that might work……………….
gor-dee
Scratcher
1000+ posts

Use one key to toggle a loop on and off

gor-dee wrote:

I think you will have to use clones, otherwise the “stop all sounds” block will stop ALL sounds. If you have a sprite for each sound and have
when green flag clicked
set [sound] to [off ]
forever

wait until <key [key] pressed?>
if <[sound] = [off ]> then
create clone of [myself]

set [sound] to [on ]
if <[sound] = [ on]> then
set [sound] to [ off]
delete this clone
wait until <not <key [ key] pressed?>>

end

end

end

when I start as a clone
forever

play sound [ v] until done
end

that might work……………….
wait until not key pressed should be right at the end of the forever loop
footsocktoe
Scratcher
1000+ posts

Use one key to toggle a loop on and off

bhughesbhs wrote:

Hi world,
I'm looking to create a code to use to create basic launchpad for music loops. I'd like to trigger an audio file/loop by pressing one key, have the playback continually loop itself until the same key is pressed again. Any suggestions?


If you don't mind the loop finishing itself before it stops, you can create a variable called “play” and use…
set [play] to [1 ]



when this sprite clicked
set [play] to ((play) * (-1))
repeat until <(play) > [0 ]>

play sound [ v] until done
end
This works because the variable “play” is being toggled between -1 and +1 on alternate clicks on the sprite.

Last edited by footsocktoe (Feb. 4, 2016 10:34:40)

minecrafterisaac
Scratcher
7 posts

Use one key to toggle a loop on and off

gor-dee wrote:

I think you will have to use clones, otherwise the “stop all sounds” block will stop ALL sounds. If you have a sprite for each sound and have
when green flag clicked
set [sound] to [off ]
forever

wait until <key [key] pressed?>
if <[sound] = [off ]> then
create clone of [myself]

set [sound] to [on ]
if <[sound] = [ on]> then
set [sound] to [ off]
delete this clone
wait until <not <key [ key] pressed?>>

end

end

end

when I start as a clone
forever

play sound [ v] until done
end

that might work……………….
i dont think so
deck26
Scratcher
1000+ posts

Use one key to toggle a loop on and off

minecrafterisaac wrote:

gor-dee wrote:

I think you will have to use clones, otherwise the “stop all sounds” block will stop ALL sounds. If you have a sprite for each sound and have
when green flag clicked
set [sound] to [off ]
forever

wait until <key [key] pressed?>
if <[sound] = [off ]> then
create clone of [myself]

set [sound] to [on ]
if <[sound] = [ on]> then
set [sound] to [ off]
delete this clone
wait until <not <key [ key] pressed?>>

end

end

end

when I start as a clone
forever

play sound [ v] until done
end

that might work……………….
i dont think so
Why comment on a 4 year old topic?
numbercruncher324
Scratcher
29 posts

Use one key to toggle a loop on and off

gor-dee wrote:

I think you will have to use clones, otherwise the “stop all sounds” block will stop ALL sounds. If you have a sprite for each sound and have
when green flag clicked
set [sound] to [off ]
forever

wait until <key [key] pressed?>
if <[sound] = [off ]> then
create clone of [myself]

set [sound] to [on ]
if <[sound] = [ on]> then
set [sound] to [ off]
delete this clone
wait until <not <key [ key] pressed?>>

end

end

end

when I start as a clone
forever

play sound [ v] until done
end

that might work……………….

well I want a modern day one so can you please give me a modern ver. tutorial, if you already have one, please bring me to it
numbercruncher324
Scratcher
29 posts

Use one key to toggle a loop on and off

gor-dee wrote:

I think you will have to use clones, otherwise the “stop all sounds” block will stop ALL sounds. If you have a sprite for each sound and have
when green flag clicked
set [sound] to [off ]
forever

wait until <key [key] pressed?>
if <[sound] = [off ]> then
create clone of [myself]

set [sound] to [on ]
if <[sound] = [ on]> then
set [sound] to [ off]
delete this clone
wait until <not <key [ key] pressed?>>

end

end

end

when I start as a clone
forever

play sound [ v] until done
end

that might work……………….

also that is very confusing because LOOK AT THE BOTTOM OF THE FIRST GROUP OF BLOCKS (STRING)

Last edited by numbercruncher324 (Dec. 28, 2024 18:01:43)

Powered by DjangoBB