9 Replies Latest reply on Aug 14, 2008 2:21 AM by Newsgroup_User

    [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)

    Level 7

      I'm not new to Director, I'm not new to Flash but I don't feel confident
      when integrating Flash within Director.

      I started using Flash button components but as I may need to have many
      on screen, it looks like it's a better idea to have them all in one
      Flash movie, perfomance wise.

      I discovered I could access Flash movieclips properties directly from
      Director which is great (changing text from MCs, visibility...)

      Now, I'd like to catch Flash events from Director, not from the awful
      "getUrl" ActionScript command, which is a real pain while editing the
      Flash movie as it opens browser windows !

      That is, instead of Flash button triggering a Lingo script, I'd like
      Director to get properties from the button the mouse has clicked over.

      Any idea how to achieve this ?


        • 1. [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)
          Well I also haven't had any success with the documented
          spriteObjRef.setCallback(actionScriptObject, ASEventName, #LingoHandlerName, lingoScriptObject);

          I've also resigned myself to the clunky getURL("event:....etc") method. (Remembering to add quotes around string variables!)

          Here's a tip for quick testing and to stop the browser opening (and since MX2004 doesn't seem able to open a SWF's FLA in CS3):

          1) In Director, import your movie as a linked swf.
          2) In Flash, set publishing settings to overwrite this swf.
          3) To publish your FLA, press Shift-F12. This doesn't run the swf, so it won't open a browser.
          4) In Director, to auto re-import your newly published swf, put this in your startup movie script:

          on startmovie
          if the runmode= "Author" then
          member("mySWF").fileName= member("mySWF").filename --include one of these for every SWF you're working on!
          end if

          5) Run Director (ctrl+alt+r -> ctrl+alt+p)

          You can now keep the FLA open in Flash (on one monitor), press shift-F12, then switch to your other monitor(s) with Director and run to instantly see the effects of any Actionscript changes.

          • 2. [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)
            pete.h Level 1
            Thanks PJ - knowing that if I solved this problem, there'd be at least two of us to benefit...I went ahead and did some experimentation, to see if I could eliminate the use of getURL for event/parameter passing and get spriteRef.setCallback() working.

            Well, seems like I found the missing link! Through no help of documentation, of course. Just the usual trial and error!

            Though not mentioned in any documentation (grrrr), it appears that Director's spriteRef.setCallback(actionScriptObject, ASEventName, #LingoHandlerName, lingoScriptObject) command will ONLY work for a Flash Asset if the actionScriptObject ALSO has an event function defined for the event you want to pass through to Director.

            For example, using a Tree component in Flash (I don't use the built-in components, as you cannot customize the look of them very well), and with an instance name of "tree" for a tree component on the Flash stage, and the "nodeOpen" event wanting to be received in Director...

            In your Flash/AS2 asset:
            var treeListener:Object = new Object(); //an intermediate object used to transfer events to Director!
            treeListener.nodeOpen = function(evt_obj:Object) {} //this is the required dummy declaration, to be able to pass the event thru to Director. Even though it does nothing, without it, the event is never passed!
            tree.addEventListener("nodeOpen",treeListener); //informs the tree instance to dispatch events to the treeListener object!

            if (System.capabilities.playerType == "DirectorXtra") { getURL("event:flashLoaded") }; //put this at the very end of all your initialization ActionScript, to inform Director that the Flash sprite is ready to manipulate! (EDITED: Thanks to TheBigC1974 for the tip about the .playerType property - see future post below)
            Then, in a Director sprite script:
            on flashLoaded me --called when Flash sprite is ready!
            sp.setCallback(sp.treeListener, "nodeOpen", #nodeOpen,me) --direct the event to this script's #nodeOpen handler (below)

            on nodeOpen(me, foListener,foEventObj)
            put "foListener properties:"&&foListener.dbgPropList() --puts a list of functions defined for the listener object
            put "foEventObj properties:"&&foEventObj.dbgPropList() --for a tree event, puts [#node, #type, #target]
            put "nodeOpen label="&foEventObj.node.attributes.label
            end nodeOpen

            Note that in the Lingo event handler, the flash event object is the 3rd parameter, not the 2nd, as mistakenly shown in the Director documentation (unless it's in a movie script, not a sprite script).
            • 3. Re: [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)
              Ex Malterra Level 1
              Sorry I didn't see this post earlier, could have shared some knowledge. When the new Flash Asset Xtra came out for Director MX 2004 a littel over two(?) years ago it really changed the way we had to handle programming the Flash components that shiped with Director. I and many other developers suddenly found our stuff not working, and it was a drag! We figured it out and had it well documented in Tom Higgins' blog, but when he left Adobe his blog got shut down. http://weblogs.macromedia.com/thiggins/ That's a shame, there was good stuff there. Could have saved folks headaches down the line.
              • 4. Re: [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)
                Ex Malterra Level 1

                Originally posted by: Newsgroup User
                Now, I'd like to catch Flash events from Director, not from the awful
                "getUrl" ActionScript command

                By Jove, I got it. Reading this post and its replies inspired me to try again.

                I've been using Flash, either the components that ship with Director or SWFs I made, for GUIs in my Director pieces for some time now because for me it works well. But like a lot of folks getURL strikes me as very inelegant, and I’ve wished for a better way. Flash’s ExternalInterface class seemed like the golden key, but unfortunately it doesn’t work with Director. Go figure.

                Take this Star Trek Thing thing I’ve been playing with. All Flash GUI with a W3D world. It has a re-skinned Flash tree and some custom stuff. What a pain using getURL that has been! Yeah the custom tree icons and whatnot need to be done in Flash, but the calls to Director functions from the SWF are a convoluted pain in the butt. I’d like to handle all button clicks in Director, and just do the visual stuff in Flash. By the way, this thing does very little as of yet, just experimenting… scales changeable, “s”, “d” and arrow keys to move, number keys for speed.

                I’m babbling. Anyhow, this morning I had to try again so I made a simple one frame SWF having four Button components on the stage, with a “for” loop to label them. ActionScript:

                ////////// label buttons
                for (var n:Number = 1; n < 5; n++)
                _root["button" + n].label = "Button " + n;


                So I pop that puppy on the stage in Director. I start it on the second frame of the movie because I’ve read in many posts from many developers that it’s a good idea not to start on the first frame, and I think they’re right. I’ve got a behavior script on frame two that has the following:

                function exitFrame(me)
                var theSwf = sprite("theSwf");

                ///// create event listener
                var evtObj = theSwf.newObject("Object");
                var fnct = theSwf.newObject("Function");
                evtObj.click = fnct;

                ///// add callbacks
                for (var n = 1; n < 5; n++)
                var currBtn = theSwf._root["button" + n];

                currBtn.addEventListener("click", evtObj);

                theSwf.setCallback(theSwf, "click", symbol("doButton"), "primeScripts");

                This is the movie script that has the function called by clicking the buttons:

                function doButton(me, obj)
                var msg;

                switch (obj.target.label)
                case "Button 1":
                msg = "Button 1 in the SWF was clicked";

                case "Button 2":
                msg = "Looks like the second button was clicked";

                case "Button 3":
                msg = "Getting near the end, good ol' Button 3";

                case "Button 4":
                msg = "That's all folks; last button clicked";


                Give it a shot, or download zip w/ Flash 8 and Director 11 files here:

                Zip File

                It’s evtObj.click = fnct; that is the key I think. I’ve tried using newObject(“Function”) in a lot of ways, usually trying to define the function in Director. Nah, just let the event object know it’s got to do something with a click and use setCallback to let it know what that something is. It was:


                Originally posted by: pete.htreeListener.nodeOpen = function(evt_obj:Object) {} //this is the required dummy declaration, to be able to pass the event thru to Director. Even though it does nothing, without it, the event is never passed!

                That got me thinking along those lines. Although I’ve never done it in the way pete.h does. If I’m going to use a tree component in Director and I want custom icons and labels I’ll use iconFunction and labelFunction in Flash, but the rest I do in Director. Using either the tree component shipped with Director or a SWF I’ve made I always used something like this, where a tree component or tree SWF is sprite one:

                function exitFrame(me)
                var tree = sprite(1);

                ///// xml/dataprovider
                txml = tree.newObject("XML");
                txml.ignoreWhite = true;
                txml.parseXml("<root label=\"Root Node\"><node label=\"Node One\" /><node label=\"Node Two\"><child label=\"Child One\" /></node></root>");
                tree.dataProvider = txml;

                ///// event listener object
                var evtObj = tree.newObject("Object");

                tree.addEventListener("nodeOpen", evtObj);
                tree.setCallback(tree, "nodeOpen", symbol("doTree"), "primeScripts");

                tree.addEventListener("nodeClose", evtObj);
                tree.setCallback(tree, "nodeClose", symbol("doTree"), "primeScripts");

                tree.addEventListener("change", evtObj);
                tree.setCallback(tree, "change", symbol("doTree"), "primeScripts");

                The movie script that makes does stuff would be something like:

                function doTree(me, obj)
                var msg;

                switch (obj.type)
                case "nodeOpen":
                msg = "Open wide";

                case "nodeClose":
                msg = "Close that damn door";

                case "change":
                msg = "cha-cha-cha-cha changes";

                member("infoText").text = msg;

                Anyway, I’m not usually this long winded but I’m happy to have finally got this figured out. It’s great the way members of the Director community can help each other get to the bottom of things. Oh, I apologize for the JavaScript syntax if you’re not into it. It may take 5 times longer to find a prime number than Lingo, but I prefer it 500 times to Lingo because it’s the way my head works.
                • 5. Re: [DMX04 ] Getting Flash events and mc properties from Director   (not from GetUrl !)
                  Level 7
                  Many thanks to both of you for your tips & solutions !

                  • 6. Re: [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)
                    Ex Malterra Level 1

                    I re-read your second post this morning and realized I had missed something when I first read it in my Saturday morning haze. You're just using getURL as a means to let Director know to go ahead and start doing stuff. To be honest when I saw getURL I stopped reading because I was hoping for a way to never have to use it again! So I missed getting the whole picture. That's what I get for being lazy!
                    • 7. Re: [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)

                      i use the getUrl function alot! The trick to get no "Browserwindows" while testing in flash is: ... see attached code

                      cheers chris
                      • 8. [DMX04 ] Getting Flash events and mc properties from Director (not   from GetUrl !)
                        pete.h Level 1
                        Here's another tip for Flash-Director integration, particularly useful when passing variables to Flash components that are Arrays or Lists...

                        To pass a Lingo Linear List as an Actionscript Array:
                        someFlashFunctionName( sprite(SpriteNum).convert(#flashObjectArray, lingoLinearList) )

                        To convert an Actionscript Array to a Lingo List:
                        myLingoList=sprite(SpriteNum).convert(#list, flashArrayObject)

                        For example:
                        sprite("flash").listComponentRef.selectedIndices=sprite("flash").convert(#flashObjectArray ,[3,5,7])
                        ...will select the 4,6,&8th items in the Flash list component with the instanceName of listComponentRef, in the sprite named "flash". Note that the preceeding sprite(). reference must be the same sprite that contains the Flash movie that the Array/List is coming from/going to, not just any old sprite.

                        convert() is a very handy function that appears to have slipped in under the radar in the 10.1.1 update. It's documented here: http://www.adobe.com/support/director/flash_8_asset_xtra.pdf

                        However, the trick here, since the documentation isn't explicit, is that in order for it to work properly with components and functions within your Flash asset, you MUST use the preceeding sprite reference...
                        ... NOT just the global...
                        Unfortunately, the documentation shows the global usage, which just didn't work for me, with what I was doing.

                        Note also, that convert() does not support Lingo Property Lists and Associative Arrays, only Linear lists and Arrays. (Not in Dir10.1.1 anyway - not sure about Dir11?)

                        thebigc1974: Thanks for the .playerType tip. I've edited my example in my previous post to incorporate it. :)
                        • 9. Re: [DMX04 ] Getting Flash events and mc properties from   Director   (not   from GetUrl !)
                          Level 7
                          Thanks for this clean trick !

                          As a quick workaround, I did a global Find & Replace of "getUrl" to
                          "trace" ;-)