Lingo Tutorial: Randomness

File "go_nuts.dir"

This example shows the use of function random(max_choices) which returns a number between 1 and max_choices. The example is in terms of the game "go nuts": You try to click the smiley face. Every time you do, you hear a beep. (You can extend it to show you how many times you have succeeded in beeping, if you want.

It looks like an impossible game, but you can actually score high easily. (Why?)
Here is the important script. Study it and then determine where to you put it (in a cast member? a sprite? a frame? the movie?)

on hideAndSeek
   repeat with x = 1 to 5
     chances = random(4) 
     if (chances = 1) then -- give 25% chances of appearing
        sprite(x).member = "smile"
     else
        sprite(x).member = "black"
     end if
   end repeat
end                                                            

File "hide_seek.dir"


Now, here is an impossible game. This example shows the use of the boolean function rollover(a_sprite) which returns true if the cursor is on top of a_sprite and false otherwise.Usually you do not need it because you can have the same effect with on mouseEnter, mouseWithin and mouseLeave events. However, in this case, it helps to use rollover() because you need to make an overall decision of one sprite being visible while all the rest are invisible. The script is shown below.

Can you see why it is impossible? Suggest a way of making it a bit more fair (e.g., by giving the user a chance of actually hitting the face from time to time).

if rollover(currentOne) then
   sprite(currentOne).member = "black"
   -- find another
   nextOne = random(5)
   if nextOne = currentOne then 
      nextOne = 1 + (currentOne + 1) mod 5
   end if
   sprite(nextOne).member = "smile"
   currentOne = nextOne
end if                                   

 

 

Maintained By: Takis Metaxas
Last Modified: October 26, 2009