TIFFANY HOLMES>Repeat loops and lists>SPRING04>HANDOUT 4

 

EVENTS>Examples of event handlers>You want to play a sound when the user clicks a button:

 

EXAMPLE: This event is placed in a movie script (orange script). If the user hits the key ÒzÓ the movie backs up to the previous marker, any other key hit advances the movie.

 

on keyDown

    if the key = "z" then

    puppetSound 2, "Clicksound"

    go previous

  else

    puppetSound 2, "Clicksound"

    go next

  end if

end

 

REPEAT LOOPS: An excellent way to make a change, the same change to multiple sprites

 

EXAMPLE: When the user hits the key ÒfÓ the sprites in channels 5-10 change position

 

on keyDown

  if the key = "f" then

    repeat with s=5 to 10

            sprite(s).loc=point(random(320), 120)

    end repeat

  end if

end

 

ARRAYS, OR LISTS: A way to capture multiple pieces of data in an organized manner. Note: Here we are looking only at linear lists (one piece of data per list item), as opposed to property lists (two pieces of data per list item).

 

EXAMPLE: This script sets up a sound list and then pulls a soundÑin this case the first sound file in the cast called ÒburpÓÑ from the list to play when the user hits the ÒfÓ key.

 

on keyDown

  if the key = "f" then

    soundlist=[ÒburpÓ, ÒbabbleÓ, ÒwhistleÓ, ÒchirpÓ]

    puppetsound 2, getAt(soundlist, 1)

  end if

end

 

EXAMPLE: This script sets up a sound list and then pulls a random sound from the list to play when the user hits the ÒfÓ key.

 

on keyDown

  if the key = "f" then

    soundlist=[ÒburpÓ, ÒbabbleÓ, ÒwhistleÓ, ÒchirpÓ]

    puppetsound 2, getAt(soundlist, random(3))

  end if

end

 

HELPFUL INFORMATION ABOUT LISTS IN LINGO: There are countless things you can do with lists in Lingo.  I would suggest reading carefully the section of the Lingo help guide about lists (displayed in class.)