6 Replies Latest reply on Jul 9, 2006 6:01 PM by ManOfMac

    Flash Tree Object & node list

    ManOfMac Level 1
      Hi All,
      Unsure if this is a noodle scratcher or undoable!

      Either way, does anyone know of a way when using the 'tree' flash component in Director to get the component to supply a list of some sort back to director.

      I'm adding & subtracting branches to the Object successfully, but I figure it must know whats where I just need it to tell Director so I can manipulate it out to a XML file !!

      Cheers


        • 1. Re: Flash Tree Object & node list
          Ex Malterra Level 1
          You should go ahead and use a Flash XML object as the data source for the Tree, and do whatever Director interfacing you need to do with that XML object. Here's some quick code to get you started.

          First, drag a Flash Tree onto the stage, make sure it's sprite 1.

          First frame code:

          function exitFrame(me)
          {

          // reference variable
          var fTree = sprite(1);

          // create XML object in Flash sprite
          _global.xmlObj = fTree.newObject("XML");
          _global.xmlObj.ignoreWhite = true;
          // load external XML file frome server, use xmlObjLoaded to store "is loaded" Boolean
          _global.xmlObjLoaded = _global.xmlObj.load(" http://www.extrek.net/xml/solTree.xml");

          }

          Second frame code:

          function exitFrame(me)
          {

          // loop until source file is loaded
          (_global.xmlObjLoaded) ? sprite(1).dataProvider = _global.xmlObj : _movie.go(_movie.frame)


          // trace some xmlObj info
          trace(xmlObj.firstChild.attributes.label + ":");
          for (var counter = 0; counter < xmlObj.firstChild.childNodes.length; counter++)
          {
          trace("\t" + xmlObj.firstChild.childNodes[counter].attributes.label);
          }

          }

          Third frame code:

          function exitFrame(me)
          {

          _movie.go(_movie.frame);

          }


          A simple example, but hope it points you in the right direction. There's material on using Flash components in Director docs and in Flash docs.
          • 2. Re: Flash Tree Object &amp; node list
            ManOfMac Level 1
            Thnx for the info, after your reply & dredging the web, I definetly think I'm in over my head here. This is my development angle:

            Picture this:
            In director, drag Library Object > Tree to stage

            Pass a whole swag of nodes at the start (hard coded in lingo), then when user clicks on a node then in the message window (using the slectedNode function) I type sendallSprites(#addnode, "A Folder Name")

            on addNode me ,FolderName
            theFile = _rSprite.selectedNode.addTreeNodeAt(0, FolderName )
            end
            Cool, the flash object add's a node under the clicked branch & displays it on screen !!

            Now we have lets say 1 root, 2 branches and 2 subs per branch !!
            I want the structure in the Flash Object returned to Director & then create and save an XML doc. that cab be opened next time.
            I've tested various approaches and the XML parser (list) returns a list of extraneuos information when al lI want from the flash object is:
            <node label="root">
            <node label='Folder01'>
            <node label='SubFolder'></node>
            </Node>
            <node label='Folder02'>
            <node label='SubFolder'></node>
            </node>
            </node>

            As mentioned at the start, I think I'm drawing a long bow !!
            • 3. Re: Flash Tree Object &amp; node list
              Ex Malterra Level 1
              quote:

              Originally posted by: ManOfMac
              As mentioned at the start, I think I'm drawing a long bow !!


              nah dude, you'll get it. sounds like you've got it thought out well enough.

              local file huh? i did pretty much what you're talking about a few months ago. you can use fileio to open the xml file and type cast it to a string, then pass that string to the flash xml obj for parsing. it's easier for the tree that way. the user's added nodes just have to be set up a certain way. then when you're ready to save just use toString to get the flash xml as a string, and save it as the external file. lather, rinse, repeat.

              i just checked my old file, it was authored before the new flash asset xtra so needs to be reworked. i'm a little bleary at the moment (i like beer), i'll pass you some code tomorrow from work when i get a chance. gotta forgive me using javascript syntax vice lingo though, i am way outta practice w/ lingo. and forget director's xml xtra. after using both i've found the flash xml obj to be allot easier to work with. but that's just me, others may disagree.
              • 4. Re: Flash Tree Object &amp; node list
                ManOfMac Level 1
                The source would be invaluable, as the title suggest "this is doin' my head in"

                Thanx :-)
                • 5. Re: Flash Tree Object &amp; node list
                  Ex Malterra Level 1
                  Alright, this is a long one. Make a Director file based on the following and when it starts it’ll open the xmlDoc.xml XML file, parse it into a Flash XML object and assign that object as the dataProvider for a Flash component Tree. Then you can select a planet node from the Tree, enter text into the Flash component text field, and click the Flash button to add a moon to the selected planet node with the entered text as its label. The button also saves whatever XML makes up the tree to the xmlDoc.xml XML file by calling the saveXml() function. You could have whatever event you want call the save function.

                  To start, drag a Flash Tree component to the stage as sprite 1, a Flash TextInput component to the stage as sprite 2 and a Flash Button component to the stage as sprite 3. Make sure to do Modify>Movie>Xtras… and add the FileIo.x32 Xtra so it’ll work if you make a projector.

                  Add this script to Frame one:

                  function exitFrame(me)
                  {

                  ///// make Fileio obj, Flash XML obj, populate Tree
                  // Fileio
                  var fileObj = new xtra("fileio");
                  fileObj.openFile(_movie.path + "xmlDoc.xml",0);
                  var xmlString = fileObj.readFile();
                  fileObj.closeFile();
                  fileObj = null;
                  // Flash XML obj
                  var fTree = sprite(1);
                  var fXmlObj = fTree.newObject("XML");
                  fXmlObj.ignoreWhite = true;
                  fXmlObj.parseXML(xmlString);
                  // populate Tree
                  fTree.dataProvider = fXmlObj;

                  ///// make button listerner to handle clicks
                  var fButton = sprite(3);
                  var fButtonEvt = fButton.newObject("Object");
                  fButton.addEventListener("click",fButtonEvt);
                  fButton.setCallback(fButton, "click", symbol("fButtonClick"), "fButtonEvtScript");

                  }

                  Make this Movie Script, name it fButtonEvtScript:

                  function fButtonClick(me,evtObj)
                  {

                  var fTree = sprite(1);
                  var fText = sprite(2);

                  // determine if a Tree node is selected
                  try
                  {
                  String(fTree.selectedNode.attributes.label);
                  // execute if node selected is planet
                  if (fTree.selectedNode.nodeName == "planet")
                  {
                  var newNode = fTree.newObject("XMLNode",1,"moon");
                  // make node lable text field if text present, else default text
                  if (fText.text == "")
                  {
                  newNode.attributes.label = "Default Moon";
                  } else {
                  newNode.attributes.label = fText.text;
                  }
                  // add new moon node
                  fTree.selectedNode.addTreeNode(newNode);
                  saveXml();
                  } else {
                  _player.alert("Must select planet node");
                  }
                  } catch (errObj) {
                  // alert no node
                  _player.alert("No Node Selected");
                  }

                  }

                  function saveXml()
                  {

                  var fTree = sprite(1);
                  // make string to save
                  var xmlString = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n"
                  xmlString += fTree.dataProvider.firstChild.toString();
                  // save file
                  var fileObj = new xtra("fileio");
                  fileObj.openFile(_movie.path + "xmlDoc.xml",0);
                  fileObj["delete"].call(fileObj);
                  fileObj.createFile(_movie.path + "xmlDoc.xml");
                  fileObj.openFile(_movie.path + "xmlDoc.xml",0);
                  fileObj.writeString(xmlString);
                  fileObj.closeFile();
                  fileObj = null;

                  }

                  Finaly add this script to any Frame after Frame one; I used Frame 10.

                  function exitFrame(me)
                  {

                  _movie.go(_movie.frame);

                  }

                  Here’s the XML file:

                  <?xml version="1.0" encoding="iso-8859-1" ?>
                  <star label="Sol">
                  <planet label="Mercury" />
                  <planet label="Venus" />
                  <planet label="Terra">
                  <moon label="Luna" />
                  </planet>
                  <planet label="Mars">
                  <moon label="Phobos" />
                  <moon label="Deimos" />
                  </planet>
                  <planet label="Jupiter">
                  <moon label="Callisto" />
                  <moon label="Ganymede" />
                  <moon label="Europa" />
                  <moon label="Io" />
                  </planet>
                  <planet label="Saturn">
                  <moon label="Titan" />
                  </planet>
                  <planet label="Uranus" />
                  <planet label="Neptune" />
                  <planet label="Pluto">
                  <moon label="Charon" />
                  </planet>
                  </star>

                  Sorry I got slack with the code comments, but I reckon you can figure out what’s going on. I figure this’ll give you a good start on what you’re trying to accomplish. I strongly suggest checking out the LiveDocs for Director regarding Flash and Components and Flash regarding Flash XML, XMLNode and the Components Language Reference. Remember though, things have changed since the Flash 8 Asset Xtra, so some of the documentation is out of date.
                  • 6. Re: Flash Tree Object &amp; node list
                    ManOfMac Level 1
                    Malterra,
                    I owe you a debt of grattitude, thanks for the code, it has set me on the righteous path without the need for artifical stimulants ;-) !!