Director Lists to Flash Arrays

The question comes up over and over on various fora. People are well aware that you can — in Director MX 2004 — directly access variables in a Flash sprite instance at run-time. Strings and numbers are easy. I’ve documented methods for using the Flash XML Object within Director (both in sprites and as free-standing objects) at Director Online and here on the blog. But what about lists?

That question came up again the other day, so I spent a couple minutes whipping together a very simple demonstration of how to pass a Lingo linear list into an ActionScript array. This is about as bare-bones as it gets.

First, I created a SWF with a dynamic text field in it that would display the contents of the array I created in the movie. I named the field “test” and put the name of the field into the text area.

Next, I imported the SWF into Director and placed it in sprite channel 1. In a movie script, I added the following:

on makeArrayFromList
  lst = [1, 2, 3, 4, 5, 6]
  arr = sprite (1).newObject (“Array”)
  repeat with i = 1 to lst.count
    arr.push (lst[i])
  end repeat
  sprite (1).test = arr.toString ()
end

That’s all there is. A call to makeArrayFromList in the Message window (while the movie’s running) creates the linear list, creates an Array object in the Flash sprite, then pushes the items from the list in order onto the Array. The toString method shows the elements in the test field.

Easy steps from here are adding parameters to the handler that allow you to pass in any linear list and define a target within the Flash movie (if you don’t want to create a _level0 variable. More complex would be handling multi-dimensional lists as well as property lists (which need to be represented by Flash Objects instead of Arrays) and multi-dimensional combinations of linear and property lists.