Lingo Tutorial: Cursor Chase

READING:Manual pages of the commands described here

File "cursorchase.dir"

A picture of batman is following the cursor around the screen! Here is how:

You call the function that handles the chase continuously inside a frame while you are chasing the cursor:

 
on ExitFrame
  HandleChase
  go to the frame
end
      

The movie script that manages the chase is as follows:

 
on HandleChase
  global chaseSpeed
  chasespeed = 25 -- how fast it follows the cursor
  sprite(2).member = cast("Batman").number -- we want to be able to select among many

  if abs(the mouseH - sprite(2).locH) < chaseSpeed then 
    hDir = 0
    sprite(2).locH = the mouseH
  else
    if (the mouseH - sprite(2).locH) > 0 then hDir = 1
    else hDir = -1
    sprite(2).locH = sprite(2).locH + (chaseSpeed * hDir)
  end if
  if abs(the mouseV - sprite(2).locV) < chaseSpeed then
    vDir = 0
    sprite(2).locV = the mouseV
  else
    if (the mouseV - sprite(2).locV) > 0 then vDir = 1 
    else vDir = -1
    sprite(2).locV = sprite(2).locV + (chaseSpeed * vDir)
  end if
end HandleChase

Now we can abstract the two parameters, speed and the icon that follows the cursor, so that we can change them later on using Lingo! Here is how you could do it: In a movie script you initialize the icon of the batman (its sprite will appear in channel 2) and the speed chase.

on startMovie
  sprite(2).member = cast("Batman").member
  global chaseSpeed
  chaseSpeed = 25
end startMovie

It would work, but it is better to do it through two functions, so that we can change them by giving new arguments. One function that sets the cast to whatever we may want, and another that sets the speed to whatever pixels per second we may want:

 on startMovie
  go to "Main Loop"
  SetCast "Batman" -- default
  SetSpeed "Medium" -- default
end startMovie
--These two set commands change the cast and speed.
on SetCast theCast
  sprite(2).member = cast(theCast).number
end SetCast
 
on SetSpeed theSpeed
  global chaseSpeed
  if theSpeed = "Fast" then chaseSpeed =  40
  if theSpeed = "Medium" then chaseSpeed = 25
  if theSpeed = "Slow" then chaseSpeed = 10
end SetSpeed

 

The resulting script is at File "cursorchase1.dir".

The default is to have batman follow the cursor. How would you instruct your program to use instead "Bart" or the "ax" to follow the cursor? How would you instruct it to increase or descrease the speed of chase?

 

 

Maintained By: Takis Metaxas
Last Modified: April 7, 2011