Solved

Import ActionScripts from another file

Posted on 2003-11-10
9
309 Views
Last Modified: 2010-04-03
This is a continuing from http://www.experts-exchange.com/Web/WebDevSoftware/Flash/Q_20788652.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.
0
Comment
Question by:valentin_v
  • 5
  • 4
9 Comments
 
LVL 27

Expert Comment

by:negatyve
ID: 9717276
What's inside box.xml?
0
 

Author Comment

by:valentin_v
ID: 9717531
<?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>
0
 
LVL 27

Expert Comment

by:negatyve
ID: 9717642
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");
=================================================================
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:valentin_v
ID: 9721948
How do I get it to refresh with new values taken from SHAPES.XML lets say every 5 seconds
0
 
LVL 27

Expert Comment

by:negatyve
ID: 9722026
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");
0
 

Author Comment

by:valentin_v
ID: 9722091
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 .

0
 
LVL 27

Expert Comment

by:negatyve
ID: 9722102
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()));
}
0
 

Author Comment

by:valentin_v
ID: 9722159
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
0
 
LVL 27

Accepted Solution

by:
negatyve earned 500 total points
ID: 9725209
boxes.xml


<?xml version="1.0" encoding="UTF-8"?>
<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>

=====================================================
=====================================================

triangles.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>


=====================================================
=====================================================

movie script:


triangles_xml = new XML();
triangles_xml.path = this;
triangles_xml.ignoreWhite = true;
triangles_xml.onLoad = function(ok)
{
      if (ok) {
            this.triangles = this.firstChild.childNodes;
            this.drawTriangles();
            clearInterval(this.loadID);
            this.loadID = setInterval(this, "updateData", 5000);
      } else {
            trace("XML FILE NOT FOUND");
      }
};
triangles_xml.updateData = function()
{
      for (var i = 0; i < this.triangles.length; i++)      this.path["tre" + i].removeMovieClip();
      delete this.triangles;
      this.load("triangles.xml");
};
triangles_xml.drawTriangles = function()
{
      for (var i = 0; i < this.triangles.length; i++){
            var pen = this.path.createEmptyMovieClip("tre" + i, i);
            var attributes = this.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);
            };
      }
}
triangles_xml.load("triangles.xml");

boxes_xml = new XML();
boxes_xml.path = this;
boxes_xml.ignoreWhite = true;
boxes_xml.onLoad = function(ok)
{
      if (ok) {
            this.boxes = this.firstChild.childNodes;
            this.drawBoxes();
            clearInterval(this.loadID);
            this.loadID = setInterval(this, "updateData", 5000);
      } else {
            trace("XML FILE NOT FOUND");
      }
};
boxes_xml.updateData = function()
{
      for (var i = 0; i < this.boxes.length; i++)      this.path["box" + i].removeMovieClip();
      delete this.boxes;
      this.load("boxes.xml");
};

boxes_xml.drawBoxes = function()
{
      for (var i = 0; i < this.boxes.length ; i++) {
            var pen = this.path.createEmptyMovieClip("box" + i, i + 1000);
            var attributes = this.boxes[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]);
            }
      }
}
boxes_xml.load("boxes.xml");
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Download slideshow or picture elements 16 437
Stock Videos 17 332
FLV versus SWF 11 277
What changes do I need to make when I create the MST file for Flash 18 6 121
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question