Articles Archive
Articles Search
Director Wiki
 

Matching Text to Screen Positions

February 1, 2001
by Gary Rosenzweig

As Lingo has evolved over the years, there have been many ways to determine where characters are position in fields and text members. For backwards compatibility, all of these are still around. However, the most recent functions for determining character positions are all that you need.

First, there was the mouseChar, the mouseWord and the mouseLine. These would return the number of the character, word, or line under the cursor. However, you couldn't use it to determine the character at an arbitrary location.

Then, we got functions like loctoCharPos which would tell us which character was under any point relative to the upper left corner of the member. Unfortunately, we had to take things into account like sprite position and scrolling.

The modern functions pointToChar, pointToWord, and pointToLine make the previous functions obsolete. Now, all you need to do is to feed one of these functions with the sprite number and screen location. They even account for scrolling.

The example movie below will show you which character, word and line the cursor is over. Here is the code that is used.

on exitFrame me
  output = ""

  n = pointToLine (sprite me.spriteNum, the mouseLoc)
  put "Line:" && n &RETURN after output

  n = pointToWord (sprite me.spriteNum, the mouseLoc)
  put "Word:" && n & RETURN after output

  n = pointToChar (sprite me.spriteNum, the mouseLoc)
  put "Char:" && n & RETURN after output
    
  member ("output").text = output
end

 

 

So what can you use this for? By combining these functions with other Lingo, you can create all sorts of things. For instance, you could show a different graphic image depending on which line of text the cursor is over. You could create an area that shows the line of text enlarged for those who have trouble reading small text. You could even recreate Director's hypertext functionality with your own code.

One thing I like to use pointToLine for is to create a functional text list. The user can scroll through a list of options and click on the one that they want. Using pointToLine is a lot easier than making every single line a piece of hypertext.

A sample Director 8 movie is available for download in Mac or Windows format.

Gary Rosenzweig's two most recent books are: "Special Edition Using Director 8" -- The most comprehensive guide to Director ever, including tons of examples and demo movies. It's suitable for novices and experts alike. "Advanced Lingo for Games" -- How to make games with Director 7 and 8. This book comes complete with full source code for more than 20 complete games. More information about these books can be found at http://clevermedia.com/resources/bookstore/. They can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.