Lingo Tutorial: Cursor Chase

READING:Manual pages of the commands described here

File "cursorchase.dir"

Make an image follow the cursor around!

A picture of batman is following the cursor around the screen. Here is how:
In a movie script you initialize the icon of the batman (its sprite resides in channel 2) and the speed chase. We could do it simply as follows:.

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

But it is better to do it through two handlers. One that sets the cast to whatever we want, and another that sets the speed to whatever pixels per second we want (so that we will generalize it later):

 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).castNum = 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

Now, you spin your wheels 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
  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

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?

 

 

Maintained By: Takis Metaxas
Last Modified: August 6, 2007