Hello;
i'm new to scratch and wrote a little
Repeat until keypress loop.
It does a set instrument to a variable (rand instrument)
plays notes then
change Rand Instrument by 4.
Problem is that the loop climbs to 128; i can't
quite figure how to "if Rand instrument = 128
then set instrument to 1.
I tried doing this in an if loop but the construction
is wrong or doesn't work.
THanks
Foob
(Later: it was my variable that ran amok; see below
for fixes.)
p.s. i will try to figure out uploading so people
can look at the code.
Last edited by UncleFoobar (2007-06-02 21:21:36)
Offline
see
http://scratch.mit.edu/projects/UncleFoobar/9433
for some reason the problem is NOT showing up
if you look at the web version / seems to cycle
fine.
There's talk of variables and costumes and some
of the deeper stuff (mod and such) that go
into a variable.
Foob
Offline
Huh; i just read the manual a bit and put the "display variable" out.
for some reason it's been incremented to 7469!
how do you detect a variable's value and reset when it get's to
a certain value?
Foob
Offline
Huh i think i know what happened; i didn't install the "Rand instrument = 128" until AFTER the counter had gotten higher than that.
Remember to click the little check box to check variable values!
Foob who learned something
Offline
It is common to use tests for x>127, rather than x=128 to avoid problems like the one you describe.
You might also want to look at using the "mod" function on the numbers pane.
Offline
Thanks for that Kevin i'll implement it.
Note All: the above code is my buggy version / the variable is up in the thousands. Kevin's suggestion would fix it or:
go to variables in the project
click the check box next to the Rand Instrument variable
note now the "count" is on the stage.
Slide it to zero. / you can hide the variable uncheck the above checked.
Foob
Offline
What exactly is a Mod? i think it's Modulo and some sort of calculator function; what does it mean?
it has two "imput" thingies so it's probably some sort of comparison.
Foob who could probably fire up wikipedia and get an answer
p.s.
http://en.wikipedia.org/wiki/Modulo
Hmmm, still not getting it…
Offline
When I learned division this was called a remainder. So if a number divides by another number evenly, its mod will be zero. Otherwise it will be the remaing portion that doesn't divide evenly. Some examples:
6 mod 3 = 0
7 mod 3 = 1
128 mod 127 = 1
Offline
The definitions you'll see of mod generally only talk about positive integers, but to do mod right, you need a definition that works for real x and y.
x mod y = x- floor(x/y)*y is one such definition.
floor(f) is the greatest integer <= f
Note that this definition of x mod y works for all real y that are not zero.
The result is between 0 and y---it may be 0 but never y.
Offline