Link to home
Start Free TrialLog in
Avatar of valentin_v
valentin_v

asked on

Import ActionScripts from another file

This is a continuing from https://www.experts-exchange.com/questions/20788652/Import-ActionScripts-from-another-file.html

How can I do same thing with another file.

I have solution from above working fine, But I need one more thing. To import another file that will draw the box over the triangles. I don;t want a link for it, just a box, so it can't be filled with color.

data_xml = new XML();
data_xml.path = this;
data_xml.ignoreWhite = true;
data_xml.onLoad = function(ok)
{
     if(ok){
          this.path.drawTriangles(this.firstChild.childNodes);
     } else {
          trace("XML FILE NOT FOUND");
     }
};
function drawTriangles(triangles_xml)
{
     var pen = this.createEmptyMovieClip("pen", 1);
     for(var i = 0; i < triangles_xml.length; i++){
          var attributes = triangles_xml[i].attributes;
          pen.lineStyle(attributes.thickness, parseInt(attributes.rgb, 16), attributes.alpha);
          pen.moveTo(attributes.mx, attributes.my);
          for(var j = 1; j < 5; j++){
               pen.lineTo(attributes["x" + j], attributes["y" + j]);
          }
     }
}
data_xml.load("box.xml");

Works fine. It will create Box I need. How do I get them to work together.


I tried putting the box code in  triangles.xml with triangles, Its drawn but filled with color as its created over triangles, and takes up their links.

I tried creating another layer, and doing same thing with box, but only box gets displayed, no triangles.

Because coordinates will be dynamically generated, I need it to read new file lets say every 10 seconds.
Avatar of negatyve
negatyve

What's inside box.xml?
Avatar of valentin_v

ASKER

<?xml version="1.0" encoding="UTF-8"?>
<triangles>
     <item thickness="1" rgb="002040" alpha="100" mx="140" my="10"  x1="160" y1="10" x2="160" y2="100" x3="140" y3="100" x4="140" y4="10" />
     <item thickness="1" rgb="002040" alpha="100" mx="180" my="10"  x1="260" y1="10" x2="260" y2="100" x3="180" y3="100" x4="180" y4="10" />
</triangles>
ok, only one xml file, "shapes.xml":

=================================================================
<?xml version="1.0" encoding="UTF-8"?>
<shapes>
      <triangles>
            <item
                  link="http://www.altavista.com/" target="_blank"
                  thickness="1" rgb="002040" alpha="100"
                  mx="100" my="130"
                  x1="358" y1="130"
                  x2="358" y2="260"
                  x3="100" y3="130" />
            <item
                  link="http://www.yahoo.com/" target="_self"
                  thickness="2" rgb="FFCC00" alpha="20"
                  mx="400" my="130"
                  x1="663" y1="130"
                  x2="400" y2="232"
                  x3="400" y3="130" />
      </triangles>
      <boxes>
            <item
                  thickness="1" rgb="002040" alpha="100"
                  mx="140" my="10"
                  x1="160" y1="10"
                  x2="160" y2="100"
                  x3="140" y3="100"
                  x4="140" y4="10" />
            <item
                  thickness="1" rgb="002040" alpha="100"
                  mx="180" my="10"
                  x1="260" y1="10"
                  x2="260" y2="100"
                  x3="180" y3="100"
                  x4="180" y4="10" />
      </boxes>
</shapes>
=================================================================


and only one movie, with this code:

=================================================================
data_xml = new XML();
data_xml.path = this;
data_xml.ignoreWhite = true;
data_xml.onLoad = function(ok)
{
      if (ok) {
            this.path.drawTriangles(this.firstChild.firstChild.childNodes);
            this.path.drawBoxes(this.firstChild.lastChild.childNodes);
      } else {
            trace("XML FILE NOT FOUND");
      }
};
function drawTriangles(triangles_xml)
{
      for (var i = 0; i < triangles_xml.length; i++) {
            var pen = this.createEmptyMovieClip("pen" + i, i);
            var attributes = triangles_xml[i].attributes;
            pen.lineStyle(attributes.thickness, parseInt(attributes.rgb, 16), attributes.alpha);
            pen.moveTo(attributes.mx, attributes.my);
            pen.beginFill(parseInt(attributes.rgb, 16), attributes.alpha);
            for (var j = 1; j < 4; j++)pen.lineTo(attributes["x" + j], attributes["y" + j]);
            pen.endFill();
            pen.__target = attributes.target;
            pen.__link = attributes.link;
            pen.onRelease = function()
            {
                  this.getURL(this.__link, this.__target);
            };
      }
}
function drawBoxes(boxes_xml)
{
      for (var i = 0; i < boxes_xml.length; i++) {
            var pen = this.createEmptyMovieClip("pen" + i, i + 1000);
            var attributes = boxes_xml[i].attributes;
            pen.lineStyle(attributes.thickness, parseInt(attributes.rgb, 16), attributes.alpha);
            pen.moveTo(attributes.mx, attributes.my);
            for (var j = 1; j < 5; j++)      pen.lineTo(attributes["x" + j], attributes["y" + j]);
      }
}
data_xml.load("shapes.xml");
=================================================================
How do I get it to refresh with new values taken from SHAPES.XML lets say every 5 seconds
data_xml = new XML();
data_xml.path = this;
data_xml.ignoreWhite = true;
data_xml.onLoad = function(ok)
{
      if (ok) {
            this.triangles = this.firstChild.firstChild.childNodes;
            this.boxes = this.firstChild.lastChild.childNodes;
            this.path.drawTriangles();
            this.path.drawBoxes();
            clearInterval(this.loadID);
            this.loadID = setInterval(this, "updateData", 5000);
      } else {
            trace("XML FILE NOT FOUND");
      }
};
data_xml.updateData = function()
{
      var total = this.triangles.length + this.boxes.length;
      for(var i = 0; i < total; i++)this.path["pen" + i].removeMovieClip();
      delete this.triangles;      delete this.boxes;
      this.load("shapes.xml");
}
function drawTriangles()
{
      for (var i = 0; i < data_xml.triangles.length; i++) {
            var pen = this.createEmptyMovieClip("pen" + i, i);
            var attributes = data_xml.triangles[i].attributes;
            pen.lineStyle(attributes.thickness, parseInt(attributes.rgb, 16), attributes.alpha);
            pen.moveTo(attributes.mx, attributes.my);
            pen.beginFill(parseInt(attributes.rgb, 16), attributes.alpha);
            for (var j = 1; j < 4; j++)pen.lineTo(attributes["x" + j], attributes["y" + j]);
            pen.endFill();
            pen.__target = attributes.target;      pen.__link = attributes.link;
            pen.onRelease = function()      {      this.getURL(this.__link, this.__target);      };
      }
}
function drawBoxes()
{
      var precs = data_xml.triangles.length;
      for (var i = precs; i < data_xml.boxes.length + precs; i++) {
            var pen = this.createEmptyMovieClip("pen" + i, i);
            var attributes = data_xml.boxes[i - precs].attributes;
            pen.lineStyle(attributes.thickness, parseInt(attributes.rgb, 16), attributes.alpha);
            pen.moveTo(attributes.mx, attributes.my);
            for (var j = 1; j < 5; j++)      pen.lineTo(attributes["x" + j], attributes["y" + j]);
      }
}
data_xml.load("shapes.xml");
Works fine ok Computer but on the web files don;t updated. They get cached, so how to reload a file so It does not get cached by FLASH.

That would be the last problem to get fixed with this .

Change this function:

data_xml.updateData = function()
{
    var total = this.triangles.length + this.boxes.length;
    for(var i = 0; i < total; i++)this.path["pen" + i].removeMovieClip();
    delete this.triangles;     delete this.boxes;
    this.load("shapes.xml");
}

to

data_xml.updateData = function()
{
    var total = this.triangles.length + this.boxes.length;
    for(var i = 0; i < total; i++)this.path["pen" + i].removeMovieClip();
    delete this.triangles;     delete this.boxes;
    this.load("shapes.xml?updated=" + (new Date().getTime()));
}
yOU THE BEST.

Sorry but only one more thing, Is there a way to split shapes.xml, into 2 files one for TRIANGLES, one for BOXES to keep them separate for easyer updating of values.

Thanks again, and this is for sure last thing
ASKER CERTIFIED SOLUTION
Avatar of negatyve
negatyve

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial