Lingo Tutorial: Cursor Chase

READING:Manual pages of the commands described here

File "cursorchase.dir" shows a picture of batman following the cursor around the screen, but not as fast as in this file cursorchase0.dir! To get an image follow your cursor you can just get it to continually match its location:

      sprite(2).locH = the mouseH
      sprite(2).locV = the mouseV
      

But how do you make the chase look more natural? 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. First, use variable chaseSpeed to indicate the pixel intervals that the image will be trying to catch up with the cursor. We can initialize it to 25, and every time we go through the frame the image will get closer to the cursor by 25 pixels (in each horizontal and vertical direction):

 
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".

Exercise: The default is to have batman follow the cursor.
How would you instruct your program to use instead "Bart" or the "Axe" to follow the cursor?
How would you instruct it to increase or descrease the speed of chase?
How would you show the amount of time it passed since the beginning of the game?

Take a look at file "cursorchase2.dir" for an implementation.

 

 

Maintained By: Takis Metaxas
Last Modified: November 11, 2013