Sample Code
A selection of many different examples of basic Lingo code.

Move a sprite across the screen continuously
on ExitFrame
  a = the currentSpriteNum
  sprite
(a).locH = sprite(a).locH + 4
end


SPIN SPRITE with a roll over
on mouseWithin
  -- You must use a bitmap
  a = the currentspriteNum
  sprite(a).rotation = sprite(a).rotation + 10
end


RGB COLOR CHANGE WITH A BUTTON
property red
on beginSprite
  red = 0
end

on
exitFrame
   red = red + 5
  sprite
(1).color = rgb(red,0,0)
  if red > 255 then
     red = 0
  
end if
end



FOLLOW BEHAVIOR
--make sprite 2 moveable in the property inspector
on ExitFrame
  a = the currentSpriteNum
  h1 = sprite(a).locH
  v1 = sprite(a).locV
  h2 = sprite(2).locH
  v2 =
sprite(2).locV
  speed = 3
  destH = h1 + (h2 - h1)/speed
  destV = v1 + (v2 - v1)/speed
  sprite(a).loc = point(destH,destV)
end


DATABASE LIST  and FONTS
on exitFrame me
   someList = ["a", "b", "c", "d"]
     repeat with i = 1 to 4
       
put someList[i]
     end repeat
end


OR


on mouseEnter
  
someList = ["John", "Joe", "Bill", "Jane"]
   Myname = someList[random(4)]
   put Myname into member 2 
 
--or
   Myname2 = someList[random(4)]
   put Myname2 into member 4

  
fontList = ["helvetica", "Verdana", "Chicago"]
   f = fontList[random(3)]
   member(4).font = f
   member(4).fontSize = 40
   member
(4).fontStyle = [#bold--try[#bold,#italic]
   member(4).foreColor = 6
end



Dest Loc Josh Nimoy   sprite beh., adds a deceleration to a sprite when moved
property destLoc
property speed

on beginSprite
  
a = the currentspritenum
  sprite
(a).destLoc = sprite(a).loc  --establish position
  speed = 5.0
end

on
exitframe
  a = the currentspritenum
  dh = sprite(a).destloc.locH  --original position
  dV = sprite(a).destloc.locV
  h = sprite(a).locH    --new position
  v = sprite(a).locv
  h = h + (dh-h) / speed  --divide by spped to get back
  v = v + (dv-v) / speed
  sprite(a).loc = point(h,v)
end



STRETCH  Josh Nimoy
on prepareFrame
  a = the currentSpriteNum
  b = get_sprite_Distance_From_Point(a,the mouseH,the mouseV)
  growfactor =
100
  sprite
(a).height = max(growfactor-b,20)
  sprite(a).width = max(growfactor-b,20)
  sprite(a).color = color(#rgb, 0, 0, b)
end

on
get_sprite_Distance_From_Point(a,x,y)
  return sqrt(power(sprite(a).locH-x,2)+power(sprite(a).locv-y,2))
end



MOUSE CLICK COUNTER
property clickcount

on beginSprite
  clickcount = 0
end

on
mouseDown
  clickcount = clickcount + 1
  if clickcount >= 3 then
    put
"ya 3"
    clickcount = 0
  end if
end


SIMPLE TIMER
property timeit

on beginSprite
  timeit = 0
end

on
exitFrame
  timeit = timeit + 1
  if timeit >= 60 then
    put
"ya"
    timeit = 0
  end if
end



WAIT TIMER
property myTicks, waiting

on beginSprite
  waiting = false
end

on
mouseDown
  put "I am waiting 2 seconds for ya"
  myTicks = the ticks
  waiting = true
end

on
ExitFrame
  if waiting = true then
    if (the ticks - myTicks) >= 60 then
      put "ya"
      waiting = false
    end if
  end if
end



Cycle a bunch of cast members:
on exitFrame
sprite(1).CastNum = sprite(1).CastNum + 1  --switch through cast mamber 1-3
  if sprite(1).CastNum > 3 then
    sprite(1).CastNum = 1
 
end if
end


OR


on exit Frame
If the frame = 25 then   
--(If you have 25 frames in the score)
   go to frame 1
end if
end


REPEAT LOOP
On MouseUp
repeat with i = 1 to 3     -- this will beep 3 times, beep 3 is a better approach
  beep
end repeat
end


OR

on exit Frame
repeat with i  = 1 to 1000
  
put i into member --change text with numbers between 1-1000
   updatestage
end repeat
end

OR

on exit Frame
repeat with i = 1000 down to 1
   
put i into member 1
    
updatestage
end repeat
end

OR


property i
on exitFrame
i = i+1
  if i > 8 then
    i = 1
  end if
    sprite(the currentSpriteNum).forecolor = --cycle through 8 colors
end


OR  add a # label to a name
on exitFrame
  repeat with n = 1 to 8
    if n >= 1 then
      x =
"countme"&n
      put
    end if
  end repeat
end


updatestage example
--turn on and off updatestage
on exitFrame me
repeat with
i = 1 to 320
    sprite(1).locH = i
    updatestage
  end repeat
 
  go the frame
end


TYPING

ENTER   gives you a line return

sendallsprites(#mouseDown)  --sends a mousedown to all sprites

<> = not



Random TRUE or FALSE
blabla = random(2)-1



MOVIE LOOPS

Switch cast Members  with keyboard keys
on exitFrame me
   if the keyPressed = "r" then sprite(1).castNum = 2
   if the keyPressed = "t" then sprite(1).castNum = 3
  
if the keyPressed = "y" then sprite(1).castNum = 1
  
if the keyPressed = 1 then go to frame 1
  
if the keyPressed = 2 then go to frame 5
go
to the frame
end


Mini-loops Behavior    good for looping specific frames in  a quicktime movie
global myloopstart,myloopend,stopper
on exitFrame me
  if the
keyPressed = "a" then
   sprite(1).movieRate = 1
    set myloopstart = 46
    set myloopend = 285
   sprite(1).movieTime = myloopstart
    set stopper = 0
  end if

  if the
keyPressed = "b" then
    set myloopstart = 428
    set myloopend = 524
   sprite(1).movieTime = myloopstart
    set stopper = 1
  end if

  if the
movieTime of sprite 1 >= myloopend then
    if
stopper = 1 then
      
sprite(1).movieRate = 0
    end if
    if
stopper = 0 then
      
sprite(1).movieTime = myloopstart
    end if
  end if
  if the
keyPressed = 2 then go to frame 5
    if the keyPressed = 3 then go to frame 10
    go
to the frame
end


Switch quicktime movies, but keep the frame count
property clickcount
property settime

on beginsprite
  clickcount = 0
  preLoadMovie "oz2"
    preLoadMovie "teasmall"
end

on
mouseDown me
  settime = sprite(1).movieTime
 
  clickcount = clickcount +
1
  if clickcount > 2 then
    clickcount = 1
  end if
 
  if clickcount = 1 then
    sprite(1).member = 4
    updatestage
    sprite
(1).movieTime = settime
  end if

  if
clickcount = 2 then
    sprite(1).member = 1
    updatestage
    sprite
(1).movieTime = settime
 
  end if
end



CLICK ON, to determine last sprite clicked on
on mouseDown me
     sprite(the clickOn).foreColor = random(255)-1
    
put the ClickOn
end



VECTOR Change attributes randomly   startMovie script
on changeFills    -- set each attribute to a new random value

repeat while the mouseDown
    r = random
(200)
    rr = random(40)
    member("star").fillMode      = [#gradient, #solid][random(2)] 
    member("star").fillColor     = rgb (56 + r, 56 + r, 56 + r)
    member("star").endcolor      = rgb (56 + r, 56 + r, 56 + r)
    member("star").fillCycles    = random(7)
    member("star").fillDirection = random(360)
    member("star").fillOffset    = point(20 - rr, 20 - rr)
    member("star").fillScale     = random(80)
    member("star").strokeColor   = rgb (56 + r), 56 + r, 56 + r)
    member("star").strokeWidth   = random(15)
    updateStage
  end repeat


ARRAYS
listm=[]
listm[1] = member(2).model("lamp1|nurbsToPoly1")
listm[2] = member(2).model("lamp1|nurbsToPoly2")
listm[3] = member(2).model("lamp1|nurbsToPoly3")
listm[4] = member(2).model("lamp2|nurbsToPoly1")
listm[5] = member(2).model("lamp2|nurbsToPoly2")
listm[6] = member(2).model("lamp2|nurbsToPoly3")
listm[7] = member(2).model("lamp3|nurbsToPoly1")
listm[8] = member(2).model("lamp3|nurbsToPoly2")
listm[9] = member(2).model("lamp3|nurbsToPoly3")
------------------
Basically, if you make an array filled with strings, it does not mean that a
variable in your code will refer to that array item, just because the
variable name is the same as the contents in that string. - So I changed
your code to just plain old arrays. What we are losing here, is the ability
to refer to array items with "real" names. If you want to do that, here's
one approach (more primitive than the one which follows it):
------------------
--make a whole bunch of pointers

sm1=1
bm1=2
em1=3
sm2=4
bm2=5
em2=6
sm3=7
bm3=8
em3=9

listm=[]

--and now you can *kinda* refer to them by name.
listm[sm1] = member(2).model("lamp1|nurbsToPoly1") --blue
listm[bm1] = member(2).model("lamp1|nurbsToPoly2")
listm[em1] = member(2).model("lamp1|nurbsToPoly3")
listm[sm2] = member(2).model("lamp2|nurbsToPoly1") --orange
listm[bm2] = member(2).model("lamp2|nurbsToPoly2")
listm[em2] = member(2).model("lamp2|nurbsToPoly3")
listm[sm3] = member(2).model("lamp3|nurbsToPoly1") --green
listm[bm3] = member(2).model("lamp3|nurbsToPoly2")
listm[em3] = member(2).model("lamp3|nurbsToPoly3")

-----------------------------------------------------
So that approach uses variables in place of numbers, but you are still
having to use array syntax. There is also a way to use dot syntax with our
names. To do this, we set up an (ever dreaded) property list:
---------------------------------------------------
-- ok, so, each item is a pair, seperated by colons.
-- on the left side, you define a new #symbol
-- on the right side, you put any old value.

listm=[#sm1:0, #bm1:0, #em1:0, \
       #sm2:0, #bm2:0, #em2:0, \
       #sm3:0, #bm3:0, #em3:0 ]

--and now you can refer to them with dot syntax,
--as well as loop through them as if they were list items.

listm.sm1 = member(2).model("lamp1|nurbsToPoly1") --blue
listm.bm1 = member(2).model("lamp1|nurbsToPoly2")
listm.em1 = member(2).model("lamp1|nurbsToPoly3")
listm.sm2 = member(2).model("lamp2|nurbsToPoly1") --orange
listm.bm2 = member(2).model("lamp2|nurbsToPoly2")
listm.em2 = member(2).model("lamp2|nurbsToPoly3")
listm.sm3 = member(2).model("lamp3|nurbsToPoly1") --green
listm.bm3 = member(2).model("lamp3|nurbsToPoly2")
listm.em3 = member(2).model("lamp3|nurbsToPoly3")

---------------------------------------------------
ok, property lists use confusing syntax (only in their setup) - but you can
see their convenience; they make it possible for items to be accessible
through both name and number. Very cool indeed.

points and rects are really just property lists.

Point = [ #locH:0 , #locV:0 ]

Rect = [ #left:0 , #top:0 , #right:0 , #bottom:0 ]

Thus, it follows (a weird easter egg, perhaps?) that we can refer to
"p.locH" as "p[1]" - less typing! (ok, ONE less character.)


MIDI IO extra    available at:  www.audiomulch.com/midiio
-- blob movie 30 frames long
    Sprite(1).movieRate = 0
    if currentChan = 1 then
           currentVal to getProp(msg, #value)
            Sprite(1).movieTime = currentVal / 2.1333
    end if


SEND MIDI
  --SendMessage(gMIDIioXtra, [#controlChange,chan,num,val])
    SendMessage(gMIDIioXtra, [#controlChange,1,0,currentValB]) 
 

  if currentChan = 2 then
    set
currentValC to getProp(msg, #value)
    SendMessage(gMIDIioXtra, [
#controlChange,2,0,currentValC]) 
  end if