views:

240

answers:

3

Hi there, I were hoping that someone can help me with this Actionscript (2)

I want to load an image into my flash file, and then assign a image from a folder using the xml file - and then make it link to the url in the xml

xmlData = new XML(); xmlData.ignoreWhite = true; xmlData.onLoad = function(loaded) {    if (loaded) {   loadMovie(_root.imagePath = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue, imageLoader);   loadMovie(_root.imageurl = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue, imageLoader);   imagePath.text =
_root.imagePath;     imageurl.text = _root.imageurl;  } else {   trace("file not loaded!");  } }; xmlData.load("/flash/languages.xml");

Here is my xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <myImage> <imagePath>/flash/image1.jpg</imagePath> <imageurl>/flash/image1.html</imageurl> </myImage> <myImage> <imagePath>/flash/image2.jpg</imagePath> <imageurl>/flash/image2.html</imageurl> </myImage> <myImage> <imagePath>/flash/image3.jpg</imagePath> <imageurl>/flash/image3.html</imageurl> </myImage>
A: 

try:

imageurl.htmlText = '<a href="'+_root.imageurl+'">image link</a>';

if you're using a TextField drawn on the stage withing the IDE, make sure HTML( the <> button ) is turned on. That should be it.

good luck!

George Profenza
A: 

Without doing it for you, the general idea is something like this:

var nodes:Array = xmlData.childNodes;
var l:Number = nodes.length;
var currNode:XMLNode = null;
var mv:MovieClip

for( var i:Number = 0; i < l; i )
{
    currNode = nodes[ i ].firstChild;
    mv = ?? //use createEmptyMovieClip or a stage instance, or some such.
    mv.loadMovie( currNode.nodeValue );

    // You may need to futz with this line, but it should work.
    mv.onRelease = function(){ getURL( currNode.nextSibling.nodeValue ) }
}

Here's a helpful thread. http://www.photoshopcafe.com/cafe/viewthread.php?tid=44914

Christopher W. Allen-Poole
Thanks Christopher - I think this would be VERY helpfull can you show me with the getURL( currNode.nextSibling.nodeValue ) how do I get the xml in there...
Gerald Ferreira
Sorry, I misnamed the xmlVariable. You'll want to call this after xmlData has finished loading.
Christopher W. Allen-Poole
A: 

I'd use the MovieClipLoader because it's got better event handling when you'd like to assign traits to a MovieClip (or image in this case) after it's loaded.

This code is not tested. Nor is this code complete! It's just to give you the idea. You'll notice some comments in there where I'm telling you that you'll have to layout your images yourself. That is to say you're going to have to account for the sizes of your images and adjust each one's _x and _y after it's loaded.

Another thing, this isn't going to work very well when you CTRL + ENTER to test in the Flash IDE because it's using root relative urls for your images and links. Since the Flash IDE isn't running on a web server, it won't be able to find your images! You may want to use a special test XML file while debugging in the Flash IDE.

Also, be sure to have a root node in your XML. It's just plain bad XML to have no root node.

XML

<?xml version="1.0" encoding="utf-8"?>
<root>
   <myImage> 
      <imagePath>/flash/image1.jpg</imagePath> 
      <imageurl>/flash/image1.html</imageurl> 
   </myImage> 
   <myImage> 
      <imagePath>/flash/image2.jpg</imagePath> 
      <imageurl>/flash/image2.html</imageurl> 
   </myImage> 
   <myImage> 
      <imagePath>/flash/image3.jpg</imagePath> 
      <imageurl>/flash/image3.html</imageurl> 
   </myImage>
</root>

Actionscript (frame 1):

var xmlData:XML = new XML(); 
xmlData.ignoreWhite = true; 
xmlData.onLoad = function(loaded) {    
    if (loaded) {  
     for(var i = 0; i < this.firstChild.childNodes.length; i++) {
      var mc:MovieClip = _root.createEmptyMovieClip("item" + i, _root.getNextHighestDepth());
      var node:XMLNode = this.firstChild.childNodes[i];
      loadMovieClip(mc, node);
     }
    } else {                
     trace("file not loaded!");      
    } 
}; 
xmlData.load("/flash/languages.xml");
function loadMovieClip(mc:MovieClip, node:XMLNode) {
    var loadListener:Object = new Object();
    loadListener.onLoadInit = function(target:MovieClip, httpStatus:Number) {
     //Be sure to do your positioning here!!! You probably
     // don't want to stack them on top of each other.
     // I didn't do that so you'll have to do it yourself.
     target._x = 30;
     target._y = 30;
     var url = node.childNodes[1].firstChild.nodeValue;
     target.onMouseUp = function() {
      getURL(url);
     }
     //be sure to do positioning of your text field too.
     var txt:TextField = _root.createTextField(target._name + "_text", _root.getNextHighestDepth(), 30, 100, 100, 20);
     txt.text = url;
     txt.type = "static";
     txt.onMouseUp = function() {
      getURL(url);
     }
    }
    var mcl:MovieClipLoader = new MovieClipLoader();
    mcl.addListener(loadListener);
    mcl.loadClip(node.childNodes[0].firstChild.nodeValue, mc);
}

I hope that helps.

blesh