Discuss Scratch

rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Ok. I was told that if I found myself repeating code over and over, I should probably do something a bit different, which is easy enough when I can type out what I want to do, which I cannot here.

I have one sprite that needs to call out the name of a musical instrument each time its clef is clicked. Since there are many, many instruments in the orchestra, I'm trying to avoid filling his sprite with repetitive code. I'm desperately trying to find “If sprite X is played, say the word X. If sprite Y is played, say the word Y.”

Somehow my self-defined blocks just ain't working. Any suggestions?
MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

I'm kind of confused on what you want to accomplish. I'm sorry; I'd love to help, could you put up an example project?
Edit: I see your project. Solution in a new post

Last edited by MicroMacro (Dec. 31, 2014 04:54:57)


MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

What you could do is that you could make a variable named “Selected Instrument” or something along those lines (doesn't matter what you name it, to be honest)

Then, when the clef is clicked, have the clef set the name of the variable to the name of the instrument, and then broadcast a message to let the speaker sprite know that it needs to say something.

Make sense?

rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Many thanks for the speedy reply! I have the general idea, but for the sprite that does the speaking, could I somehow work this into an if . . . then . . . else structure, or is that what you are saying? The Looping constructs take only certain types of variables and I haven't had much luck defining them and placing them in a loop.
MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

I'm sorry, I've lost track. For me, understanding programming concepts in regular language is hard for me.

Basically, here's how it would work. I'll be using the variable “instName”, but you can name it anything you want.

Clef Sprite's Code (using trumpet as example)
when this sprite clicked
stop all sounds
set volume to (50)%
play sound [trumpet v]
set [instName v] to [trumpet]
broadcast [Say Instrument Name v]

Speaker Sprite's Code
when I receive [Say Instrument Name v]
say (join [This instrument is the ] (instName)) for (2) secs

I think if you want to give information about the instrument, you'd need to do something like this:
when I receive [Say Instrument Name v]
say (join [This instrument is the ] (instName)) for (2) secs
if <(instName) = [trumpet]> then //Again, using trumpet as an example
say [This instrument blah blah blah...] for (2) secs
end

You'd then continue making those if statements, until you've covered all the instruments. It's not necessary to use the “if/else” blocks, just plop a bunch of ifs together, and you'll be fine.

I hope I helped!

Last edited by MicroMacro (Dec. 31, 2014 06:24:10)


rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Um. Wow! My pseudo solution was a lot clunkier! Thanks!
MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

No problem. Let me know if you need any more help. I'm glad to.

rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Should be working great, but for some reason, instead of saying, “These are the cellos,” he's saying “These are 1.” I checked on the cello variable under data objects and it too said “1” but everything in the code has the appropriate wording in it.

Thoughts?
MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

I know your issue.

What you've done is made a new variable called Cello, which has the data “1”

Instead of putting a variable there, just type the word “Cello” into the set variable block. It should look like this:

set [Selected Instrument v] to [Cello]

rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Did that. And while it did not fix the entire problem, I fixed the rest with an “if.” Tip of the hat to you! Many thanks!
MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

What else is wrong with it?

rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Actually. It didn't get fixed. I changed the text, per your suggestion. But the sprite script takes a variable. The cello variable on this list evaluates to 0 or 1, so I guess I can't use it anywhere?
rcdall
New to Scratch
21 posts

Script talking & Objects - Final Piece of Project

Got it. I was being an idiot. :\
MicroMacro
Scratcher
100+ posts

Script talking & Objects - Final Piece of Project

It's because you're doing it… oddly.

Here, I'll use the cellos and the violins as an example.

Cello Code:
when this sprite clicked
stop all sounds
set volume to (200)%
play sound [Cello v]
set [InstName v] to [Cello]
broadcast [Say Instrument Name v]

Violin Code:
when this sprite clicked
stop all sounds
set volume to (200)%
play sound [Violin v]
set [InstName v] to [Violin]
broadcast [Say Instrument Name v]

Now, code for the speaker:

when I receive [Say Instrument Name v]
if <(InstName) = [Cello]> then
say [This is the Cello.] for (2) secs //Add any additional information about the Cello in this if statement.
end
if <(InstName) = [Violin]> then
say [This is the Violin.] for (2) secs //Add any additional information about the Violins in this if statement
end
//Continue with the rest of the instruments

Make sense?

Last edited by MicroMacro (Dec. 31, 2014 06:53:55)


Powered by DjangoBB