View Full Version : assigning URL to MCs via XML
trick
February 23rd, 2009, 12:02 PM
So I'm trying to get better at AS3, and am working on a file that uses XML. I've got some of it to work, but can't wrap my head around this. My XML looks kinda like this:
<portfolio>
<piece>
<image>portfolio/01.jpg</image>
<description>one</description>
<link>http://somelink.com/</link>
</piece>
</portfolio>
with more than one "piece" obviously.
I can get the XML and do stuff with the images (put them all on the stage with a for loop). Next I want to make the images link to the URLs, but I can't figure out how to assign the corresponding variable to the movieclips.
I load the XML and run my for loop:
function xmlLoaded(e:Event):void {
var loadedxml:XML = new XML(e.target.data);
for (var i:uint=0; i<loadedxml.piece.length(); i++)
{
var pieceholder:MovieClip = new MovieClip();
portfolio.addChild(pieceholder);
pieceholder.buttonMode = true;
// how to get just the URL we need?
linky = loadedxml.piece.link.text()[i];
var pieceloader = new Loader();
var pictURLReq:URLRequest = new URLRequest(loadedxml.piece.image.text()[i]);
pieceholder.addChild(pieceloader);
pieceloader.load(pictURLReq);
pieceloader.addEventListener(MouseEvent.MOUSE_DOWN , linkygo);
}
}
and have a function for the link click:
function linkygo(e:MouseEvent):void
{
var strURL:URLRequest = new URLRequest(linky);
navigateToURL(strURL, "_blank");
}
and what happens? all the buttons go to the same link, the last one from my XML.
how can I make the buttons go to the correct, corresponding URL?
Thanks!!!
e_owen
February 23rd, 2009, 12:35 PM
at present you are not declaring 'linky' as local to that MovieClip and so there is only instantiation of it- which is being updated with the new xml data every time. in the loop you need to say:
pieceholder.linky = loadedxml.peice.link.text()[i];
then in the ClickHandler function:
var strURL:URLRequest = newURLRequest(e.currentTarget.linky);
trick
February 23rd, 2009, 12:58 PM
thanks for the quick reply! it still isn't working though, it throws this error:
ReferenceError: Error #1069: Property linky not found on flash.display.Loader and there is no default value.
tracing "e.currentTarget" returns: [object Loader]
e_owen
February 23rd, 2009, 01:04 PM
thanks for the quick reply! it still isn't working though, it throws this error:
ReferenceError: Error #1069: Property linky not found on flash.display.Loader and there is no default value.
tracing "e.currentTarget" returns: [object Loader]
I said to create the 'linky' property on peiceholder by mistake, create it on peiceloader instead as that is dispatching the click event.
peiceloader.linky = ...
trick
February 23rd, 2009, 01:11 PM
now I get this:
TypeError: Error #1010: A term is undefined and has no properties.
at portfol_fla::MainTimeline/xmlLoaded()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
:(
here's the updated code:
function xmlLoaded(e:Event):void {
var loadedxml:XML = new XML(e.target.data);
for (var i:uint=0; i<loadedxml.piece.length(); i++)
{
var pieceholder:MovieClip = new MovieClip();
portfolio.addChild(pieceholder);
pieceholder.buttonMode = true;
var pieceloader = new Loader();
var pictURLReq:URLRequest = new URLRequest(loadedxml.piece.image.text()[i]);
pieceholder.addChild(pieceloader);
pieceloader.load(pictURLReq);
// how to get just the URL we need?
pieceloader.linky = loadedxml.piece.link.text()[i];
pieceloader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, pieceloaderComplete);
pieceloader.addEventListener(MouseEvent.MOUSE_DOWN , linkygo);
}
}
function linkygo(e:MouseEvent):void
{
var strURL:URLRequest = new URLRequest(e.currentTarget.linky);
navigateToURL(strURL, "_blank");
}
e_owen
February 23rd, 2009, 04:01 PM
So it appears that you cannot add a property to a Loader, but you definitely can to a MovieClip... If you switch the CLICK event and linky property to the pieceholder MovieClip that will work fine, obviously the hit area will be the entire pieceholder MovieClip so you can create a new MovieClip inside pieceholder that will hold the Loader and have the linky property and CLICK event.
trick
February 23rd, 2009, 04:58 PM
thanks! ok, I tried:
pieceholder.linky = loadedxml.piece.link.text()[i];
pieceholder.addEventListener(MouseEvent.MOUSE_DOWN , linkygo);
and still get
ReferenceError: Error #1069: Property linky not found on flash.display.Loader and there is no default value.
at portfol_fla::MainTimeline/xmlLoaded()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
it would be fine to have all of pieceholder be the hit area... am I just not defining the linky variable in the correct way? is there some syntax that I'm missing?
e_owen
February 23rd, 2009, 05:29 PM
Have you tried tracing
loadedxml.piece.link.text()[i];
to make sure that it has a value of any kind? You could try hard-coding a string to 'linky' and see if that works, then you would know that the error is when parsing the xml.
check out http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm for all your xml needs...
trick
February 23rd, 2009, 05:34 PM
yep, and it does have the correct value (the full URL from the XML).
I just can't pass it correctly to the click function for each piece. argh.
e_owen
February 23rd, 2009, 06:31 PM
yep, and it does have the correct value (the full URL from the XML).
I just can't pass it correctly to the click function for each piece. argh.
Can you post your .fla?
trick
February 23rd, 2009, 06:44 PM
thanks for taking a look!
e_owen
February 23rd, 2009, 07:11 PM
The only problem with your file was that you were tracing pieceloader.linky which no longer existed since we addded the property to pieceholder... The attached .fla works as it should.
trick
February 23rd, 2009, 07:34 PM
d'oh! never thought about the trace.
thanks e_owen!
^_^b
ddot
September 9th, 2009, 12:56 AM
this helped me greatly... what a GodSend... sometimes it's hard to ask the right question... or explain yourself to google where you can get pointed in the right direction
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.