Article

Integrate Flash MX 2004 and Director MX 2004

Page: 1 2

Add the BuddyAPI Director Xtra to the Project

In order for the code to function correctly, we need to add the BuddyAPI 3.6 Xtra into the project; it will be included in the Director Projector when it's published.

With the project open, select Modify > Movie > Xtras, and click 'Add'. Locate the budapi.x32 file from the Xtras folder and click 'OK'.

We can now insert the code to control the Xtra we just added.

Creating the Document Handling Functions

Our next task is to add the control functions that allow us to accept incoming function requests from Flash and, believe it or not, it's not as difficult as you might think.

If we look at the internal cast of the Director movie, we see that, in position 1, we have the imported SWF file that we created earlier and, in position 2, we have the script that we added.

We're now going to add to positions 3 and 4, 2 further scripts that facilitate communication between Flash and Director.

  1. With the Cast window (Window > Cast) and Behavior Inspector open (Window > Behavior Inspector), select position number 3. From the Behavior Inspector, click the '+' button and select 'New Behavior'.
  2. 1342_image7

  3. Name the behavior prepareMovie and click 'OK'. Click the 'Script Window' icon and add the following code:
  4. global DMXDocHandler  
    on prepareMovie  
     DMXDocHandler = new ( script "DMXDocHandlerClass" )  
    end prepareMovie

  5. Close the Script Window.
  6. With the prepareMovie script selected in the cast, from the 'Script' tab of the Property Inspector, change the type from Behavior to Movie.


  7. What we are doing here is using the prepareMovie event handler to create a new instance of a script we're about to add, called DMXDocHandlerClass.

  8. With the Cast window open (Window > Cast) and Behavior Inspector open (Window > Behavior Inspector) select position number 4. From the Behavior Inspector click the '+' button and select 'New Behavior'.
  9. Name the behavior DMXDocHandlerClass and change the 'Type' within the Property Inspector to Parent. Click the 'Script Window' icon and add the following code:
  10. property pDefaultDocDir  
     
    on new (me)  
       pDefaultDocDir = "Files"  
     return me    
    end  
     
    on OpenFile me , p_FileType , p_Dir , p_FileName  
     set FileToOpen = the moviePath & pDefaultDocDir  & "\" &  p_Dir & "\" & p_FileName  
     -- Open the FileType  
     if (p_FileType = "PDF") then  
       me.StartAcrobat ( FileToOpen )  
     end if  
    end OpenFile  
     
    --Acrobat File opening  
    on StartAcrobat me, p_PDFFile  
      if ( baFileExists( p_PDFFile ) = 1 )  then -- check if file exists  
       set AcroExe = baFindApp( "pdf" ) -- see if acrobat is installed  
       if AcroExe <> "" then -- if they have acrobat and the file exists  
         baOpenFile(  p_PDFFile , "Maximised" ) -- open the file  
       else --  
         alert("You do not have Adobe Acrobat installed")  
       end if  
     end if  
    end StartAcrobat

  11. Close the Script Window.

Let's examine the code to see what's happening and how the function works.

First of all, we declare pDefaultDocDir to make it accessible later. This is the default location for the files, and can be referenced within the directory structure that we created earlier.

When the new instance of the script is created, we populate the value of pDefaultDocDir with the name of the directory in which the files are stored, in this case, 'Files'.

on new (me)  
   pDefaultDocDir = "Files"  
 return me    
end

We then have a new function, which accepts the following parameters:

p_FileType: The filetype (PDF, XLS, DOC, MXP etc)
p_Dir: The directory in which the file is located
p_FileName: The name of the file

These parameters are passed from the Flash movie to the Director projector when the button is pressed in the Flash movie. We will add this code within Flash in a moment.

on OpenFile me , p_FileType , p_Dir , p_FileName  
 set FileToOpen = the moviePath & pDefaultDocDir  & "\" &  p_Dir & "\" & p_FileName  
 -- Open the FileType  
 if (p_FileType = "PDF") then  
   me.StartAcrobat ( FileToOpen )  
 end if  
end OpenFile

The parameters are then concatenated into a new variable, called FileToOpen, which is the full path to the file that we're trying to open.

If the p_FileType is found to the 'PDF', we call the StartAcrobat() function, passing the name of the file.

Note: You can create as many different functions for detecting different filetypes as you wish, not just PDFs, as in this example.

We then get into the guts of the Director Xtra that we've just added.

As the full path to the file is passed to the StartAcrobat() function, we use the baFindApp(filetype) function of the BuddyAPI Xtra to detect whether Adobe Acrobat is installed. We then open the file using the baOpenFile(filename, windowSize) function.
If there is no file handler for the PDF file (i.e. Adobe Acrobat is not installed), we can trigger a simple alert to inform the user.

on StartAcrobat me, p_PDFFile  
     set AcroExe = baFindApp( "pdf" ) โ€“ Is Acrobat installed?  
   if AcroExe <> "" then โ€“ Acrobat is installed, enter condition  
     baOpenFile(  p_PDFFile , "Maximised" ) -- open the file  
   else  
     alert("You do not have Adobe Acrobat installed")  
   end if  
 end StartAcrobat

It's as simple as that! Of course, you could take the user to a frame within your Flash movie and allow them to install the Acrobat Reader by substituting:

alert()

for:

sprite(1).callFrame(frameNumber)

This is a simple modification, as it allows us to call a function on a specific frame within Flash by referencing the relevant sprite within the Internal Cast (in this case, the imported Flash movie is at position 1).

The only thing we have to do now, is to add the code within Flash to call the DMXDocHandlerClass lingo script.

Returning to Flash to Add Communication Code to Director

Before we add the code to reference the PDF file we wish to open, add a PDF into the ISO / Files / PDFs folder so that we can check that it works.

This article's source files already include one, but you can easily add your own PDF files; if you do, don't forget to change the filename and/or file type in the function below.

  1. Double click the imported SWF file on the stage to automatically edit the FLA within Flash.

  2. Select the first frame of the Actions layer and add the following code:
  3. //==========================Load Documents Via Director  
    launchButton.onRelease = function() {  
     getURL('lingo:DMXDocHandler.OpenFile("PDF","PDFs","SmartTabs QuickStart Guide.pdf")');  
    };

    Note: In the source FLA for this article, I have externalised the ActionScript into an .as file for greater scalability and usability.

    We use the getURL() function to talk to the Director Projector, while the OpenFile() lingo script passes in the parameters that are needed (p_FileType, p_Dir and p_FileName).

    In this manner, we have a high degree of control over the files we open, and their locations. This is essential for larger projects with many different filetypes, where the assets that the user wants to open are in different locations.

    We're all done in Flash! Now it's time to move back into Director to publish and test the application.

  4. Click the 'Done' button at the top left of the timeline to return to Director.

Testing the Application

It's time to test the application. Select File > Publish.

You should now be presented with a Director Projector. Click the button and see what happens โ€“ if all has gone well, you should successfully open the PDF that is included in the article source files (or the custom file that you added if you have the reader installed).

To burn your project onto CD, simply copy the contents of the ISO/ folder (removing the Director Movie *.dir). Add an autorun file if you want the CD to run automatically when placed in the drive.

The application works well, and will scale easily by creating new handling functions to access any file type that you wish to open. It allows you to check for the existence of an appropriate program to open the file and perform graceful actions, rather than giving non-informative error messages.

You can use this application as the basis for creating complex installation routines, fancy front ends for CDs or even a snazzy Curriculum Vitae!

Also, we've only brushed the surface of the BuddyAPI Director Xtra, due to the limitations of the trial version, but, if you wish to use more of the functionality, go ahead and purchase it, and explore the extra power of Director and Flash in unison.

Now that you have the basics of Flash and Director integration under your belt, experiment and see what you can accomplish. I'm sure the SitePoint community would love to see your creations!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: