PDA

View Full Version : Problem with navigateToUrl when passing string var to mouseEvent.



ziggaloop
September 18th, 2009, 09:17 AM
the XML file:


<?xml version="1.0" encoding="utf-8"?>
<XML>
<myXMLList>

<ListItem>
<itemImage>img/francois.jpg</itemImage>
<itemUrl>http://www.consulting.xerox.com/Track/thoughtleaders+bios+ragnet+downloads+podcasts+futu reofdocs+mp3+link/http:/www.xerox.com/downloads/usa/en/t/TL_Francois_Ragnet_DocumentManagement_final.mp3</itemUrl>
<itemText>Total duration: 12 minutes
Audio: MP3, 5.4MB
Francois Ragnet of Xerox Global Services explains that the future of documents and document management is here now. Learn about how the role of the document within the enterprise is changing as it becomes much more dynamic and rich through a concept that we call smarter documents.
</itemText>
</ListItem>

<ListItem>
<itemImage>img/image002.jpg</itemImage>
<itemUrl>http://www.xerox.com/downloads/usa/en/t/TL_Dave_Drab_InformationSecurity_final.mp3</itemUrl>
<itemText>Total Duration: 0 hr. 11 min.
Audio: MP3, 5.4MB
Dave explains what corporations today need to do to in the area of information security that helps create competitive advantage.
</itemText>
</ListItem>
<ListItem>
<itemImage>img/image003.jpg</itemImage>
<itemUrl>http://docushare.xerox.com/doug</itemUrl>
<itemText>333333. And I like how the quick brown fox jumps over the lazy dog.</itemText>
</ListItem>

<ListItem>
<itemImage>img/image004.jpg</itemImage>
<itemUrl>http://docushare.xerox.com/doug</itemUrl>
<itemText>444444. And I like how the quick brown fox jumps over the lazy dog.</itemText>
</ListItem>


</myXMLList>
</XML>


the Flash:





var j:Number = 0 ;
var myXML:XML = new XML();
var XML_URL:String = "http://localhost/pss/movie/action_XML_menu.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);


function xmlLoaded(event:Event):void {
trace(myLoader.data);

// Place the xml data into the myXML object
myXML = XML(myLoader.data);
// Initialize and give var name to the new external XMLDocument
var xmlDoc:XMLDocument = new XMLDocument();
// Ignore spacing around nodes
xmlDoc.ignoreWhite = true;
// Define a new name for the loaded XML that is the data in myLoader
var menuXML:XML = XML(myLoader.data);
// Parse the XML data into a readable format
xmlDoc.parseXML(menuXML.toXMLString());

// Variable to assign to list items
var n:Number = myXML..ListItem.length();


//define a new loop for URL array

for each (var itemList:XML in myXML..itemUrl){
var itemZrl:String = itemList.toString();

trace (itemList);
}


}


What am i doing wrong? why is only the last url passed to the mouse events?

Krilnon
September 18th, 2009, 10:42 AM
Well, you didn't really post the part of the code that had anything to do with MouseEvents, but there seemed to be a lot of random extra stuff in your code that wasn't really necessary. My guess would be that you're doing something like accessing itemZrl from your MouseEvent handler. However, it's only going to have one value at once, so it's going to be the last item that was iterated through.

Here's perhaps a more concise way to write (mostly) the same thing, by the way:
var XML_URL:String = 'test.xml';

var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
myLoader.load(new URLRequest(XML_URL));

function xmlLoaded(event:Event):void {
for each (var itemList:XML in XML(event.target.data)..itemUrl) {
trace('item: ', itemList);
}
}

ziggaloop
September 18th, 2009, 12:46 PM
OMG!!! you are absolutely right, forgive me it was late when i posted that... Total brain fart!

Here is the mouse event part:

for each (var ListItem:XML in myXML..ListItem) {

var itemUrl:String = ListItem.itemUrl.toString();

// I can trace the itemUrl here and get the correct value...

function getLink(event:MouseEvent):void{
var loader:URLLoader = new URLLoader();

var request:URLRequest = new URLRequest

navigateToURL(request, "_blank");

// Tracing itemUrl here only gives me that last item in the array

}

clip_parent.addEventListener(MouseEvent.CLICK, getLink);
// tracing itemUrl here is all good too!




}

thatsasif
September 18th, 2009, 12:56 PM
how can you put a function in for loop.

ziggaloop
September 18th, 2009, 03:41 PM
This resolves the problem:

// Make the clip link ready

clip_parent.buttonMode = true;
clip_parent.itemURL = itemUrl;
clip_parent.addEventListener(MouseEvent.CLICK, getLink);

}

//end of the for loop


//add the URL event listener.

function getLink(event:MouseEvent):void{

var loader:URLLoader = new URLLoader();

var request:URLRequest=new URLRequest(event.currentTarget.itemURL);

//trace("clicked " + event.currentTarget.name+ " should link to " + event.currentTarget.itemURL);
navigateToURL(request, "_blank");

//end