Lingo bits - loch, sprite #, randomness, repeat loop

Memorize the following two things:

on mouseup - what event is this the Lingo name for? A mouse click (releasing the mouse).
on exitframe - what event is this the Lingo name for? Leaving a frame in the score as the movie plays.

The 4 places that can have a script on them in Director:

1) sprite
2) frame
3) movie - soon we will start doing this
4) cast member (please try to put scripts on a sprite instead)

----------
----------

The following is the scripting example we worked through during class:

Setup of the Director movie:

In Director, create a movie with 2 cast members, and a stage size of 320x240.
The 2 cast members need to go on the stage at the same time, so make sure they are not too large.
Set up the score so that there are 3 sections:
- The 1st section has both cast members on the stage, and will be where you choose to click on them and navigate to the other 2 sections.
- The other 2 sections will each have one of the cast members on the stage.
- Make the lingo navigation between the 3 sections work -- so that you can click to one of the other 2 sections from the 1st section, but that you can click back to the first section from those other 2 sections.
- save the movie on the hard drive.

Once this is working, do this:

- put a script in the frame directly under one of your markers - either under section 2 or 3.
Double-click in the scripting channel to attach a script there - it opens the script window.

- change on exitFrame in the open script window to on enterFrame

- Type in the following script:

on enterframe
  put random(320) into x
  sprite(1).loch = x
end

Where does sprite(1) comes from? A sprite that is in channel 1 is referred to as sprite(1). A sprite that is in channel 4 is referred to as sprite(4). Please memorize this principle!

- Once you have rewound your movie, and seen it work, save the movie under a different name!

- open the same script again and consider the different syntaxes of Lingo:

put random(320) into x    VS    x = random(320)    <-- these two lines mean the same thing
verbose/english-based             newer dot syntax

- type in the dot syntax version
- using the comment button in the script window, comment out the verbose syntax line
- Once you have rewound your movie, and seen it work, save the movie under a different name!

- think about x as it is shown in the script - what is it? It is a variable. A variable is a way to hold information or a setting that is useful for something and will vary each time the script runs.

- you could change x to a different variable - like rn - you would just have to change each line where x appeared and replace x with the new variable (I'll use rn)
Variable name rule: must start with a letter, can have numbers in it, no spaces or funny symbols, can't be a word already used in lingo syntax.

- Rewind your movie, and check that it still works


- There is a shorter way to do the work of the two lines of commands in the script:
sprite(1).loch = random(320) <-- this can replace the two lines between on enterframe and end
- for this example, please type in the above, replacing the two lines already there
- Once you have rewound your movie, and seen it work, save the movie under a different name!


- if we wanted to set several sprites to get set to the SAME random number for their loch as the first sprite, we would want to keep using the variable version of our lines (using the random for each sprite would not guarantee each sprite the same loch) - the variable would let us keep the setting we got once and then reuse it!

- let's say we DO want to set a whole bunch of sprites, but let's say we want to set each one to its own random location - do we need the variable, or could we write it all in one line?
Answer: we could write each one in its own line, like below:

- add more sprites to your stage in that section - let's add 5!
Change your script to:

on enterframe
   sprite(1).loch = random(320)
   sprite(2).loch = random(320)
   sprite(3).loch = random(320)
   sprite(4).loch = random(320)
   sprite(5).loch = random(320)
end
- Be aware that this script assumes you have sprites in channels 1-5
- Once you have rewound your movie, and seen it work, save the movie under a different name!


- can you imagine doing the above with 100 sprites?!
- here's an even more efficient way to accomplish the same thing...

Using a repeat loop (a useful structure)
- you use a repeat loop when you can see that you are repeating the same action again and again in a script, but there is only one change to that action each time it repeats.
Change your script to:

on enterframe
   repeat with rn = 1 to 5    
     sprite(rn).loch = random(320)
   end repeat
end

- Why does it say repeat with rn = 1 to 5? Because the first time the repeat loop repeats, rn will be 1, the second time the repeat loop repeats rn will be 2, the third time 3, etc., until it gets to 5. This still is assuming that you have sprites in channels 1-5.
- If you had 100 sprites (in channels 1 to 100) all you would have to change about the repeat loop is the number 5 (you'd change it to 100). So you see how efficient a repeat loop can be!
- Once you have rewound your movie, and seen it work, save the movie under a different name!


The logic of the setup, the numbers and what's been going on... Now where did this 320 come from that we keep using in the script? Why random(320)?
Answer: what is the width of the stage in pixels? 320.

- How Director calculates sprite location:
On the Stage:
- Horizontally: in pixels from the left side of the stage (how many pixels in from the left)
- Vertically: in pixels from the top of the stage (how many pixels down from the top)
Where On the Sprite:
- registration point - you can see it in the paint or vector windows, you can also set it in those windows

Lets set the script to do random vertical settings instead of random horizontal settings.
- How would we do that? Answer: change all loch to locv instead, and change random(320) to something based on the height of the stage --> random(240)

- Once you have rewound your movie, and seen it work, save the movie under a different name!


How would you go about setting both locv and loch for each sprite?
Change your script to:

on enterframe
   repeat with rn = 1 to 5    
     sprite(rn).loch = random(320)
     sprite(rn).locv = random(240)
   end repeat
end
- Once you have rewound your movie, and seen it work, save the movie under a different name!


- on enterframe VS on prepareframe
Change the script so that it uses on prepareframe instead of on enterframe (so that you can't see their positions being switched, you just click and jump to the section and they are switched before the frame is displayed.)
- Once you have rewound your movie, and seen it work, save the movie under a different name!



- More Advanced Challenge: What if you didn't want the sprites to be partway off the stage?
Controlling randomness within certain limits...

Notice how sprites may be partway off of the stage; this is because of the way that Director calculates sprite location. Remember the registration point? Where is it on your sprite? Most often it is dead center, so that there are many pixels of sprite on each side of the registration point. But when Director is setting the location of the sprite, it sets the registration point of the sprite to the number it has for the loch or locv settings...
So, if random(320) gets us a result of 1 for loch, the registration point of the sprite gets set to 1, and most of the left side of the sprite sticks off the stage. At the opposite end of the scale, if random(320) gets us a result of 320 for loch, the registration point of the sprite gets set to 320, and most of the right side of the sprite sticks off the stage.

To control this problem, you have to know the dimensions (in pixels) of your sprite. You can find its measurements in the property inspector (W [for width] and H [for height] give you the dimensions). To keep the sprite on stage horizontally, you work with the width of the sprite; vertically, you work with the height of the sprite.
So make a note of where the registration point is, the width and height of the sprite, and figure out how many pixels are on each side of the registration point (horizontally and vertically).

For example: Let's say that all of my 5 sprites are the same width and height. Let's say that each sprite is 36 pixels wide, 20 pixels high. And let's say the registration point is dead center.
Therefore: the number of pixels on each side of the registration point horizontally is 18; the number of pixels on each side of the registration point vertically is 10.

So, for a stage size of 320x240:
- the minimum loch setting for the sprites would be 18
- the maximum loch setting for the sprites would be 302 (320 - 18 = 302)

Changing the script to the following fixes the loch settings so they won't let the sprite stick out off the stage:

on enterframe

  repeat with rn = 1 to 5 
    sprite(rn).loch = random(302)
    if sprite(rn).loch < 18 then
       sprite(rn).loch = 18
    end if
  end repeat

end


And, for a stage size of 320x240:
- the minimum locv setting for the sprites would be 10
- the maximum locv setting for the sprites would be 230 (240 - 10 = 230)

Changing the script to the following fixes the locv settings too!:

on enterframe

  repeat with rn = 1 to 5 
    sprite(rn).loch = random(302)
    if sprite(rn).loch < 18 then
       sprite(rn).loch = 18
    end if
    sprite(rn).locv = random(230)
    if sprite(rn).locv < 10 then
       sprite(rn).locv = 10
    end if
  end repeat

end

- Once you have rewound your movie, and seen it work, save the movie under a different name!