Articles Archive
Articles Search
Director Wiki
 

Manipulating an embedded Flash movie

July 3, 2000
by Tim Friedmann

The world as we know it

When the Flash Asset Xtra for Director was released with Director 6.5 many articles were published about communication between a Flash movie and the Shockwave movie that contained it. In these articles, communication is defined as a Flash movie "talking" to the Shockwave movie. This makes it possible to create a Flash navigational element that controls a Director movie.

But what about communication in the other direction? Is it possible to let the Director movie talk to the Flash member? Can you manipulate the playback and composition of a Flash member via Lingo? The answer until Director 7 was, "yes, a little." With the introduction of version 8, the answer is, "yes, a lot!"

Best of both worlds

Flash is great for building long animation sequences that have small file sizes. If you integrate such animations in Director you can zoom them:

sprite(FlashSprite).viewScale = 200

and scroll them:

sprite(FlashSprite).viewPoint = sprite(FlashSprite).viewPoint + point (10,10)

If you want to play a specific part of an animation in Director, you usually would define labels and use the go to frame

command. The same approach can be used to set the playback head inside a Flash animation. The goToFrame command, when directed to the Flash sprite, serves this purpose:

sprite(FlashSprite).goToFrame("FlashMovieFrame")

Macromedia introduced some new Flash sprite commands in Director 8 that make the exchange of animation parts possible. The new keywords are getVariable and setVariable. Check out this demo movie:

Director 8 download for Mac or Windows.

The Flash movie is divided into four sections. You can choose the scene that will play in each section (from 1 to 5) by entering the desired scene number and clicking the "execute" button. You can zoom and scroll while a scene is playing.

To make the exchange of animations in the four sections possible, four variables (var1, var2, var3, var4) are defined in the Flash movie. Each variable represents one of the four sections and is initially set to label0. To show a particular animation, the value of one of those variables is changed. For example:

sprite(flashSprite).setVariable("var3","label2")

will play scene 2 in section 3.

Of course, the scene is not played by itself. As in Director, changing a user defined variable will not do anything unless a script reacts to the change. In Flash 4 there is a language called Action Script, which is the Flash equivalent of Lingo (although not as extensive).

This is the basic idea behind this new way of manipulating embedded Flash movies:

The dark side of the moon

This is where the process gets a bit tricky. Most Flash developers are not very comfortable with Action scripting, and the value changes are really best tested in Director. Tight interaction between Director and Flash movie design is needed. In order to do this you need to be aware of some of the differences between Flash and Director. Though Flash has many concepts in common with Director, such as time-based animations, tweening and key frames, there are some vital differences.

Channels are called layers in Flash; sprites are instances; the layer order is, from the Director developer's point of view, upside down; the commands used to build up animations are different; an unlimited number of objects can be inside one layer; and so on. There are good references on the web that cover the material, such as www.flashzone.com and www.flashplanet.com, so I won't go into detail here.

The most basic difference, and the important distinction in terms of this article, is in the architecture of Flash. Director files are rather flat. There is just one timeline (score), and all sprites are more or less basic elements. The only exception to that concept is filmLoops. Flash has a similar way of encapsulating animations and elements called movie clips. Movie clips have their own timeline, and their playback can, unlike filmLoops in Director, be controlled and manipulated. Movie clips can also contain other movie clips. This allows you to give your project a tree structure similar to the directory structure on your computer. The architecture of the Flash movie in the sample movie above has the following structure:

Flash movie [root]
  square1 [instance of movie clip "letsgo"]
  square2 [instance of movie clip "letsgo"]
  square3 [instance of movie clip "letsgo"]
  square4 [instance of movie clip "letsgo"]
    

The four nested elements (square1 to square4) are instances of a movie clip called letsgo.

The movie clip letsgo contains the five animated scenes plus an empty scene, which is shown initially and when no scene is playing. Each scene has a label (which corresponds to a marker in Director). The empty scene is labelled label0 and the five animations are label1 through label5.

Instances are similar to sprites in Director. Flash, like Director, references a movie clip when an instance of it is in a scene. Each instance is independent from another instance of the same movie clip, so it is possible to show four different scenes in the four sections on screen.

Unlike Director, there is no message hierarchy in Flash. If you want an element to react on mouse events, you have to make it a button. If you want Flash to react to a frame event (in Director terms, enterFrame), you attach a script to the desired frame. There is no predefined layer for Frame scripts; you can attach the script to the layer of your choice. But if you don't do it carefully, you will have problems finding your Frame scripts again after some time has passed. That's why many Flash programmers define a separate action layer with empty frames and keyframes to put their frame scripts into.

My own planet

Let's now look at the sample movie we saw earlier, to find out what happens when you change the value of one of the four variables (var1 to var4). First of all, the four variables must be initialized. This happens in the first frame of the action layer in Flash:

Label: "init"
  Set Variable: "var1" = "label0"
  Set Variable: "var2" = "label0"
  Set Variable: "var3" = "label0"
  Set Variable: "var4" = "label0"

After starting the process, we want the Flash movie to react every time the values of these variables are changed. Because there are no global Movie scripts in Flash, we have to build a loop in Flash so that a Frame script can be constantly triggered. Unlike Director, Flash requires that you always use two frames to build a loop. Looping on one frame leads to stack overflow errors and causes distressing error messages. In our sample movie the frame reacting to value changes has the label checkPlay and the following frame has a frame script that sends the playback head back to checkplay.

Frame: "checkPlay" + 1
  Go to and Play ("checkPlay")
  

Note: Make sure to use Go to and Play and not Go to previous frame, which stops playback after the first back jump.

The checkPlay frame is the frame that reacts to value changes in the interface variables. Whenever a value other than label0 occurs it needs to send the right movie clip instance to the label defined in the variable:

Label: "checkPlay" 
  If (var1 ne "label0")
     Begin Tell Target ("/square1")
        Go to and Play (/:var1)
        Set Variable: "/:var1" = "label0"
     End Tell Target
  End If
  
  If (var2 ne "label0")
     Begin Tell Target ("/square2")
        Go to and Play (/:var2)
        Set Variable: "/:var2" = "label0"
     End Tell Target
  End If
  
  If (var3 ne "label0")
     Begin Tell Target ("/square3")
        Go to and Play (/:var3)
        Set Variable: "/:var3" = "label0"
     End Tell Target
  End If
  
  If (var4 ne "label0")
     Begin Tell Target ("/square4")
        Go to and Play (/:var4)
        Set Variable: "/:var4" = "label0"
     End Tell Target
  End If
  

With Tell Target you can set the focus on any given movie clip instance. The "Go to and Play" command sends the playback head of the instance to the label in the variable. Each movie clip has its own set of variables. To get the root variables inside the Tell Target block you have to put /: in front of the variable name.

The variable must be set to label0 to avoid playing the new scene over and over again from the first frame each time we enter the loop and check the value of that variable. After each scene a frame script in the letsgo movie clip sends the playback head to the empty scene at "label0". The scene label0 consists of two frames to loop on until the corresponding interface variable is changed again.

Usually Flash programmers would use a Stop Action in scene label0 to pause the playback head there. Unfortunately, I could not manage to start the playback head of the letsgo movie clip instance again from the checkPlay frame in the main timeline. This seems to be one of those nasty side-effects.

Back to earth

With the scripts above and some hard days getting used to the different philosophy of Flash you can create new web formats that can take advantage of both animation environments:

Director even can help you to debug your Flash movies! With the getVariable property you can check the values of the variables, whenever you want.

Tim Friedmann has been working in the multimedia industry since 1996. He has worked as a Director developer for German companies like Concept, Pixelpark and now Infosion GmbH. He specializes in online game development.

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