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

#1 2007-06-02 05:08:46

UncleFoobar
Scratcher
Registered: 2007-06-02
Posts: 7

Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#2 2007-06-02 05:29:34

UncleFoobar
Scratcher
Registered: 2007-06-02
Posts: 7

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#3 2007-06-02 05:39:28

UncleFoobar
Scratcher
Registered: 2007-06-02
Posts: 7

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#4 2007-06-02 05:45:51

UncleFoobar
Scratcher
Registered: 2007-06-02
Posts: 7

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#5 2007-06-02 18:43:35

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#6 2007-06-02 21:20:46

UncleFoobar
Scratcher
Registered: 2007-06-02
Posts: 7

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#7 2007-06-02 21:23:53

UncleFoobar
Scratcher
Registered: 2007-06-02
Posts: 7

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#8 2007-06-02 21:46:22

weissjd
Scratcher
Registered: 2007-05-16
Posts: 64

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

#9 2007-06-03 11:38:57

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Using Variable with Sound loop; gets stuck on #128 gunshot

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

 

Board footer