Discuss Scratch

Eddomannen
New to Scratch
1 post

How to make character make random sounds as it moves?

Hi, just fooling around a little trying to make a character that maybe eventually can shoot against random enemies or something like that.

This is what happens when the left arrow key is pressed.
forever
if <key [left arrow v] pressed?> then
change x by (-10)
switch costume to [ v]
play sound (item (random v) of [list v] :: list)
end
end

It changes costume well (it looks like the character is now facing the left) and it also moves as I want, the only problem right now is the sound. It´s supposed to make a sound randomized from a list of four (I have a list of four sounds to make for each direction the guy can go in) but when I try it, it plays three out of four of the sounds at the same time and that´s not what I want!
Can somebody here help me identify the problem and tell me what I should do instead?

First post here, sorry if it´s unorganized
Thanks in advance~
/Edvin
FuturePr0
Scratcher
100+ posts

How to make character make random sounds as it moves?

That won't work, you don't need a list to do it. delete theat list and replace it with this:
play sound (pick random (1) to (1))  until done
you can change the max range. and min range.
Also, can you please share the project so we can help you more? and this should be on ‘help with scripts’.

Last edited by FuturePr0 (Feb. 8, 2018 11:31:46)


IMPORTANT!
Please read this, it will be helpful for both of us.
I haven't made a project with actual code for nearly a year, so I have completely forgotten almost everything about computers and programming, please help revive my once really massive knowledge on computing again by pointing out any errors I have made.
And also:
This is my signature, it automatically appears under everything I say. To make your new signature now, go here: https://scratch.mit.edu/discuss/settings/yourusernamehere
Type in your username in the ‘yourusernamehere’ section of the url.
To tell if something is a signature, you should see a small faint gray line that separates my signature and my actual post
PrincessFlowerTV
Scratcher
1000+ posts

How to make character make random sounds as it moves?

FuturePr0 wrote:

and this should be on ‘help with scripts’.
I agree. I'll report this to be moved for you.

PrincessFlowerTV ✿
~~ Teen ~~ Christan ~~ Homeschooled ~~ Forum Helper ~~ Wiki Editor ~~


Check out my blog you won't regret it :3
deck26
Scratcher
1000+ posts

How to make character make random sounds as it moves?

You probably don't want the sound to be in the same loop as checking for the key press as either the sound will keep restarting or you'll have to tell it to play the sound until finished and can't detect any key press in the meantime. But are other keypresses going to use the same set of sounds or a different set?

Assuming any key press uses the same sound set you could use a variable to indicate whether something is playing.

set [playing v] to [0]
forever
if <key [left arrow v] pressed?> then
change x by (-10)
switch costume to [ v]
if <(playing) = [0]> then
broadcast [newsound v]
end
end
end

when I receive [newsound v]
set [playing v] to [1]
play sound (pick random () to (10)) until done
set [playing v] to [0]


Powered by DjangoBB

Standard | Mobile